使用子目录名重命名子目录中的文件,并为每个子目录重新启动增量编号

时间:2012-11-30 02:03:03

标签: batch-file rename

我有几个目录,名为“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

3 个答案:

答案 0 :(得分:1)

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

设置/变量=!变量!+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

答案 1 :(得分: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

答案 2 :(得分: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