【发布时间】:2019-12-03 20:38:37
【问题描述】:
尝试在文件夹中搜索尚未以 *.txt 结尾且 1 天内未修改的文件,并将扩展名重命名为 .txt
$app_files = get-childitem "C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test" -recurse -exclude *.txt | where-object {$_.LastWriteTime -lt (Get-Date).AddDays(-1)}
foreach ( $file in $app_files ) {
$newfile = $file.Name + ".txt"
Rename-Item -Literalpath "C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test\$file" $newfile
}
重命名项目:无法重命名,因为项目位于 'C:\Users\adm.aross\Desktop\重命名 DHL 文件 - TKT0087521\Test\C:\Users\adm.aross\Desktop\重命名 DHL 文件 - TKT0087521\Test\01-06-2019.log' 不存在。在行:5 字符:9 + 重命名项目 -Literalpath "C:\Users\adm.aross\Desktop\Rename D ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
重命名项目:无法重命名,因为项目位于 'C:\Users\adm.aross\Desktop\重命名 DHL 文件 - TKT0087521\Test\C:\Users\adm.aross\Desktop\重命名 DHL 文件 - TKT0087521\Test\01-07-2019.log' 不存在。在行:5 字符:9 + 重命名项目 -Literalpath "C:\Users\adm.aross\Desktop\Rename D ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
重命名项目:无法重命名,因为项目位于 'C:\Users\adm.aross\Desktop\重命名 DHL 文件 - TKT0087521\Test\C:\Users\adm.aross\Desktop\重命名 DHL 文件 - TKT0087521\Test\08-05-2019.log.log' 不存在。在行:5 字符:9 + 重命名项目 -Literalpath "C:\Users\adm.aross\Desktop\Rename D ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
【问题讨论】:
-
请修正您的代码格式。我不知道什么是代码,什么是错误文本……或者在那可怕的混乱中是否有任何代码。 [咧嘴]
-
发生错误是因为
"$file"、$file.ToString()和$file.FullName是相同的字符串。试试… -Literalpath "C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test\$($file.Name)" … -
-Literalpath $file.FullName