移动项检查文件是否存在

时间:2019-10-15 13:22:48

标签: powershell powershell-3.0

我有一个脚本,该脚本将文件存储在一个上载文件夹中,并将其向上移动一个目录。当脚本在根目录中遇到重复文件(上传文件移至该文件)时,该脚本将停止。我如何让脚本检查文件是否存在,如果文件名重复,如何重命名并保留两个副本?

脚本遇到重复的文件名时就会停止

$path = 'C:\Usr\*\*upload*'
Get-ChildItem -Directory -Path $path |
    ForEach-Object {
        Get-ChildItem -File -Path $_.FullName |
            ForEach-Object {
                Move-Item -Path $_.FullName -Destination $(Split-Path -Parent $_.PSParentPath) -WhatIf
    }

我仍然无法使它正常工作。有什么想法吗?

$num=1
Start-Transcript -path C:\Scripts\output.txt -append
Get-ChildItem -Directory -Path 'C:\Usrs\*\*upload*' |
    ForEach-Object {
        Get-ChildItem -File -Path $_.FullName |
            ForEach-Object {
            $nextName = $_.FullName
            while(Test-Path -Path $nextName)
            {
            $nextName = Join-Path $_.FullName ($_.BaseName + "_$num" + $_.Extension)
            $num+=1
            }
                Move-Item -Path $_.FullName -Destination $(Split-Path -Parent $_.PSParentPath)
            }
    }
Stop-Transcript

0 个答案:

没有答案