将自动编号前​​缀添加到目录下的所有文件中

时间:2018-06-07 07:45:43

标签: windows batch-file

您如何使用.bat脚本为目录中的所有文件(也在子文件夹中)添加前缀?

示例:

  

输入

fileNameABC, fileNameDEF, fileNameHIJ...

  

输出

001-fileNameABC, 002-fileNameDEF, 003-fileNameHIJ.....

非常感谢。

1 个答案:

答案 0 :(得分:0)

请注意,此脚本有一些限制,例如仅设计为001到999之间的数字(您必须在多个位置进行修改以扩展范围),以及它检测前缀是否已经有限的方式已被添加。但这应该让你开始......

@ECHO OFF
IF %1.==. ECHO Source path not provided & PAUSE & EXIT/B
IF NOT EXIST %1 ECHO Source directory does not exist & PAUSE  & EXIT/B
SETLOCAL
REM This script only supports numerical prefixes in the range 001 to 999
SET P=1000
FOR /R %1 %%I IN (.) DO CALL:0 "%%I"
PAUSE
EXIT/B
:0
REM Uncomment below to restart numbering from 1 for each subfolder
::SET P=1000
FOR %%I IN (%1\*) DO CALL:1 "%%I" || EXIT/B
EXIT/B
:1
IF %P%==1999 ECHO Overflow: Too many files found (More than 999) & EXIT/B 1
REM Additional test to see if prefix may have been added already. This test is not very accurate
SET T=%~N1
CALL:2 %T:~0,1% || CALL:2 %T:~1,1% || CALL:2 %T:~2,1% || IF %T:~3,1%==- EXIT/B 0
REM END: Additional test
SET/A P=P+1
REN %1 "%P:~1%-%~NX1"
EXIT/B 0
:2
REM Make ERRORLEVEL 0, else 1 if %1 is digit
CD.
FOR /L %%I IN (0,1,9) DO IF %1==%%I EXIT/B 1