windows 使用 forfiles 和 7zip 的文件名中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6120624/
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
Spaces in filenames using forfiles and 7zip
提问by chesterm8
I am writing a script to zip up old files, using forfiles for the looping and 7za for the zipping. I have been struggling with how to deal with spaces in the filenames, and although I have found a solution that seems to work, not understanding why has me nervous about deploying it to production.
我正在编写一个脚本来压缩旧文件,使用 forfiles 进行循环,使用 7za 进行压缩。我一直在努力解决如何处理文件名中的空格,虽然我找到了一个似乎有效的解决方案,但不明白为什么我对将其部署到生产环境感到紧张。
Here is the command that appears to work:
这是似乎有效的命令:
forfiles -p%rootLogDir% -s -m*.log -d-14 -c"cmd /c 7za a -tzip """@PATH\@FILE-%date%.zip""" """@PATH\@FILE""""
Note that the path and filename arguments to 7za have 3 (yes THREE) sets of quotes around them. One set of quotes does nothing, two sets of quotes resulted in the script adding every file in the directory into the archive, rather than the one specified, and three appears to work.
请注意, 7za 的路径和文件名参数在它们周围有 3(是三)组引号。一组引号什么也不做,两组引号导致脚本将目录中的每个文件添加到存档中,而不是指定的那一组,而三组似乎可以工作。
Can anyone explain why so many are required? I would have thought that after one set the rest are superfluous, but apparently I am wrong.
谁能解释为什么需要这么多?我会认为在一组之后其余的都是多余的,但显然我错了。
采纳答案by shellter
( forfiles
is a cmd.exe subcommand? If so, you should re-tag your question with one of the many (MS)windows tags available. )
(forfiles
是 cmd.exe 子命令吗?如果是这样,您应该使用许多可用的 (MS)windows 标记之一重新标记您的问题。)
Quoting can be a maddening experience, even for people with much experience in shell and bat scripts.
引用可能是一种令人抓狂的体验,即使对于在 shell 和 bat 脚本方面有丰富经验的人也是如此。
@BugFinder's msg is appropriate, and I would add that 3 dbl-quotes is one way to "quote" a single double-quote char.
@BugFinder 的 msg 是合适的,我想补充一点,3 dbl-quotes 是“引用”单个双引号字符的一种方式。
You are working 'with' the command processor (cmd.exe?). You type a command (with 1 or more words or operators), i.e.
您正在“使用”命令处理器(cmd.exe?)。您键入一个命令(带有 1 个或多个单词或运算符),即
myCommand /s /d %dir% file1 file2 > sumFile
and then press the enter key. The command processor doesn't just start executing the command you have submitted, it scans the line, looking for special case words and symbols, like variable names. In Unix shells environment variables look like $varName
or ${varName}
. In .bat files %var%
or %%v%%
. That variable has to be converted into a value.
然后按回车键。命令处理器不只是开始执行您提交的命令,它还会扫描行,寻找特殊的单词和符号,例如变量名。在 Unix shells 环境变量看起来像$varName
or ${varName}
。在 .bat 文件%var%
或%%v%%
. 该变量必须转换为值。
That is just one pass that the command processor makes, there are others. But for your needs, the cmd-processor is scanning for word sets that should be taken as one word. If you have a var="one two"
, not surrounding the value side with quotes will confuse any command that has to process it. it will look like var=one .... two
(separate word, right?).
这只是命令处理器进行的一次传递,还有其他传递。但是为了您的需要,cmd 处理器正在扫描应视为一个词的词集。如果您有一个var="one two"
, 没有用引号将值侧括起来会混淆任何必须处理它的命令。它看起来像var=one .... two
(单独的词,对吧?)。
So if for some reason you need to pass thru a double-quote char to a lower layer of processing, you have to follow the command processor rules, quote things as needed and then everything will work ok.
因此,如果由于某种原因您需要将双引号字符传递给较低的处理层,则必须遵循命令处理器规则,根据需要引用内容,然后一切正常。
Just as "word" is a quoted word, """
is a quoted double quote. After the command processor does it normal processing of quotes, the middle double quote will still be remaining in the command processor's 'view' of what is on the command line (after variable have been expanded, and all the other order of processing rules have been followed.)
就像“word”是一个带引号的词一样,"""
是一个带引号的双引号。命令处理器对引号进行正常处理后,中间的双引号仍将保留在命令处理器对命令行内容的“视图”中(在变量被扩展后,所有其他处理规则的顺序都已跟着。)
Whew!
哇!
So to wrap it up, you wrote in a comment
所以总结一下,你在评论中写道
But then
"""
->""
->"
,
但随后
"""
->""
->"
,
No, """
=> "
不,"""
=>"
so why is one quote not sufficient?
那么为什么一个报价是不够的呢?
Because using """
is one way to have the command processor leave a stand-alone double quote char remain on the command line and not blow up.
因为 using"""
是让命令处理器在命令行上保留一个独立的双引号字符而不是炸毁的一种方法。
Or does the first condensed double quote gain some sort of magical power?
或者第一个浓缩的双引号是否获得了某种神奇的力量?
No magical powers, note that your command has a later """
s. Each set is leaving 1 "
char for later processing.
没有神通,注意你的命令后面有"""
s。每组留下 1 个"
字符供以后处理。
As @BugFinder points out, \"
might be sufficient, but I'm not sure which command processor or shell you are using, so I can't run a test.
正如@BugFinder 指出的那样,\"
可能就足够了,但我不确定您使用的是哪个命令处理器或 shell,因此我无法运行测试。
Finally, Per an excellent reminder written by belisarius,
最后,根据 belisarius 写的一个很好的提醒,
Allow me to welcome you to StackOverflow and remind three things we usually do here: 1) As you receive help, try to give it too, answering questions in your area of expertise 2) Read the FAQs, http://tinyurl.com/2vycnvr, 3) When you see good Q&A, vote them up by using the gray triangles, http://i.stack.imgur.com/kygEP.png, as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign , http://i.stack.imgur.com/uqJeW.png
请允许我欢迎您使用 StackOverflow 并提醒我们通常在这里做的三件事:1) 在您获得帮助时,也尝试提供帮助,回答您专业领域的问题 2) 阅读常见问题解答,http://tinyurl.com/ 2vycnvr, 3) 当你看到好的问答,使用灰色三角形给他们投票, http://i.stack.imgur.com/kygEP.png,因为系统的可信度是基于用户获得的声誉分享他们的知识。还记得通过按复选标记来接受更好地解决您的问题的答案,如果有的话,http://i.stack.imgur.com/uqJeW.png
I hope this helps.
我希望这有帮助。
回答by BugFinder
At a quick guess, like a number of things """ = ", eg 2 quotes round a quote.. a \" may have also been sufficient.
快速猜测一下,像很多东西一样“”“=”,例如2个引号围绕一个引号......一个\“也可能就足够了。
回答by user7156149
All filenames and paths which contain spaces must be quoted.
所有包含空格的文件名和路径都必须用引号引起来。
Next, regarding your question, how about stating the path like:
接下来,关于您的问题,如何说明路径:
start /max /d"C:\Program files\foo\" ba.exe -somearguments