2012-06-19 2 views
2

A I 여기 두려워하지만, 약간 오래된 질문은 간다 :배치 파일은 폴더에있는 파일의 이름을 변경하고 다시

내가 시퀀스 예를 들어, 일부 .RAW 파일을 생성하는 프로그램이 있습니다.

Example_1.RAW
Example_2.RAW는

이는 예를 들어, 필요에 따라 수, 여분의 유효 숫자를 추가합니다.

Example_10.RAW
Example_200.RAW

그때 출력으로 더 많은 파일을 생성하는 배치 프로세서를 통해 실행하는 번호에이 파일 이름을 변환 할 필요가있다. 이것은 다음 등 1.RAW, 2.RAW 모든 내 .RAWs 이름을 변경

@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 

: 내 코드는, 이름 변경을 수행하고 텍스트 파일에 이전 및 새 파일 이름을 저장하는 배치 파일을 작성했습니다 old 및 new 파일 이름으로 Names.txt를 만듭니다. 도스 프로세스의 숫자 덕분에 이것은 다소 우스운 번호 매김 과정을 의미합니다. 즉, Ex_1, Ex_10, Ex_100, Ex_101을 1, 2, 3, 4로 처리하여 사후 처리에서 더 많은 작업을하게됩니다. 적절한 결과를 얻으려면 이러한 결과 중 하나를 선택해야합니다.

Names.txt를 사용하는 다른 배치 파일을 작성하고 출력 파일의 프로세스를 되돌릴 수 있습니까? 그러면 1.raw, 1.something, 1.something 등의 폴더를 가지고 Name.txt를 참조하여 Example_1.raw 등으로 이름을 바꿉니 까?

+0

원시 파일에 nxtxt 파일이 있습니까? 그렇지 않은 경우 원시 파일의 새 이름과 일치하는 이름을 가진 원래 원시 파일 이름을 txt 파일에 저장할 수 있습니다 (예 : 1.txt 파일에 Example_1.RAW를 저장하고 Example_1.RAW의 이름을 1.RAW로 바꿉니다. 또 다른 배치는 txt 파일을 반복하고 해당 RAW 파일을 찾고 이전 이름으로 바꿉니다. – Helbreder

+0

헬브러 - 확실한 옵션이 아닌 가장 좋은 솔루션인데, 포스트 프로세싱을 마친 후에 n 개의 텍스트 파일을 삭제할 수 있습니다. 나는 원래의 코드가 파일 이름을 % i % .txt로 출력하도록 수정해야한다고 생각한다. 역 코드가 될 수있는 단서가 있습니까? –

+0

그건 문제가되지 않습니다. 파일 이름에서 확장자를 제거하는 도구가 있습니다. 나는 대답을 준비 할 것이다 : – Helbreder

답변

2

이 당신은 등 1.RAW, 2.RAW에 RAW 파일의 이름을 변경 한 두 배치 파일을 준비 할 수있는 작업을

@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

완벽! 내가 만든 유일한 변경 사항은 번호 접미사가없는 원래 파일 이름으로 파일을 다시 필요로하므로 마지막 두 번의 호출에서 _ %% number %%를 삭제하는 것입니다.그 외에도 나는 두 개의 이름을 하나의 "Rename"과 "NameBack"배치 파일로 나누었습니다.이 파일은 매력처럼 작동했습니다. 감사! –

1

을해야하고, 두 번째는이 과정을 다시 반전.

이름 바꾸기 스크립트는 TXT 파일에 대응에서 RAW 파일의 원래 이름을 저장합니다

@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 
2

내가 할 수 있었다 :

@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 스크립트는 정보가 모든 해당 파일의 이름을 복원하는 것을 사용 '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%\*.*" 
관련 문제