[英]How to convert GCI DirectoryName to string for .Substring()
嘗試從此輸出直接父文件夾,因為完整路徑太大。 最終目標是從修改日期排序的不同文件夾輸出重復文件。
我想我需要將gci DirectoryName
輸出轉換為字符串,所以我可以.Substring($LenRootPath)
如下所示:
我試過-Expand
,但它無法將System.Object
轉換為System.String
。 我也嘗試了[string[]]
,它被忽略了,並嘗試了.Substring($rootPath.Length)
,但仍然得到一個System.Object
。
將gci directoryname
為字符串?
$rootPath = "c:\test"
$array = @(Join-Path $rootPath "\1111\",
Join-Path $rootPath "\2222\",
Join-Path $rootPath "\3333\")
Get-ChildItem -Path $array -Filter "*.doc" -Recurse |
where {!$_.PSIsContainer} |
Select-Object DirectoryName, Name, LastWriteTime |
Sort-Object Name, LastWriteTime -Desc |
Format-Table @{
Name="LastWriteTime";
Expression={$_.LastWriteTime.ToString("yyyy-MM-dd HH:mm")}
}, DirectoryName -GroupBy Name
代碼的問題在於$array
的定義。 您需要在每個周圍添加括號,以便cmdlet在被解釋為數組中的字符串之前執行。 例如:
$array = @((join-path $rootPath "\1111\"),
(join-path $rootPath "\2222\"),
(join-path $rootPath "\3333\"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.