0

我有几个目录,分别命名为“A”、“B”、“C”等。每个都有一些文件。我喜欢使用目录名加上每个目录中从 1 开始的索引号来重命名每个目录中的文件,左零填充到 3 的宽度。例如:子目录 A 有 3 个文件,它们将被重命名为:

A_001.dat
A_002.dat
A_003.dat

子目录 B 有 2 个文件,应重命名为:

B_001.dat
B_002.dat

等等。这些文件将被移动到主目录。我有以下批处理文件,但我似乎无法增加数字。请帮忙。

@echo off

set HomeFolder=%CD%
set OldExt=TXT
set NewExt=DAT

setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in ('dir *.%OldExt% /b /s') do (
    set i=1
    for /f "delims=" %%b in ("%%~dpa\.") do (
        set pad=00!i!
        set str=!pad:~-3!
        echo move /b "%%a" "%HomeFolder%\%%~nxb_!str!.%NewExt%"
        set /A i=!i!+1
)
)
endlocal
pause

而正确的答案是:

@echo off

set HomeFolder=%CD%
set OldExt=TXT
set NewExt=TIF
set i=1
set Folder=
set LastFolder=

setlocal ENABLEDELAYEDEXPANSION

for /f "delims=" %%a in ('dir *.%OldExt% /b /s') do (
    for /f "delims=" %%b in ("%%~dpa\.") do (
        set Folder=%%~nxb        
        if NOT !Folder!==!LastFolder! (set /A i=1)
        set LastFolder=!Folder!
        set pad=00!i!
        set str=!pad:~-3!
        copy /b "%%a" "%HomeFolder%\%%~nxb_!str!.%NewExt%"
        Set /A i+=1
    )
)

endlocal
4

3 回答 3

1
@echo off

:: By Elektro H@cker

Setlocal enabledelayedexpansion

set "OldExt=TXT"
set "NewExt=DAT"

FOR /R %%@ in (*%OldExt%) DO (

    REM Sets the directory of the file
    Set "Directory=%%~dp@"

    REM Cuts the directory name to obtain the last folder name
    Set "Directory=!Directory:~0,-1!"
    For /L %%X in (0,1,50) DO (Call Set "Directory=!Directory:*\=!")

    REM Check if this directory is the same of the last accesed directory to reset the counter or not
    Echo "!Directory!"|FINDSTR "^\"!LastDirectory!\"$" >NUL && (Set /A "Count+=1") || (Set /A "Count=1")

    REM Check if the number incrementation have 1-3 digits and copies the file
    Call Echo !COUNT!|FINDSTR "^[0-9]$"    >NUL && (Call Copy "%%@" ".\!Directory!_00!COUNT!.%NewExt%")
    Call Echo !COUNT!|FINDSTR "^[0-9].$"   >NUL && (Call Copy "%%@" ".\!Directory!_0!COUNT!.%NewExt%" )
    Call Echo !COUNT!|FINDSTR "^[0-9]..+$" >NUL && (Call Copy "%%@" ".\!Directory!_!COUNT!.%NewExt%"  )

    REM Sets the last accesed directory
    Call Set "LastDirectory=!Directory!"
)

Pause&exit

3个名为“A”“B”和“C”的子目录,每个子目录内有3个文件,输出结果为:

a_001.DAT
a_002.DAT
a_003.DAT
b_001.DAT
b_002.DAT
b_003.DAT
c_001.DAT
c_002.DAT
c_003.DAT
于 2012-11-30T09:25:44.340 回答
1

在循环或括号表达式中,您需要使用延迟扩展

设置 /a 变量=!变量!+1

但是您需要使用 setlocal ENABLEDELAYEDEXPANSION 激活此功能并使用匹配的 endlocal 重置它

尝试以下操作:

@echo off

set HomeFolder=%CD%
set OldExt=TXT
set NewExt=DAT

setlocal ENABLEDELAYEDEXPANSION

for /f "delims=" %%a in ('dir *.%OldExt% /b /s') do (
set i=1
for /f "delims=" %%b in ("%%~dpa\.") do (
    set pad=00%i%
    set str=%pad:~-3%
    echo move /b "%%a" "%HomeFolder%\%%~nxb_%str%.%NewExt%"
    set /A i+=1
)
)

endlocal

pause
于 2012-11-30T02:24:25.570 回答
0

这是任何想要做同样事情的人的工作脚本:

@echo off

set HomeFolder=%CD%
set OldExt=TXT
set NewExt=DAT
set i=1
set Folder=
set LastFolder=

setlocal ENABLEDELAYEDEXPANSION

for /f "delims=" %%a in ('dir *.%OldExt% /b /s') do (
    for /f "delims=" %%b in ("%%~dpa\.") do (
        set Folder=%%~nxb        
        if NOT !Folder!==!LastFolder! (set /A i=1)
        set LastFolder=!Folder!
        set pad=00!i!
        set str=!pad:~-3!
        copy /b "%%a" "%HomeFolder%\%%~nxb_!str!.%NewExt%"
        Set /A i+=1
    )
)

endlocal
于 2012-11-30T20:58:27.800 回答