2

恐怕是一个有点过时的问题,但这里有:

我有一个程序可以按顺序生成一些 .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 else,请参阅 Names.txt 并将它们重命名为 Example_1.raw 等?

4

3 回答 3

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
于 2012-06-19T09:51:07.540 回答
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%\*.*"
于 2012-11-29T14:14:34.903 回答
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
于 2012-06-19T10:21:50.657 回答