用powershell文件通配文件

时间:2017-08-15 16:07:37

标签: powershell glob organizer rename-item-cmdlet

脚本几乎正在按预期工作,仍在努力重命名重复文件。我无法弄清楚如何命名像

这样的文件
  

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

  
      
  1. 获取Source文件夹中的文件(Get-ChildItems)
  2.   
  3. 重命名文件以包含 - 而不是“”(重命名项目)
  4.   
  5. 将Child Name属性设置为新名称($ File.Name)
  6.   
  7. 根据前4个字符
  8. 在源代码中创建新文件夹   
  9. 将项目移动到新创建的文件夹(move-item)
  10.   
$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))
}