2017-08-15 63 views
0

脚本的工作原理几乎如此,仍在重命名重复文件。我无法弄清楚如何得到它来命名文件,如使用powershell进行文件匹配

文件名(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` 
+0

您知道命名路径和变量交叉是令人讨厌的事实吗?我认为映射是没有必要的,最好使用匹配结构的正则表达式''^([AZ] {4})[ - ]([0-9] {3})(。*)$'' – LotPings

回答

1

您可以简单地使用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})','$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 
+0

谢谢vrdse!这像一个魅力。 但是,它也按文件扩展名排序。这对另一个我已经去过的项目很有用,但对于这个项目并没有那么多。 我在想我可以使用.basename来解决这个问题。思考? – Jimmy

+0

我是否在'.BaseName'的正确轨道上? 另外我注意到'.replace'没有完全按预期工作,因为这些目录都是以AAA-123格式命名的,但是这些文件不会被重命名。 – Jimmy

+0

你的意思是按文件扩展名排序? – vrdse

0
  1. 获取文件中源文件夹(GET-ChildItems)
  2. 重命名文件,包括一个 - 而不是 “”(重新命名 - 项目)
  3. 对子Name属性以新的名称($ File.Name)
  4. 创建源基于前4点字符
  5. 移到-项目新创建的文件夹(移动项目)新文件夹
$Source = "C:\Start" 
$Destinatoin = "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 $Destinatoin\$($File.Name.Substring(0,3)) 
} 
+0

@vrdse你的PoC环境一直很有用谢谢你。我工作的重命名重复,我想它遵循以下格式: 文件名(1).EXT 文件名(2).EXT等 但我有我发言时有些困难。我已更新我的原始帖子以反映当前的代码。 再次感谢您的全力协助。 – Jimmy

相关问题