本帖最后由 pcl_test 于 2019-12-6 20:44 编辑
兼容处理名称带Unicode字符的文件和子文件夹、带点的文件夹或不带扩展名的文件同名问题- @echo off
- rem 指定目录路径
- set "folder=C:\Users\Administrator\Desktop\test"
-
- rem 0为转换文件名,1为转换文件夹名,2为同时转换文件名和文件夹名
- set f=2
-
- rem 0为不包括子文件夹,1为包括子文件夹
- set r=1
-
- rem 2为繁转简,4为简转繁
- set n=2
-
- if not exist "%folder%" (echo;"%folder%" not found&goto end)
- echo;"%f%"|>nul findstr "^\"[012]\"$"||(echo;参数错误&goto end)
- echo;"%r%"|>nul findstr "^\"[01]\"$"||(echo;参数错误&goto end)
- echo;"%n%"|>nul findstr "^\"[24]\"$"||(echo;参数错误&goto end)
- set locale=[System.Globalization.Cultureinfo]::CurrentCulture.LCID
- if "%folder:~-1%" equ "\" set "folder=%folder:~,-1%"
- if "%r%" equ "1" (set r=-recurse) else set "r="
- if "%f%" equ "0" (set "f=$_.PSIsContainer -eq $false") else (
- if "%f%" equ "1" (set "f=$_.PSIsContainer -eq $true") else set "f=1 -eq 1"
- )
-
- powershell -NoProfile -ExecutionPolicy bypass ^
- $cs='^
- using System;^
- using System.Runtime.InteropServices;^
- public static class ToConvert^
- {^
- [DllImport(\"kernel32\", CharSet = CharSet.Unicode, SetLastError = true)]^
- public static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, string lpDestStr, int cchDest);^
- }';^
- Add-Type -TypeDefinition $cs;^
- dir -LiteralPath '%folder%' %r%^|?{%f%}^|sort {$_.FullName.Length} -des^|%%{^
- $scr=$_.Name;$len=$scr.Length;$des=' '*$len;^
- [ToConvert]::LCMapString(%locale%, 0x0%n%000000, $scr, $len, $des, $len)^|out-null;^
- if($scr -ne $des){^
- $fd=[IO.Directory]::GetParent($_.FullName).FullName+'\';^
- $newname=$fd+$des;^
- $n=0;^
- while(Test-Path -LiteralPath $newname)^
- {^
- $n++;^
- if($_.PSIsContainer){^
- $newname=$fd+$des+'_'+$n;^
- }else{^
- if($des -match '(.+)(\.[^^\.]+$)'){$tmp=$matches[1]+'_'+$n+$matches[2]}else{$tmp=$des+'_'+$n}^
- $newname=$fd+$tmp;^
- }^
- }^
- mv -LiteralPath $_.FullName $newname;^
- if(Test-Path -LiteralPath $_.FullName){$ts='*转换失败,需手动重命名';}else{$ts=$newname.Split('\')[-1];}^
- write-host ($_.FullName.ToLower().replace('%folder%'.ToLower(),'')+' ==^> '+$ts);^
- }^
- }
- :end
- pause
复制代码
|