次のバッチファイルを作成し、任意の名前を付けます。「myRename.bat」という名前を使用しています。
:: myRename.bat
@echo off
SETLOCAL
:: verify the first file exists
if not exist "%~1" ( echo ERROR: File not found "%~1" & goto endofscript )
:: verify the second file exists
if not exist "%~2" ( echo ERROR: File not found "%~2" & goto endofscript )
:: Create a guaranteed unique string for temporarily naming one file
set instance=%date:~-4,4%%date:~-10,2%%date:~-7,2%
set instance=%instance%-%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
set instance=%instance%-%RANDOM%
:: rename the first file to a temporary name
ren "%~1" "%~nx1.%instance%"
:: rename the second file to the first file name
ren "%~2" "%~nx1"
:: rename teh first file to the second file name
ren "%~1.%instance%" "%~nx2"
:endofscript
これらの2つのファイルがこのパスに存在すると仮定します。
- c:\ temp \ Rename test \ File A.txt
- c:\ temp \ Rename test \ File B.txt
次に、以下のコマンドを実行すると、名前が入れ替わります。
myRename "c:\temp\Rename test\File A.txt" "c:\temp\Rename test\File B.txt"
ファイルAまたはファイルBのいずれかが見つからない場合、そのエラーが画面に報告され、プロセスが停止します。