【问题标题】:Is there a way to write a batch file to rename a file using a set variable?有没有办法编写批处理文件以使用设置变量重命名文件?
【发布时间】:2020-01-19 17:45:32
【问题描述】:

我正在尝试创建一个批处理文件来创建文件夹并将文件放置在这些文件夹中。我已经完成了,但我现在想将文件重命名为一个设置变量(项目的名称)

我已经尝试过重命名功能。

@echo off
SET x="Project Name"
md %x%\"Bid Proposals"
md %x%\"Reports"
md %x%\"Drawings"
md %x%\"Specifications"
md %x%\"Addendums"
cd /d  K:\ESTIMATING\2019 ESTIMATING\2019 BID-PROPOSALS\%x%\Bid Proposals
copy "filelocation\Bid Proposal - Template.docx" 
rename "Bid Proposal - Template" "%x% - Bid Proposal"

我希望它使用变量将文件重命名为项目名称。

它复制并放置文件,但不重命名。

【问题讨论】:

    标签: batch-file variables rename


    【解决方案1】:

    引用过多。

    试试

    @echo off
    SET "pname=Project Name"
    for %%a in ("Bid Proposals" "Reports" "Drawings" "Specifications"
     "Addendums") do md "%pname%\%%~a"
    cd /d  K:\ESTIMATING\2019 ESTIMATING\2019 BID-PROPOSALS\%pname%\Bid Proposals
    copy "filelocation\Bid Proposal - Template.docx" .
    rename "Bid Proposal - Template" "%pname% - Bid Proposal"
    

    语法SET "var=value"(其中值可能为空)用于确保分配的值中不包含任何杂散的尾随空格。

    我已将 x 更改为更有意义的内容。

    for 依次将每个 [在这种情况下,引用] 值分配给 %%a%%~a 删除 %%a 中的引号。

    我相信copy 将需要一个目的地。我已经添加了.,这意味着the current directory,所以文件"filelocation\Bid Proposal - Template.docx"应该被复制到当前目录"Bid Proposal - Template.docx"

    rename 将匹配文件名,因此 from 名称可能应该是 `"Bid Proposal - Template.docx" 而 to 名称应该是 "%pname% - 投标提案.docx"

    而且我相信Addendum 的复数形式可能是Addenda

    【讨论】:

    • 我复制了您的建议,现在这是一种更有效的方法。重命名仍然不起作用....您对附录/附录也是正确的
    • 谢谢!!!我刚刚添加了文件扩展名,它解决了重命名问题。此外,我的声誉低于 15,但我确实对此表示赞同!
    • copy,目的地是可选的。如果没有给定目标,则默认为当前工作文件夹(尽管用. 明确地给它当前工作文件夹并没有什么坏处)
    • 如果我正确理解了这个任务,我看不出复制文件的理由,然后将其重命名为两个单独的指令。因此,我将删除最后一行并将其上方的行更改为 Copy /Y "filelocation\Bid Proposal - Template.docx" "%x% - Bid Proposal.docx"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2014-02-26
    相关资源
    最近更新 更多