如何在目录中的所有* .txt文件中插入前导零以获取具有相同文件名长度的所有文件?

时间:2015-12-23 14:13:02

标签: batch-file

我正在尝试使用批处理文件单步执行文件夹。该文件夹的文件名为

  • 的1.txt
  • 2.txt
  • 10.txt
  • 100.txt

所以我想将它们重命名为

  • 001.txt
  • 002.txt
  • 010.txt
  • 100.txt

我试过了

FOR /R C:\Test\ %%G IN (*.txt) DO echo "%%G"

但这给了我一个输出

1.txt
10.txt
100.txt
2.txt

如何按顺序输出?

2 个答案:

答案 0 :(得分:0)

以下是此任务的注释批处理代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem The environment variable LeadingZeros will be a string containing as
rem much zeros as longest file name has characters without file extension.

set "LeadingZeros="

rem The environment variable ZerosCount will have the number of zeros.

set "ZerosCount=0"

rem Search in current directory for files with extension TXT and call for
rem each file name without file extension the subroutine GetLeadingZeros.

for %%I in (*.txt) do call :GetLeadingZeros "%%~nI"

rem Is there no file with more than 1 character, there
rem is nothing to do and therefore exit this batch file.

if %ZerosCount% LEQ 1 endlocal & goto :EOF

rem Otherwise insert at beginning of each file name the string with
rem the leading zeros and rename the file with using only the last
rem ZerosCount characters from file name with leading zeros.

for %%I in (*.txt) do (
    set "FileName=%LeadingZeros%%%~nI"
    ren "%%~fI" "!FileName:~-%ZerosCount%!%%~xI"
)

rem Exit the batch file after renaming all files.

endlocal
goto :EOF

rem This subroutine determines length of current file name and at
rem the same time builds a string with just zeros of same length.

rem Once the file name length and number of zeros for this file is
rem determined, this number is compared with the greatest length already
rem determined before. If this file has a longer file name than all other
rem files before, this file name specifies the number of zeros to insert
rem at begin of each file name in the second loop above.

:GetLeadingZeros
set "TempZeros="
set "TempCount=0"
set "FileName=%~1"

:NextChar
if "%FileName%" == "" goto CompareLengths
set "TempZeros=%TempZeros%0"
set "FileName=%FileName:~1%"
set /A TempCount+=1
goto NextChar

:CompareLengths
if %TempCount% LEQ %ZerosCount% goto :EOF
set "LeadingZeros=%TempZeros%"
set "ZerosCount=%TempCount%"
goto :EOF

如果数字位数固定,则比实际需要更复杂。但是编写一个固定数字为3的批处理文件对我来说并不是很有趣,因为它太简单了,看看这个 FOR 循环。

@echo off
for %%I in (*.txt) do (
    set "FileName=00%%~nI"
    ren "%%~fI" "!FileName:~-3!%%~xI"
)

第一批代码在当前目录中找出最长的文件名,并根据此文件名确定要在每个文件名的开头插入的前导零的数量,以最终获得具有相同文件名长度的所有文件。

编辑:此批处理代码比第一批代码快一点,但也是如此:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem The environment variable ZerosCount will have the number of zeros
rem which is equal the number of characters of longest file name.

set "ZerosCount=0"

rem Search in current directory for files with extension TXT and call for
rem each file name without file extension the subroutine GetLeadingZeros.

for %%I in (*.txt) do call :GetLeadingZeros "%%~nI"

rem Is there no file with more than 1 character, there
rem is nothing to do and therefore exit this batch file.

if %ZerosCount% LEQ 1 endlocal & goto :EOF

rem The environment variable LeadingZeros will be a string containing as
rem much zeros as longest file name has characters without file extension.

set "LeadingZeros="
for /L %%N in (1,1,%ZerosCount%) do set "LeadingZeros=!LeadingZeros!0"

rem Otherwise insert at beginning of each file name the string with
rem the leading zeros and rename the file with using only the last
rem ZerosCount characters from file name with leading zeros.

for %%I in (*.txt) do (
    set "FileName=%LeadingZeros%%%~nI"
    echo ren "%%~fI" "!FileName:~-%ZerosCount%!%%~xI"
)

rem Exit the batch file after renaming all files.

endlocal
goto :EOF

rem This subroutine determines length of current file name.

rem Once the file name length is determined, this number is compared with the
rem greatest length already determined before. If this file has a longer file
rem name than all other files before, this file name specifies the number of
rem zeros to insert at begin of each file name in the third loop above.

:GetLeadingZeros
set "NameLength=0"
set "FileName=%~1"

:NextChar
if "%FileName%" == "" goto CompareLengths
set "FileName=%FileName:~1%"
set /A NameLength+=1
goto NextChar

:CompareLengths
if %NameLength% LEQ %ZerosCount% goto :EOF
set "ZerosCount=%NameLength%"
goto :EOF

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • ren /?
  • set /?
  • setlocal /?
PS:我从来没有为自己编写用于重命名文件的批处理文件,因为我使用Total Commander内置multi-rename tool制作文件/文件夹重命名始终是一项非常简单的任务,无需编码技能。

答案 1 :(得分:0)

强大的解决方案应具备以下功能:

  • 只应重命名基本名称为纯数字的.txt文件
  • 如果基本名称长度已超出所需宽度,则应忽略(不截断)

以下解决方案符合上述标准。我选择填充到3位数。扩展宽度应该是一个直接的练习

Pure Batch

@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%F in (
  'dir /b /a-d *.txt^|findstr /i "^[0-9][0-9]*\.txt$"'
) do (
  set "old=%%F"
  set "new=00%%F"
  if !old!==!old:~-6! echo ren !old! !new:~-7!
)

JREN.BAT - 混合JScript /批量重命名实用程序

JREREN.BAT是一个通用的正则表达式重命名实用程序。它是纯脚本,可​​以在任何Windows机器上从XP开始本地运行。如果您需要分页帮助,可以运行jren /?jren /??来获取完整文档。

jren "^\d+\.txt$" "lpad($0,'0000000')" /i /j

请记住,JREN是批处理脚本,因此如果将命令放在另一个批处理脚本中,则必须使用call jren