batch-file - 如何在命令行上将文件名和扩展名分隔成变量

标签 batch-file cmd

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

如果 x.dtsx 已经存在于 b 中,那么我需要将 x.dtsx 重命名为 x_Standby.dtsx 然后,重命名后将 x.dtsx 复制到 b

我当前的代码如下所示:

if exists %1 rename %1 %(should be 1_standy.extension)
xcopy %1 %2

最佳答案

如果您使用命令处理器扩展(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 exist %2\%~nx1 ren %2\%~nx1 %~n1_standby%~x1

关于batch-file - 如何在命令行上将文件名和扩展名分隔成变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8749637/

相关文章:

c# - 我们可以从共享路径运行批处理文件 (.bat)

python - 在 python 或类似中打开 URL "silently"

mysql - 从命令提示符使用批处理文件运行 sql 脚本

windows - 如何将 .bat 文件中的变量导入到 PowerShell 脚本中?

python-3.x - 如何使用 python 发布curl命令

javascript - 在 Node.js 命令上,为什么我收到错误消息 "windows cannot find $. Make sure you' 已经正确输入了名称...”

windows - 将文件的第一行设置为变量的最短 Windows 批处理文件代码?

windows - 即使没有 SETLOCAL,在嵌套脚本中使用循环变量是否安全

windows - 批处理文件范围

windows - xcopy 可以传输子目录中的文件,但不能传输目录本身吗?