使用 powershell 重命名文件

File renaming with powershell

脚本几乎按预期运行,但仍在努力重命名重复文件。我不知道如何让它像

这样命名文件

filename(1).ext

filename(2).ext

我得到的最接近的是

filename(1).ext

filename(1)(2).ext

 #region actual script
 $srcRoot = "C:\srcLocation"
 $dstRoot = "C:\dstLocation"

 $fileList = Get-ChildItem -Path $srcRoot -File -Force -Recurse

    foreach ($file in $fileList) {


    $fileName = $file.Name.ToUpper()
    $fileExt = $file.Extension.ToUpper()
     $dstFileName = $null

     switch -Regex ($fileName)
    {
   '[A-Z]{4}-[0-9]{3}' { $dstFileName = $fileName }
   '[A-Z]{4} [0-9]{3}' { $dstFileName = $fileName -replace '([A-Z]{4})\s([0-
    9]{3})','-' }
   '[A-Z]{4}[0-9]{3}' { $dstFileName = $fileName -replace '([A-Z]{4})([0-9]
    {3})','-'}
   Default { Write-Warning -Message "$fileName is not an expected filename" 
   }
   }

   if ($dstFileName) {
   $dstDir = $dstFileName.Split('.')[0].Substring(0,8)
   $dstPath = Join-Path -Path $dstRoot -ChildPath $dstDir

   if (-not (Test-Path -Path $dstPath)) {
       New-Item -Path $dstPath -ItemType Directory
   }
   $i = 1
   if (test-path $dstPath$dstFileName){
           $dstFileName = $dstFileName.Split('.')[0] + "($i)" + $fileExt

   While (test-path $dstPath$dstFileName){
        $i +=1
        $dstFileName = $dstFileName -replace 

   }
   }


   Write-Verbose "Moving $($file.FullName)"
   Move-Item -Path $($file.FullName) -Destination $dstPath$dstFileName -
   ErrorAction Continue
   }
   }
   #endregion

您可以简单地使用 PowerShell 中字符串对象的 Replace 方法。要验证您的输入,您可以使用 RegEx。 Move-Item 将抛出错误,如果文件已经存在于目标中。完整的脚本如下所示。

#region setup
New-Item -Path C:\srcpath,C:\dstpath -ItemType Directory
Set-Location C:\srcpath
New-Item 'ABCD123.txt','ABCD 123.txt','AbCD-123.txt','AAAA111.txt','BBBB 222.jpg','BBBB-222.txt' -ItemType File
#endregion

#region actual script
$srcRoot = "C:\srcpath"
$dstRoot = "C:\dstpath"

$fileList = Get-ChildItem -Path $srcRoot -File -Force -Recurse

foreach ($file in $fileList) {

    $fileName = $file.Name.ToUpper()
    $dstFileName = $null

    switch -Regex ($fileName)
    {
        '[A-Z]{4}-[0-9]{3}' { $dstFileName = $fileName }
        '[A-Z]{4} [0-9]{3}' { $dstFileName = $fileName -replace '([A-Z]{4})\s([0-9]{3})','-' }
        '[A-Z]{4}[0-9]{3}' { $dstFileName = $fileName -replace '([A-Z]{4})([0-9]{3})','-'}
        Default { Write-Warning -Message "$fileName is not an expected filename" }
    }

    if ($dstFileName) {
        $dstDir = $dstFileName.Split('.')[0]
        $dstPath = Join-Path -Path $dstRoot -ChildPath $dstDir

        if (-not (Test-Path -Path $dstPath)) {
            New-Item -Path $dstPath -ItemType Directory
        }

        Write-Verbose "Moving $($file.FullName)"
        Move-Item -Path $($file.FullName) -Destination $dstPath$dstFileName -ErrorAction Continue
    }
}
#endregion

#region result
Write-Host '----- Result -----' -BackgroundColor DarkYellow
Get-ChildItem C:\dstpath -Recurse | Select-Object -ExpandProperty FullName
#endregion
  1. Gets the Files in Source folder (Get-ChildItems)
  2. Renames The File to include a - instead of a " " (Rename-Item)
  3. Sets the Child Name property to the new name ($File.Name)
  4. Creates new Folder in source based on first 4 Chars
  5. Moves-Item to new created folder (move-item)
$Source = "C:\Start"
$Destination = "C:\End"

foreach($File in (Get-ChildItem -Path $Source -File -Recurse)){
    Rename-Item $File.Fullname ($File.Name -replace " ", "-")
    $file.Name = ($File.Name -replace " ", "-")
    New-Item "$($Destination)$($File.Name.Substring(0,3))" -ItemType directory
    move-item $File.FullName -force -destination $Destination$($File.Name.Substring(0,3))
}