[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 pcl_test 于 2019-12-6 20:44 编辑

兼容处理名称带Unicode字符的文件和子文件夹、带点的文件夹或不带扩展名的文件同名问题
  1. @echo off
  2. rem 指定目录路径
  3. set "folder=C:\Users\Administrator\Desktop\test"
  4. rem 0为转换文件名,1为转换文件夹名,2为同时转换文件名和文件夹名
  5. set f=2
  6. rem 0为不包括子文件夹,1为包括子文件夹
  7. set r=1
  8. rem 2为繁转简,4为简转繁
  9. set n=2
  10. if not exist "%folder%" (echo;"%folder%" not found&goto end)
  11. echo;"%f%"|>nul findstr "^\"[012]\"$"||(echo;参数错误&goto end)
  12. echo;"%r%"|>nul findstr "^\"[01]\"$"||(echo;参数错误&goto end)
  13. echo;"%n%"|>nul findstr "^\"[24]\"$"||(echo;参数错误&goto end)
  14. set locale=[System.Globalization.Cultureinfo]::CurrentCulture.LCID
  15. if "%folder:~-1%" equ "\" set "folder=%folder:~,-1%"
  16. if "%r%" equ "1" (set r=-recurse) else set "r="
  17. if "%f%" equ "0" (set "f=$_.PSIsContainer -eq $false") else (
  18.     if "%f%" equ "1" (set "f=$_.PSIsContainer -eq $true") else set "f=1 -eq 1"
  19. )
  20. powershell -NoProfile -ExecutionPolicy bypass ^
  21.     $cs='^
  22.         using System;^
  23.         using System.Runtime.InteropServices;^
  24.         public static class ToConvert^
  25.         {^
  26.             [DllImport(\"kernel32\", CharSet = CharSet.Unicode, SetLastError = true)]^
  27.             public static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, string lpDestStr, int cchDest);^
  28.         }';^
  29.     Add-Type -TypeDefinition $cs;^
  30.     dir -LiteralPath '%folder%' %r%^|?{%f%}^|sort {$_.FullName.Length} -des^|%%{^
  31.         $scr=$_.Name;$len=$scr.Length;$des=' '*$len;^
  32.         [ToConvert]::LCMapString(%locale%, 0x0%n%000000, $scr, $len, $des, $len)^|out-null;^
  33.         if($scr -ne $des){^
  34.             $fd=[IO.Directory]::GetParent($_.FullName).FullName+'\';^
  35.             $newname=$fd+$des;^
  36.             $n=0;^
  37.             while(Test-Path -LiteralPath $newname)^
  38.             {^
  39.                 $n++;^
  40.                 if($_.PSIsContainer){^
  41.                     $newname=$fd+$des+'_'+$n;^
  42.                 }else{^
  43.                     if($des -match '(.+)(\.[^^\.]+$)'){$tmp=$matches[1]+'_'+$n+$matches[2]}else{$tmp=$des+'_'+$n}^
  44.                     $newname=$fd+$tmp;^
  45.                 }^
  46.             }^
  47.             mv -LiteralPath $_.FullName $newname;^
  48.             if(Test-Path -LiteralPath $_.FullName){$ts='*转换失败,需手动重命名';}else{$ts=$newname.Split('\')[-1];}^
  49.             write-host ($_.FullName.ToLower().replace('%folder%'.ToLower(),'')+' ==^> '+$ts);^
  50.         }^
  51.     }
  52. :end
  53. pause
复制代码
2

评分人数

TOP

返回列表