windows 批量连接字符串和变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36480837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:32:56  来源:igfitidea点击:

Concatenating strings and variables in batch

windowsbatch-filecmdstring-concatenation

提问by ProtoVB

I would like to create a dynamic file path in .batfile. At the moment the file path looks like this:

我想在.bat文件中创建一个动态文件路径。目前文件路径如下所示:

"C:\FolderA\FolderB\FileA.xlsx"

I would like to incorporate today's date in the file name to produce something like that:

我想在文件名中加入今天的日期以产生类似的东西:

/exp:"C:\FolderA\FolderB\FileA " & date() & ".xlsx" /T`

I have tried creating a variable and concatenating it with the hard coded part but it does not work:

我尝试创建一个变量并将其与硬编码部分连接,但它不起作用:

set Mydate=!date:~10,4!!date:~7,2!!date:~4,2!
/exp:"C:\FolderA\FolderB\FileA "&%Mydate%&".xlsx" /T

What are the rules on concatenating characters and variables and on quotation marks in batch? How to debug in batch using Notepad?

批量连接字符和变量以及引号的规则是什么?如何使用记事本批量调试?

采纳答案by Stephan

?

?

/exp:"C:\FolderA\FolderB\FileA "&%Mydate%&".xlsx" /T

?

?

This is not cmdsyntax. To set a variable, use the setcommand. Also to concatenate, you don't have (read: must not) use something like concatenation symbols:

这不是cmd语法。要设置变量,请使用该set命令。同样要连接,您没有(阅读:不得)使用连接符号之类的东西:

set "var=C:\FolderA\FolderB\FileA %Mydate%.xlsx"

(whatever /exp:or /tis supposed to do - it does not work in cmd)

(无论做什么/exp:/t应该做什么 - 它都不起作用cmd

To rename a file, use ren(or the long form rename). To get help to a command use command /?e.g. rename /?

要重命名文件,请使用ren(或长格式rename)。要获得命令的帮助,请使用command /?例如rename /?