使用 PowerShell 移动文件时检测重复文件并重命名

Detect duplicate file and rename when moving file with PowerShell

我正在创建一个函数,允许我们使用 Move-ItemRename-Item 将文件从一个目录移动到另一个目录。如果目录中已经存在文件,我需要重命名它。例如,如果test.txt已经存在,我需要将其重命名为test(1).txt,如果(1)已经存在,则需要重命名为test(2).txt,等等。

这是我目前的情况:

Function Move-PTIFile
{
    param($Origin, $Destination, [Boolean]$CreateDirectory)

    # see if the destination exists
    if (Test-Path $Destination)
    {
        # get the file name
        $FileName = $Origin.Split("\") | Select-Object -Last 1

        # check if the file exists within the destination
        if (Test-Path $FileName){
            Rename-Item $Origin -NewName "$FileName.txt"
        }
        else{

        }

        Move-Item $Origin $Destination
        Write-Output "File Moved"
    }
    else
    {   
        # create the folder at the destination path if the nerd indicates that they want the directory to be created
        if ($CreateDirectory -eq $true){
            Mkdir $Destination
            Move-Item $Origin $Destination
            Write-Output "Created File in $Destination"
        }
        else{
            Write-Error "Folder/Directory doesn't exist.`nPlease create folder or flag CreateDirectory parameter as true."
        }
    }
}

############## TESTING ##############
$Origin = "C:\MyFiles\Temp\Test.txt"
$Destination = "C:\MyFiles\Temp\Movedd\Test\Testing\Testttt"

Move-PTIFile $Origin $Destination $false

您建议如何进行此操作?

这样就可以了:

Function Move-PTIFile {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Origin, 

        [Parameter(Mandatory = $true, Position = 1)]
        [string]$Destination, 

        [switch]$CreateDirectory
    )

    # see if the destination exists
    if (!(Test-Path $Destination -PathType Container)) {
        if (!($CreateDirectory)) {
            Write-Error "Folder '$Destination' does not exist.`r`nPlease create folder or flag CreateDirectory parameter as true."
            return
        }
        Write-Verbose -Message "Creating folder '$Destination'"
        New-Item -Path $Destination -ItemType Directory -Force | Out-Null
    }

    # check if a file with that name already exists within the destination
    $fileName = Join-Path $Destination ([System.IO.Path]::GetFileName($Origin))
    if (Test-Path $fileName -PathType Leaf){
        $baseName  = [System.IO.Path]::GetFileNameWithoutExtension($Origin)
        $extension = [System.IO.Path]::GetExtension($Origin)                # this includes the dot
        $allFiles  = Get-ChildItem $Destination | Where-Object {$_.PSIsContainer -eq $false} | Foreach-Object {$_.Name}
        $newName = $baseName + $extension
        $count = 1
        while ($allFiles -contains $newName) {
            $newName = [string]::Format("{0}({1}){2}", $baseName, $count.ToString(), $extension)
            $count++
        }
        # rename the file already in the destination folder
        Write-Verbose -Message "Renaming file '$fileName' to '$newName'"
        Rename-Item $fileName -NewName $newName
    }

    Write-Verbose -Message "Moving file '$Origin' to folder '$Destination'"
    Move-Item $Origin $Destination
}


############## TESTING ##############
$Origin = "C:\MyFiles\Temp\Test.txt"
$Destination = "C:\MyFiles\Temp\Movedd\Test\Testing\Testttt"

Move-PTIFile $Origin $Destination -Verbose