0

ローカルドライブの特定のパスに存在する2つの特定のファイルの名前を切り替える必要があり、.batまたは.vbsでそれを実現できるかどうか疑問に思います。

つまり、スクリプトを1回実行すると、 「ファイルA」が「ファイルB」(「C:\ PathA \ FileA.txt」と「C:\ PathA \ FileB.txt」)にスワップされ、もう一度実行すると、それらを再度交換します。

同じことができるかどうかも知りたいです。

1)この状況では->>「C:\ SomePath \ FileA.txt」および「D:\ SomeOtherPath\FileB.txt」

2)2つのファイルの代わりに、2つのフォルダーを切り替えたい場合。

4

1 に答える 1

3

次のバッチファイルを作成し、任意の名前を付けます。「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のいずれかが見つからない場合、そのエラーが画面に報告され、プロセスが停止します。

于 2012-10-14T04:13:28.457 に答える