代码之家  ›  专栏  ›  技术社区  ›  Matt

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

  •  0
  • Matt  · 技术社区  · 6 年前

    我正在创建一个函数,它允许我们利用 Move-Item Rename-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 回复  |  直到 6 年前
        1
  •  2
  •   Theo    6 年前

    这样可以做到:

    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