Batch file 批处理:重命名扩展名并用字母递增

Batch file 批处理:重命名扩展名并用字母递增,batch-file,batch-processing,Batch File,Batch Processing,需要知道如何重命名文件扩展名 例如: -testAA_1.csv -testBB_2.csv -testCC_3.csv last file changed was testDD_14.csv to testDD.cc1 致: 写入.csv文件的程序将不断添加到目录中,因此我需要能够搜索最后一个递增扩展名的目录并从那里开始 例如: -testAA_1.csv -testBB_2.csv -testCC_3.csv last file changed was testDD_14.csv to

需要知道如何重命名文件扩展名

例如:

-testAA_1.csv
-testBB_2.csv
-testCC_3.csv
last file changed was testDD_14.csv to testDD.cc1
致:

写入.csv文件的程序将不断添加到目录中,因此我需要能够搜索最后一个递增扩展名的目录并从那里开始

例如:

-testAA_1.csv
-testBB_2.csv
-testCC_3.csv
last file changed was testDD_14.csv to testDD.cc1
需要再次运行批处理,并使最新的文件以.cd1文件扩展名开始

@echo off
setlocal EnableDelayedExpansion

rem Create nextLetter array used to increment letters
set "letter=z"
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
   set "nextLetter[!letter!]=%%a"
   set "letter=%%a"
)

rem Initialize extension for the first time
set "lastExt=.zz1"
rem Search directory for the last incremented extension, if any
if exist -test*.??1 for /F %%a in ('dir /B /O-E -test*.??1') do set "lastExt=%%~Xa" & goto continue
:continue
rem Get first and second letters from last extension
set "first=%lastExt:~1,1%"
set "second=%lastExt:~2,1%"

rem Process original files
for /F "tokens=1* delims=_" %%a in ('dir /B -test*.csv') do (
   rem Increment extension
   for %%c in (!second!) do set "second=!nextLetter[%%c]!"
   if "!second!" equ "a" for %%c in (!first!) do set "first=!nextLetter[%%c]!"
   rem Rename this file
   ren %%a_%%b %%a.!first!!second!1
)
这是一个会话示例输出:

C:\ dir /B /OE
test.bat
-testAA_1.csv
-testCC_3.csv
-testBB_2.csv
-testCC_3.OK
-testBB_2.OK
-testAA_1.OK

C:\ test.bat

C:\ dir /B /OE
-testAA.aa1
-testBB.ab1
-testCC.ac1
test.bat
-testCC_3.OK
-testBB_2.OK
-testAA_1.OK

C:\ copy *.OK *.csv
-testAA_1.OK
-testBB_2.OK
-testCC_3.OK
        3 archivo(s) copiado(s).

C:\ test.bat

C:\ dir /B /OE
-testAA.aa1
-testBB.ab1
-testCC.ac1
-testAA.ad1
-testBB.ae1
-testCC.af1
test.bat
-testCC_3.OK
-testAA_1.OK
-testBB_2.OK
@ECHO关闭
SETLOCAL enabledelayedexpansion
设置“tebahpla=z y x w v u t s r q p o n m l k j i h g f e d c b a”
设置“sourcedir=U:\sourcedir\t w o”
对于/f“tokens=1*delims=\%%a IN(
“dir/b/a-d”%sourcedir%\*.\u*.csv“
)做(
对于%%x IN(%tebahpla%)执行,对于%%y IN(%tebahpla%)执行(
如果不存在“%sourcedir%\test*%%x%%y1”则设置“newext=%%x%%y1”
)
REN“%sourcedir%\%%a\%%b”“%%a.!newext!”
)
目录“%sourcedir%\*.*1”
后藤:EOF

您需要更改
sourcedir
的设置以适应您的情况。

阅读
HELP REN
HELP FOR
,然后在
FOR
循环中尝试
REN
。如果您发现此答案有用,您可以选择它作为最佳答案,并在您有足够的代表点数时进行投票。。。