ファイルを処理する簡単なバッチスクリプトを次に示します。
@echo off
rem set your file-match pattern here. Could be *.* for all files, or whatever you wish.
set "zzmatchpattern=FILE.txt.*"
rem set filename for renamed files
set "zzfinalname=TEST.txt"
rem set source folder here
set "zzsourcepath=C:\source\"
rem set destination folder here
set "zzdestpath=C:\dest\"
rem set your loop delay here
set "zzdelayminutes=15"
rem **********************************
rem might be good to add some error checking here
rem **********************************
:start
rem:start
set /a zzdelayseconds=zzdelayminutes*60
:restart
rem:restart
for %%f in ("%zzsourcepath%%zzmatchpattern%") do call :work "%%~f"
timeout /t %zzdelayseconds%
goto :restart
:work
rem:work
set "zz_w1=%~1"
echo.
echo Renaming file "%zz_w1%" to "%zzfinalname%"
ren "%zz_w1%" "%zzfinalname%">nul 2>&1
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
timeout /t %zzdelayseconds%
rem Go get next file, if any.
goto :EOF
より完全なソリューションが必要な場合は、以下のバッチスクリプトに含まれています。
- 入力値の妥当性検証(エラーチェック)をかなり完了します。
- 簡単な変更で、コマンドラインから安全に値を入力できます。
- 開始時に、既存のターゲットファイルが存在するかどうかを確認し、外部プロセスがファイルを削除するのを待ちます。
- 失敗した名前変更および移動操作を確認して報告します。
- 終了/失敗後に名前が変更されたが移動されていないファイルを処理した後に再開します。
- (オプション)プロンプト表示された遅延(Choice.exe)を許可して、「今すぐ続行」と「優雅な」終了を許可します。
@echo off
rem set your file-match pattern here. Could be *.* for all files, or whatever you wish.
set "zzmatchpattern=FILE.txt.*"
rem set filename for renamed files
set "zzfinalname=TEST.txt"
rem set source folder here
set "zzsourcepath=C:\source"
rem set destination folder here
set "zzdestpath=C:\dest"
rem set your loop delay here
set "zzdelayminutes=15"
rem select Prompted-delay, or simple Timeout-delay. P=Prompted, otherwise simple Timeout
rem For Prompted-delay "Choice.exe" must be present on the computer.
set "zzdelaytype=T"
rem **********************************
rem error checking
rem **********************************
:checksourcepath
rem:checksourcepath
rem insure source path is valid (exists), and has trailing slash
if "%zzsourcepath%."=="." goto :sourcepatherror
set zzt1=%zzsourcepath:~-1,1%
if not "%zzt1%."=="\." set "zzsourcepath=%zzsourcepath%\"
if not exist "%zzsourcepath%*.*" goto :sourcepatherror
goto :checkdestpath
:sourcepatherror
rem:sourcepatherror
echo.
echo Error processing source path: [%zzsourcepath%].
echo.
call :cleanexit
exit /b 1
:checkdestpath
rem:checkdestpath
rem insure dest path is valid (exists), and has trailing slash
if "%zzdestpath%."=="." goto :destpatherror
set zzt1=%zzdestpath:~-1,1%
if not "%zzt1%."=="\." set "zzdestpath=%zzdestpath%\"
if not exist "%zzdestpath%*.*" goto :createdestpath
goto :checkname
:createdestpath
rem:createdestpath
md "%zzdestpath%" >nul 2>&1
if not exist "%zzdestpath%*.*" goto :destpatherror
goto :checkname
:destpatherror
rem:destpatherror
echo.
echo Error processing destination path: [%zzdestpath%].
echo.
call :cleanexit
exit /b 2
:checkname
rem:checkname
if "%zzfinalname%."=="." goto :nameerror
goto :checkdelayminutes
:nameerror
rem:nameerror
echo.
echo Error: Rename target filename cannot be empty.
echo.
call :cleanexit
exit /b 3
:checkdelayminutes
rem:checkdelayminutes
set zzt1=0
set zzt2=1
set /a zzt1=zzt2*zzdelayminutes
if "%zzt1%."=="." goto :minute1serror
if %zzt1% LEQ 0 goto :minutes1error
if %zzt1% GEQ 1441 goto :minutes2error
goto :checkpattern
:minutes1error
rem:minutes1error
echo.
echo Error: Minutes must be a number greater than 0.
echo.
call :cleanexit
exit /b 4
:minutes2error
rem:minutes2error
echo.
echo Error: Minutes must be a number not greater than 1440 (1 day).
echo.
call :cleanexit
exit /b 5
:checkpattern
rem:checkpattern
rem pattern must have at least 1 "*"
if "%zzmatchpattern%."=="." goto :patternerror
echo "%zzmatchpattern%"|find "*">nul
if %errorlevel% EQU 0 goto :start
rem goto :patternerror
:patternerror
rem:patternerror
echo.
echo Error: Pattern cannot be empty, and must contain at least 1 "*".
echo.
call :cleanexit
exit /b 6
:start
rem:start
set /a zzdelayseconds=zzdelayminutes*60
set /a zzpartialdelay=zzdelayseconds/3
set zzexiting=0
:restart
rem:restart
rem if destination file already exists, wait a bit more
if not exist "%zzdestpath%%zzfinalname%" goto:checkaborted
call :waitexternal
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :restart
:checkaborted
rem:checkaborted
if not exist "%zzsourcepath%%zzfinalname%" goto :work1
echo.
echo Completing previously started file move.
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
goto :restart
:work1
rem:work1
set zzdelayflag=1
set zzsuccess=0
for %%f in ("%zzsourcepath%%zzmatchpattern%") do call :work2 "%%~f"
if %zzexiting% NEQ 0 exit /b %zzexiting%
echo %zzsuccess% file(s) processed.
if %zzdelayflag% EQU 0 goto :checksuccess
call :dodelay %zzdelayseconds%
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :restart
:checksuccess
rem:checksuccess
if %zzsuccess% NEQ 0 goto :restart
echo.
echo Failed to rename all existing files, exiting.
call :cleanexit
set zzexiting=7
exit /b %zzexiting%
goto :EOF
:work2
rem:work2
if %zzexiting% NEQ 0 exit /b %zzexiting%
set "zz_w1=%~1"
set zzdelayflag=0
echo.
echo Renaming file "%zz_w1%" to "%zzfinalname%"
ren "%zz_w1%" "%zzfinalname%">nul 2>&1
if exist "%zz_w1%" goto :renameerror
if not exist "%zzsourcepath%%zzfinalname%" goto :renameerror
goto :movefinalfile
:renameerror
rem:renameerror
echo Error: Failed to rename file "%zz_w1%" to "%zzfinalname%"
echo.
rem Rename failed, skip it and get next file (immediately), if any.
goto :EOF
:movefinalfile
rem:movefinalfile
rem Rename success, move the file
if not exist "%zzdestpath%%zzfinalname%" goto :domove
call :waitexternal
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :movefinalfile
:domove
rem:domove
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
if exist "%zzsourcepath%%zzfinalname%" goto :moveerror
goto :movecomplete
:moveerror
rem:moveerror
echo Error: Failed to move file "%zz_w1%" to "%zzdestpath%%zzfinalname%"
rem Move failed, restore (un-rename) file and skip it and get next file (immediately), if any.
for %%g in ("%zz_w1%") do set "zzt1=%%~nxg"
echo Restore file: "%zzsourcepath%%zzfinalname%" to "%zzt1%"
ren "%zzsourcepath%%zzfinalname%" "%zzt1%">nul 2>&1
echo.
goto :EOF
:movecomplete
rem:movecomplete
set /a zzsuccess+=1
call :dodelay %zzdelayseconds%
if %zzexiting% NEQ 0 exit /b %zzexiting%
rem echo.
rem Go get next file, if any.
goto :EOF
:dodelay
rem:dodelay
set zztime=%1
if /I "%zzdelaytype%."=="P." goto :dopauseddelay
echo.
timeout /t %zztime%
goto :EOF
:dopauseddelay
rem:dopauseddelay
echo.
echo Waiting (%zztime% seconds) to process next file... You can wait to
echo continue after delay or Q(uit) or C(ontinue) now.
choice /c cq /n /t %zztime% /d c /m "Press Q(uit) or C(ontinue now) or No Action (wait) to continue after delay. "
if %errorlevel% EQU 1 goto :EOF
echo.
echo User selected Q(uit), exiting.
call :cleanexit
set zzexiting=254
exit /b %zzexiting%
goto :EOF
:waitexternal
rem:waitexternal
echo.
echo Waiting for externl process...
call :dodelay %zzpartialdelay%
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :EOF
:cleanexit
rem:cleanexit
set "zzdelayflag="
set "zzdelayminutes="
set "zzdelayseconds="
set "zzdelaytype="
set "zzdestpath="
set "zzexiting="
set "zzfinalname="
set "zzmatchpattern="
set "zzpartialdelay="
set "zzsourcepath="
set "zzsuccess="
set "zzt1="
set "zzt2="
set "zztime="
set "zz_w1="
goto :EOF
この修正または説明が必要な場合は、お知らせください。