需要知道如何重命名文件扩展名
例如:
-testAA_1.csv
-testBB_2.csv
-testCC_3.csv
为:
-testAA.aa1
-testBB.ab1
-testCC.ac1 (all the way to .zz1)
编写.csv文件的程序将不断添加到目录中,因此我需要能够在搜索目录中找到最后一个增量扩展并从那里开始。
例如:
last file changed was testDD_14.csv to testDD.cc1
需要再次运行批处理并使最新文件以文件扩展名.cd1
开头答案 0 :(得分:0)
@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
答案 1 :(得分:0)
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "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"
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "tokens=1* delims=_" %%a IN (
'dir /b /a-d "%sourcedir%\*_*.csv" '
) DO (
FOR %%x IN (%tebahpla%) DO FOR %%y IN (%tebahpla%) DO (
IF NOT EXIST "%sourcedir%\test*.%%x%%y1" SET "newext=%%x%%y1"
)
REN "%sourcedir%\%%a_%%b" "%%a.!newext!"
)
DIR "%sourcedir%\*.*1"
GOTO :EOF
您需要更改sourcedir
的设置以适合您的具体情况。