【问题标题】:cannot rename because item does not exist无法重命名,因为项目不存在
【发布时间】: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

标签: powershell powershell-4.0


【解决方案1】:
$RootFolder = 'C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test'
$CheckDate = [DateTime]::UtcNow.AddDays(-1)

Get-ChildItem -LiteralPath $RootFolder -Exclude @('*.txt') -Recurse:$true |
    Where-Object { $_.psIsContainer -eq $false } | # Check is file, not a directory
    Where-Object { $_.LastWriteTimeUtc -lt $CheckDate } | 
    Where-Object { $_.CreationTimeUtc -lt $CheckDate } |
    Where-Object { ( Test-Path -LiteralPath "$($_.FullName).txt" -PathType Any ) -ne $true } | # Check if there is no .txt file already
    ForEach-Object { Rename-Item -LiteralPath $_.FullName -NewName "$($_.Name).txt" }
  1. 检查 WriteTime 和 CreationTime。可能有文件时的情况 在写入后创建(当文件从另一个源复制时, WriteTime 被复制,CreationTime 不是)
  2. 检查是否可以重命名文件(目标名称不存在)
  3. 检查它是一个文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 2022-06-23
    • 2011-12-23
    • 2010-12-17
    • 1970-01-01
    • 2010-11-25
    • 2011-05-02
    相关资源
    最近更新 更多