创建以下批处理文件并将其命名为您想要的任何名称。我正在使用名称“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
假设此路径中存在这两个文件:
- c:\temp\重命名 test\File A.txt
- c:\temp\重命名测试\文件 B.txt
然后您可以运行以下命令,它们将交换名称:
myRename "c:\temp\Rename test\File A.txt" "c:\temp\Rename test\File B.txt"
如果未找到文件 A 或文件 B,则会在屏幕上报告该错误并且进程停止。