使用 forfiles 和 7zip 的文件名中的空格

发布于 2024-11-10 00:54:36 字数 419 浏览 8 评论 0原文

我正在编写一个脚本来压缩旧文件,使用 forfiles 进行循环,使用 7za 进行压缩。我一直在努力解决如何处理文件名中的空格,尽管我找到了一个似乎有效的解决方案,但不明白为什么我对将其部署到生产中感到紧张。

下面是看起来有效的命令:

forfiles -p%rootLogDir% -s -m*.log -d-14 -c"cmd /c 7za a -tzip """@PATH\@FILE-%date%.zip""" """@PATH\@FILE""""

请注意,7za 的路径和文件名参数周围有 3 个(是的,三组)引号。一组引号不执行任何操作,两组引号导致脚本将目录中的每个文件(而不是指定的文件)添加到存档中,而三组引号似乎可以工作。

谁能解释一下为什么需要这么多?我本以为一组之后剩下的都是多余的,但显然我错了。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆で 2024-11-17 00:54:36

forfiles 是 cmd.exe 子命令?如果是这样,您应该使用众多可用的 (MS)windows 标签之一重新标记您的问题。)

引用可能是一种令人发狂的体验,即使对于具有以下功能的人也是如此 :丰富的 shell 和 bat 脚本经验。

@BugFinder 的消息是合适的,我想补充一点,3 个双引号是“引用”单个双引号字符的一种方法。

您正在“使用”命令处理器(cmd.exe?)。您键入一个命令(包含 1 个或多个单词或运算符),即

myCommand /s /d %dir% file1 file2 > sumFile

,然后按 Enter 键。命令处理器不仅仅开始执行您提交的命令,它还会扫描该行,查找特殊情况的单词和符号,例如变量名。在 Unix shell 中,环境变量类似于 $varName${varName}。在 .bat 文件 %var%%%v%% 中。该变量必须转换为一个值。

这只是命令处理器执行的一次操作,还有其他操作。但根据您的需要,cmd 处理器正在扫描应视为一个单词的单词集。如果您有一个 var="one Two",不使用引号将值一侧括起来将会使任何必须处理它的命令感到困惑。它看起来像 var=one .... Two (单独的单词,对吧?)。

因此,如果由于某种原因您需要将双引号字符传递到较低的处理层,则必须遵循命令处理器规则,根据需要引用内容,然后一切都会正常工作。

正如“word”是带引号的单词,""" 是带引号的双引号。命令处理器对引号进行正常处理后,中间的双引号仍将保留在命令处理器的 '查看'命令行上的内容(在扩展变量之后,并且遵循所有其他处理规则顺序。)

哇!

所以为了总结它,您在评论中写道

然后 """ -> "" -> " ,

不,""" => <代码>“

那么为什么单引号还不够呢?

因为使用 """ 是让命令处理器将独立双引号字符保留在命令行上而不被破坏的一种方法。

或者第一个压缩双引号是否获得了某种神奇的力量?

没有神奇的力量,请注意,您的命令有后面的 """。每组都留下 1 " 字符供以后处理。

正如 @BugFinder 指出的, \" 可能就足够了,但我不确定您正在使用哪个命令处理器或 shell,所以我无法运行测试。

( forfiles is a cmd.exe subcommand? If so, you should re-tag your question with one of the many (MS)windows tags available. )

Quoting can be a maddening experience, even for people with much experience in shell and bat scripts.

@BugFinder's msg is appropriate, and I would add that 3 dbl-quotes is one way to "quote" a single double-quote char.

You are working 'with' the command processor (cmd.exe?). You type a command (with 1 or more words or operators), i.e.

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.

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?).

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.)

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.

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.

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.

用心笑 2024-11-17 00:54:36

快速猜测一下,就像很多东西“””=“,例如,两个引号围绕一个引号..一个\”可能也足够了。

At a quick guess, like a number of things """ = ", eg 2 quotes round a quote.. a \" may have also been sufficient.

为人所爱 2024-11-17 00:54:36

所有包含空格的文件名和路径都必须加引号。

接下来,关于您的问题,如何陈述路径,例如:

start /max /d"C:\Program files\foo\" ba.exe -somearguments

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文