bat批处理语句,把多个文件夹里面的文件按名称排序后,以文件夹名为基础,递增命名

文件夹名类似这样:P002-0001,P004-0041....P100-1961

文件夹里按名称排序,第一个文件名字改为文件夹名,之后的文件名每个按顺序递增1
如P004-0041文件夹里面,文件依次改名为P004-0041,P004-0042,P004-0043,P004-0044,P004-0045.......

您这个需求真复杂,按照做工程的方式实现了,能否多给点分啊?(呵,开玩笑的)
把下面的代码存为my_dir_ren.bat,放在这些文件夹所在的文件夹里,运行就可以了。我已经在Win7下测试通过了。
@echo off
@setlocal enabledelayedexpansion
@for /f "delims=" %%i in ('dir /b /ad') do (

@cd %%i
@call :substr_before_char - %%i dir1
@call :substr_after_char - %%i dir2
@call :clr_bgn0s !dir2! n
@echo !dir1! !dir2! !n!
@for /f "delims=" %%j in ('dir /b /on') do (
@set rn=0000!n!
@set rn=!rn:~-4!
@echo !rn!
@ren "%%j" "!dir1!-!rn!.txt"
@set /a n=n+1
)
@cd..
)
@goto End0

:clr_bgn0s
@setlocal
@set str=%1

:Rep1
@set fc=%str:~0,1%
@if %fc%==0 (
@set str=%str:~1%
@goto Rep1
)
:Rtn1
@endlocal& set "%~2=%str%"
@goto :eof

:substr_after_char
@setlocal
@set c=%1

@set str=%2
@set tmp_str=%str%
:Rep2
@set get_c=%tmp_str:~0,1%
@if %get_c%==%c% (
@set tmp_str=%tmp_str:~1%
@goto Rtn2
)
@set tmp_str=%tmp_str:~1%
@if "%tmp_str%"=="" (
@goto Rtn2
) else (
@goto Rep2
)
:Rtn2
@set str=%tmp_str%
@endlocal& set "%~3=%str%"
@goto :eof

:substr_before_char
@setlocal
@set c=%1

@set str=%2
@set tmp_str=%str%
:Rep3
@set get_c=%tmp_str:~-1%
@if %get_c%==%c% (
@set tmp_str=%tmp_str:~0,-1%
@goto Rtn3
)
@set tmp_str=%tmp_str:~0,-1%
@if "%tmp_str%"=="" (
@set tmp_str=%str%
@goto Rtn3
) else (
@goto Rep3
)
:Rtn3
@set str=%tmp_str%
@endlocal& set "%~3=%str%"
@goto :eof

:End0
温馨提示:答案为网友推荐,仅供参考
相似回答