压缩文件,包括文件名中的文件夹名称

时间:2012-12-19 19:05:35

标签: powershell

我想展平文件夹结构,并以一种方式将每个父目录名称包含在文件名中。我试过这个,但得到一个错误:

Missing ')' in method call

我完全看不出有什么问题

(ls -r -include *.ext) | % { mv -literal $_\3 $_\3.Name.Insert(0, [String]::Format("{0} - ", $_\3.Directory.Name))}

2 个答案:

答案 0 :(得分:4)

试试这个:

ls . -r *.ext -name | ?{!$_.PSIsContainer} | mi -dest {$_ -replace '\\','_'} -whatif

或者如果在V3上:

ls . -r *.ext -name -file | mi -dest {$_ -replace '\\','_'} -whatif

删除-whatif以实际执行移动。

答案 1 :(得分:1)

是否要展平文件夹结构并将所有重命名的文件移动到根目录?例如:

$rootPath = 'C:\TempPath'
(ls $rootPath -r -include *.ext) | %{
    [string]$newFilename = $_.Name.Insert(0, [String]::Format("{0} - ", $_.Directory.Name))
    #write-host $newFilename
    mv -literal $_ "$rootPath$newFilename"
} 
相关问题