Powershell 展平文件名中包含foldername的文件

Powershell 展平文件名中包含foldername的文件,powershell,Powershell,我想扁平化文件夹结构,并以一种方式将每个父目录名包含到文件名中。我已尝试此操作,但出现错误: Missing ')' in method call 我看不出有什么问题 (ls -r -include *.ext) | % { mv -literal $_\3 $_\3.Name.Insert(0, [String]::Format("{0} - ", $_\3.Directory.Name))} 是否要展平文件夹结构并将所有重命名的文件移动到根目录?例如: $rootPath = 'C:\T

我想扁平化文件夹结构,并以一种方式将每个父目录名包含到文件名中。我已尝试此操作,但出现错误:

Missing ')' in method call
我看不出有什么问题

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

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

$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"
} 

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

$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"
} 
试试这个:

ls . -r *.ext -name | ?{!$_.PSIsContainer} | mi -dest {$_ -replace '\\','_'} -whatif
或者如果在V3上:

ls . -r *.ext -name -file | mi -dest {$_ -replace '\\','_'} -whatif
移除
-whatif
以实际执行移动。

尝试以下操作:

ls . -r *.ext -name | ?{!$_.PSIsContainer} | mi -dest {$_ -replace '\\','_'} -whatif
或者如果在V3上:

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

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

是。我想将任何子文件夹中存在的具有给定扩展名的所有文件移动到根目录,并将其文件名更改为filename,包括将文件夹结构更改为root。脚本引发以下错误:无法对空值表达式调用方法。在C:\remove.ps1:3 char:42+[string]$newFilename=$\ux.Name.Insert(是的。我想将任何子文件夹中存在的具有给定扩展名的所有文件移动到根目录,并将其文件名更改为filename,包括将文件夹结构更改为root目录。脚本引发以下错误:无法对空值表达式调用方法。在C:\remove.ps1:3 char:42+[string]$newFilename=$\ Name.Insert(如果路径包含方括号[],则失败。似乎必须使用LiteralPath。您可以使用它更新吗?如果路径包含方括号[],则失败。似乎必须使用LiteralPath。您可以使用它更新吗?