批处理文件,用于重命名文件夹中的文件,然后重新命名

时间:2012-06-19 09:19:55

标签: batch-file batch-processing

我担心的一个稍微过时的问题但是这里有:

我有一个程序可以按顺序生成一些.RAW文件,例如。

Example_1.RAW
Example_2.RAW

然后根据需要在数字上添加额外的有效数字,例如

Example_10.RAW
Example_200.RAW

我需要将这些文件名转换为数字,以便通过批处理器运行,然后生成更多文件作为输出。我编写了一个批处理文件,用于重命名并将旧文件名和新文件名存储在文本文件中,我的代码是:

@echo off
set i=1
echo Running Batch Rename
for %%f in (*.RAW) do (set file=%%f) & CALL :rename

:rename
echo %i% >>Names.txt
echo %file% >>Names.txt
echo. >>Names.txt
ren %file% %i%.RAW
set /A i+=1

然后将我的所有.RAW重命名为1.RAW,2.RAW等,并使用旧的和新的文件名创建Names.txt。由于DOS处理数字的方式,这意味着我得到一个稍微不稳定的编号过程,即它将处理Ex_1,Ex_10,Ex_100,Ex_101为1,2,3,4,这将导致后期处理中的更多工作这些结果是为了在正确的地方得到正确的结果。

是否可以编写另一个带有Names.txt的批处理文件并反转输出文件的进程?所以它需要一个带有1.raw,1.something,1.something的文件夹,参考Names.txt并将它们重命名为Example_1.raw等?

3 个答案:

答案 0 :(得分:2)

这应该做的工作

@echo off
set cnt=0
del Names.txt > nul 2>&1
echo Running Batch Rename
for %%f in (*.RAW) do (
    set "file=%%f"
    CALL :renameToNumber
)
echo .. Do the jobs ...
rem ** As sample: copy the file
copy *.raw *.some
call :renameBack
exit /b

:renameToNumber
set /A cnt+=1
set "number=00000%cnt%"
set "number=%number:~-4%"
(echo %number% %file%) >> Names.txt
ren "%file%" %number%.RAW
exit /b

:renameBack
for /F "tokens=1,*" %%A in (names.txt) DO (
   set "number=%%A"
   set "filename=%%~nB"
   call ren %%number%%.* "%%filename%%_%%number%%.*"
   call echo ren %%number%%.* "%%filename%%_%%number%%.*"
)
exit /b

答案 1 :(得分:2)

我能够调整上面的代码,在现有的批处理例程中运行,通过测试目录中的最后一个文件 - 使用'if errorlevel' - 以便在到达最后一个文件后例程没有退出但是而是回到了我的日常生活。

    ::  *** Renumber .JPG files ***

    setlocal EnableDelayedExpansion
    set dir=C:%homepath%\Desktop\Temp

    :: Create variable and set to zero
    set cnt=0

    :: For each file in specified class
    for %%f in (%dir%\*.jpg) do (
        :: Get name of file 
        set "file=%%f"
        :: Run specified Subroutine
        CALL :renameToNumber
    )

    :renameToNumber
    :: Increment variable by 1
    set /A cnt+=1
    :: Preceed value of variable with 00000
    set "number=00000%cnt%"
    :: Delete all but final 2 digits
    set "number=%number:~-2%"
    :: Store new and original name of file
    (echo %number% %file%) >> Names.txt
    :: Rename file
    ren "%file%" %number%.jpg
    :: Quit Subroutine if no further files
    if errorlevel==1 goto :Continue
    :: Loop back to start of Subroutine
    exit /b

    :Continue

::  *** Zip the image files into an RAR file ***
    SET rar=C:\Program Files (x86)\WinRAR\RAR.exe
    "%rar%" a -ep -m0 temp.rar "%dir%\*.*"

答案 2 :(得分:1)

您可以准备两个批处理文件,一个用于将RAW文件重命名为1.RAW,2.RAW等,另一个用于反转此过程。

重命名脚本将RAW文件的原始名称存储在相应的txt文件中:

@echo OFF
@setlocal ENABLEDELAYEDEXPANSION


set I=1

for %%G in (*.RAW) do (
    set ORIGINAL_NAME=%%~nG
    ( 
        REM Try to rename file
        ren "%%G" "!I!.RAW"
    ) && (
        REM Renaming was successful
        > "!I!.txt" echo !ORIGINAL_NAME!
        set /A I+=1
    ) || (
        REM Renaming was a failure
        echo Cannot rename [!ORIGINAL_NAME!.RAW] file.
    )
)

@endlocal

RenameBack脚本使用该信息来恢复所有相应文件的名称:

@echo OFF
@setlocal ENABLEDELAYEDEXPANSION


for %%F in (*.txt) do (
    set BASENAME=%%~nF
    REM Read original name from txt file
    for /F %%G in (%%F) do (
        REM iterate over all corresponding files
        for %%H in (!BASENAME!.*) do (
            set EXTENSION=%%~xH
            REM Remove dot from extension string
            set EXTENSION=!EXTENSION:~1!
            if not "!EXTENSION!" == "txt" (
                REM Process files
                (
                    REM try to rename corresponding file to old name
                    ren "!BASENAME!.!EXTENSION!" "%%G.!EXTENSION!"
                ) && (
                    REM Operation was successful - remove TXT file
                    del /F /Q "%%F"
                ) || (
                    REM Something went wrong
                    echo Cannot restore old name for file [!BASENAME!.!EXTENSION!].
                )
            )
        )
    )
)

@endlocal