- Function GenerateNewName{
- Param( [System.IO.FileInfo]$FilePath )
- $int = 0
- if($pool.ContainsKey($FilePath.BaseName)){
- $FilePath = "{0}\{1}{2}" -f ($FilePath.DirectoryName,$pool[$FilePath.BaseName],$FilePath.Extension)
- }
- while(test-Path $FilePath.FullName){
- $int ++
- $FilePath = "{0}\{1}{2:D3}{3}" -f ($FilePath.DirectoryName,(([char[]]$FilePath.BaseName)[0..39] -join ""),$int,$FilePath.Extension)
- }
- $pool[$FilePath.BaseName]=$FilePath.BaseName
-
- Return $FilePath.FullName
- }
- $pool = @{}
- dir C:\ceshi -Recurse | %{if ($_ -is [System.IO.FileInfo] -and ($_.BaseName).Length -gt 40){
- Move-Item $_.FullName (GenerateNewName($_)) -Verbose
- }}
复制代码
|