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

时间:2018-07-11 12:41:51

标签: powershell filesystems

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

这是我到目前为止所拥有的:

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

您如何建议这样做?

1 个答案:

答案 0 :(得分:2)

这可以做到:

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