如何使用 Powershell 增加文件的副本?

How to increment copies of a file using Powershell?

我必须每小时将文件从一个文件夹移动到另一个文件夹。

如果目标文件夹中有文件副本,我希望将其命名为 FileName_1。在接下来的一个小时内,如果原始文件再次从源中复制,则希望将其命名为 FileName_2。如果它仍然存在则 FileName_3,然后 FileName_4 依此类推。

目前,我只能到达 FileName_1。问题是因为我更改了文件名并将其替换为新文件名,所以它覆盖了 FileName_1.

等一下。我在这里做了功课。我有一个代码块,它将按名称对文件进行排序,拆分名称,并将文件计数器加 1,但我没有到达代码的这一部分,因为初始检查(如果文件存在)在制作了第一个副本。

感谢任何帮助。

代码:

#Sources 
$source = "C:\Users\Desktop\TEST\Test_1\*"
$sourceNameChange = "C:\Users\Desktop\TEST\Test_1"

#Destination 
$destination = "C:\Users\Desktop\TEST\Test_2"

Function MoveFiles{
    Param(
        [string]$src,
        [string]$dest,
        [string]$srcNameChange
    )
   Get-ChildItem -Force -Recurse $src -ErrorAction Stop -ErrorVariable SearchError | ForEach-Object{
     
        $fileNameSrc = $_.Name
        $baseNameSrc = $_.BaseName
        # Check for duplicate files
        $testPath = Test-Path -Path $dest$baseNameSrc.*
        If( $testPath )
        {
        $fileCounter = 1
        $newName = $_.BaseName + "_"+$fileCounter + $_.Extension
        "$srcNameChange$fileNameSrc" | Rename-Item -NewName $newName
        }  
        $testPathDest = Test-Path -Path $dest$baseNameSrc_.*
       If($testPathDest){            
           $sort = Get-Item -Path $dest\* -Filter *$baseNameSrc_* | Sort-Object -Property Name -Descending 
           $destFileName = $sort[1].BaseName
           $destFileCounter = $destFileName.Split("_")[1]
           $destNewName = $_.BaseName + "_"+($destFileCounter+1) + $_.Extension
           "$srcNameChange$fileNameSrc" | Rename-Item -NewName $destNewName
       }
    }
    Move-Item -Path $src  -Destination $dest -Force
} 
MoveFiles -src $source -dest $destination -srcNameChange $sourceNameChange

我添加了很多内联注释来帮助您理解函数的逻辑,基本上,您的代码缺少的是一个循环,该循环增加您正在移动的文件的索引,直到它不存在。

注意,请确保 $destination 不是包含在 $source 中的文件夹,否则您会因使用 -Recurse.

function Move-Files {
    [cmdletbinding()]
    param(
        [parameter(Mandatory, ValueFromPipeline)]
        [string] $Source,
        [parameter(Mandatory)]
        [string] $Destination
    )
    begin {
        # if destination doesn't exist, create the folder
        if(-not (Test-Path $destination)) {
            $null = New-Item -Path $destination -ItemType Directory
        }
    }
    process {
        # get all the files in `$source` and iterate over them
        Get-ChildItem $source -File -Recurse -Force | ForEach-Object {
            # check if a file with the same name exists in `$destination`
            $thisFile = Join-Path $destination -ChildPath $_.Name
            # if it does exist
            if(Test-Path $thisFile) {
                $i = 0
                # start a loop, using `$i` as index
                do {
                    # increase `$i` on each iteration and get a new name
                    $newName = $_.BaseName + "_" + ++$i + $_.Extension
                    # join the destination path with the new name
                    $thisFile = Join-Path $destination -ChildPath $newName
                    # do this while `$thisFile` exists on `$destination`
                } while(Test-Path $thisFile)
            }
            # if we are here we can assume that either the file didn't exist
            # on `$destination` OR the `do` loop got us a new name
            Move-Item -LiteralPath $_.FullName -Destination $thisFile
        }
    }
}

$source = 'C:\Users\Desktop\TEST\Test_1'
$destination = 'C:\Users\Desktop\TEST\Test_2'
Move-Files -Source $source -Destination $destination