写这么长是为了尽可能提高兼容性:- @echo off>nul 2>nul 3>nul 4>nul
- for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
- set a=
- (for /f "useback delims=" %%b in ("%%a") do (
- if defined a (echo;%%b) else (
- set a=%%b
-
- setlocal enabledelayedexpansion
- set a=!a:?=-!
- for %%c in (\ / : ^< ^> ^| ^") do if defined a set a=!a:%%c=-!
- for /f "tokens=1* delims=*" %%c in ("!a!") do (
- for /f "delims=" %%e in ("!a!") do (
- endlocal&set a=%%e
- if "%%d" neq "" (
- setlocal enabledelayedexpansion
- for /l %%a in (0 1 63) do (
- if "!a:~%%a,1!"=="*" (
- set "m=!m!-"
- ) else set "m=!m!!a:~%%a,1!"
- )
- ) else setlocal enabledelayedexpansion
- )
- )
- for /f "delims=" %%c in ("!a!") do endlocal&set a=%%c
- rem 替换特殊字符为-
-
- if not defined a echo;%%b
- )
- ))>$
- if not defined a set "a=%%~na"
- rem 获取文件名和输出文本
-
- set $=!a!#
- set len=0
- for %%a in (32 16 8 4 2 1) do (
- if !$:~%%a^,1!. NEQ . set/a len+=%%a&set $=!$:~%%a!
- )
- rem 计算文件名长度
-
- set n=
- setlocal enabledelayedexpansion
- for %%b in (!len!) do (
- for %%c in ("!a!*.tx$") do (
- for /f %%d in ("!n!") do endlocal&set n=%%~d
- set test=%%~nc
- setlocal enabledelayedexpansion
- rem 转换变量
-
- for /f "tokens=1-3 delims=0123456789" %%d in (
- "!test:~%%b!\"
- ) do (
- if %%f neq \ set /a n-=1
- )
- rem 排除文件名干扰
- )
- )
- move $ "!a!!n!.tx$"
- endlocal
- )
- del /f /a *.txt
- ren *.tx$ *.txt
- :: 取代原有的txt
- pause>con
复制代码 总结:批处理中的 * 和 ! 真蛋疼 |