Batch file dos命令将文件名和扩展名转换为变量

Batch file dos命令将文件名和扩展名转换为变量,batch-file,dos,batch-processing,batch-rename,Batch File,Dos,Batch Processing,Batch Rename,我需要将文件x.dtsx从位置a复制到位置b 如果b中已经存在x.dtsx,那么我需要将x.dtsx重命名为x_Standby.dtsx,然后在将x.dtsx副本重命名为b之后 我当前的代码如下所示: if exists %1 rename %1 %(should be 1_standy.extension) xcopy %1 %2 if exist %2\%~nx1 ren %2\%~nx1 %~n1_standby%~x1 如果使用命令处理器扩展(Windows 2000及更高版本的默认

我需要将文件x.dtsx从位置a复制到位置b

如果b中已经存在x.dtsx,那么我需要将x.dtsx重命名为x_Standby.dtsx,然后在将x.dtsx副本重命名为b之后

我当前的代码如下所示:

if exists %1 rename %1 %(should be 1_standy.extension)
xcopy %1 %2
if exist %2\%~nx1 ren %2\%~nx1 %~n1_standby%~x1

如果使用命令处理器扩展(Windows 2000及更高版本的默认设置),则可以使用以下可选语法:

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
可以组合修改器以获得复合结果:

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
因此,您的命令将如下所示:

if exists %1 rename %1 %(should be 1_standy.extension)
xcopy %1 %2
if exist %2\%~nx1 ren %2\%~nx1 %~n1_standby%~x1

非常感谢Neil Aweome,我重复一遍:DOS不是CMD,而且
[batch processing]neq[batch=file]