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

马特

我正在创建一个函数,它允许我们使用Move-Item将文件从一个目录移动到另一个目录Rename-Item如果目录中已经存在文件,我需要重命名它。例如,如果test.txt已经存在,我需要将其重命名为test(1).txttest(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

你会如何建议去做这件事?

根据

这样做:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Ruby在ftp站点上移动并重命名文件

来自分类Dev

使用git移动并重命名多个文件

来自分类Dev

使用Ruby在ftp站点上移动并重命名文件

来自分类Dev

使用git移动并重命名多个文件

来自分类Dev

外壳移动文件并重命名它们

来自分类Dev

创建重复文件并重命名

来自分类Dev

在使用 Powershell 移动文件之前重命名文件

来自分类Dev

使用JavaCode将文件移动到nas并重命名我的文件

来自分类Dev

在symfony2中移动并重命名文件

来自分类Dev

复制文件并重命名

来自分类Dev

复制并重命名文件

来自分类Dev

Powershell递归文件名读取并重命名

来自分类Dev

如果新文件夹中已存在文件,请使用空格移动文件并重命名

来自分类Dev

使用FtpWebRequest移动/重命名文件

来自分类Dev

Powershell - 如何在移动文件后重命名文件?

来自分类Dev

PowerShell重命名文件

来自分类Dev

使用vbs复制并重命名最旧的文件

来自分类Dev

使用循环创建副本并重命名文件

来自分类Dev

发现重复文件时,在Camel中重命名文件

来自分类Dev

从服务器收到文件后,如何在C#中移动文件并重命名?

来自分类Dev

从服务器收到文件后,如何在C#中移动文件并重命名?

来自分类Dev

使用Powershell递归重命名文件

来自分类Dev

当目标文件已经存在时,如何使用WinSCP .NET程序集移动/重命名文件?

来自分类Dev

当目标文件已经存在时,如何使用WinSCP .NET程序集移动/重命名文件?

来自分类Dev

Gulp-复制并重命名文件

来自分类Dev

上载多个文件并重命名-PHP

来自分类Dev

复制文件并重命名-Gradle

来自分类Dev

剥离文件名并重命名

来自分类Dev

(PHP) 解压并重命名 CSV 文件?

Related 相关文章

热门标签

归档