繁体   English   中英

如何将GCI DirectoryName转换为.Substring()的字符串

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM