1

脚本几乎按照预期工作,仍在为重命名重复文件而苦苦挣扎。我不知道如何让它命名文件

文件名(1).ext

文件名(2).ext

我得到的最接近的是

文件名(1).ext

文件名(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})','$1-$2' }
   '[A-Z]{4}[0-9]{3}' { $dstFileName = $fileName -replace '([A-Z]{4})([0-9]
    {3})','$1-$2'}
   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
4

2 回答 2

1

您可以简单地Replace在 PowerShell 中使用字符串对象的方法。要验证您的输入,您可以使用 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})','$1-$2' }
        '[A-Z]{4}[0-9]{3}' { $dstFileName = $fileName -replace '([A-Z]{4})([0-9]{3})','$1-$2'}
        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
于 2017-08-15T16:52:34.937 回答
0
  1. 获取源文件夹中的文件 (Get-ChildItems)
  2. 重命名文件以包含 - 而不是“”(重命名项)
  3. 将 Child Name 属性设置为新名称 ($File.Name)
  4. 根据前 4 个字符在源中创建新文件夹
  5. 将项目移动到新创建的文件夹(移动项目)
$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))
}
于 2017-08-15T18:21:49.730 回答