【问题标题】:Batch script move file with rename source file if it exist in destination folder [duplicate]如果目标文件夹中存在带有重命名源文件的批处理脚本移动文件[重复]
【发布时间】:2019-04-08 18:36:57
【问题描述】:

我需要将文件 test1.txt 从 C:\temp\ 移动到 D:\temp\(例如)。如果目标文件夹 D:\temp\ 中已经存在同名文件,我想将源文件移动并重命名为 test1.txt~。 是否可以进一步简化或优化脚本?

move c:\temp\test1.txt d:\temp\test1.txt~ || echo No | move /-Y c:\temp\test1.txt d:\temp\test1.txt

我需要一个用于 Windows 的类似 Linux 命令:

test -d DestinationFolder || mkdir -p DestinationFolder && mv -bfv SourceFile DestinationFolder

【问题讨论】:

  • 使用if exist 检查目标文件是否已经存在。无论如何,如果新名称也已经存在怎么办?也看看this answer(用move替换copy应该没什么大不了的);而不是编号,您可以只附加 ~~~ 等字符串(这在链接的答案中很容易实现)
  • @aschipfl 我不仅需要“复制所有文件而不覆盖”我还需要“移动和重命名源文件”

标签: windows batch-file cmd


【解决方案1】:

commandA && commandB 运行commandA,如果成功则运行 命令B

echo No | move /-Y c:\temp\test1.txt d:\temp\test1.txt && move c:\temp\test1.txt d:\temp\test1.txt~

我认为这是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多