win7 以上系统,若有错误则先升级系统的POWERSHELL版本,
删掉程序中 每行前的 # 号,则会把子文件名添加到主文件夹后面,用 _ 号连接, 如:JC00001_身份证_土地证_户口本
子文件夹按顺序读取,同名自动加 (数字); 如:a(1).jpg, a(2).jpg;
用法:保存为:XXXX.ps1,ANSI编码,右键运行,不要用编辑器运行(除非你知道运行环境)。
XP 系统到微软官网下载 XP 版 POWERSHELL。但未测试XP版的语法是否完全兼容。- function New-FileName {
- param([string]$dir, [string]$baseName, [string]$extension, [int]$n=1);
- $targetName=[IO.Path]::Combine($dir, $baseName + $extension);
- if (![IO.File]::Exists($targetName)) {
- return $targetName;
- }
- else {
- $baseName=($baseName -replace '\s*\(\d+\)\s*$') + '(' + $n++ + ')';
- return New-FileName -dir $dir -baseName $baseName -extension $extension -n $n;
- }
- }
-
- (ls|?{$_.PsIsContainer}).Foreach{
- $folderPath=$_.FullName;
- $childFolders=ls $folderPath -r|?{$_.PsIsContainer}|sort @{e={$_.FullName.Length}; Ascending=$true};
- # $folderName=$_.Name;
- foreach ($childFolder in $childFolders) {
- # $folderName += '_' + $childFolder.Name;
- (ls $childFolder.FullName|?{!$_.PsIsContainer}).Foreach{
- $baseName=[IO.Path]::GetFileNameWithoutExtension($_.Name);
- $extension=[IO.Path]::GetExtension($_.Name);
- $newFile=New-FileName -dir $folderPath -baseName $baseName -extension $extension;
- Move-Item $_.FullName -dest $newFile -Verbose -Force;
- }
- }
- [IO.Directory]::Delete($childFolders[0].FullName, $true);
- # Rename-Item $folderPath -NewName $folderName -Force;
- }
- &cmd /c pause
复制代码
|