简体   繁体   English

如何用带引号的字符串替换Windows批处理文件中的%% f?

[英]How can I replace %%f in a Windows batch file with a quoted string?

I've done some initial searches here and couldn't find an answer that quite fits what I am trying to do here. 我已经在此处进行了一些初步搜索,但找不到与我在此尝试的答案完全匹配的答案。 I have basically inherited this batch script to handle a specific process of copying / moving / manipulating a set of files, and it was recently brought up that it does not work if the batch file is run from a directory that has spaces in it. 我基本上已经继承了该批处理脚本,以处理复制/移动/处理一组文件的特定过程,最近有人提出,如果从其中有空格的目录运行该批处理文件,它将不起作用。 I've narrowed the problem down to the following block, and cannot find the right way to replace it: 我已将问题缩小到以下块,并且找不到替换它的正确方法:

for /r %%f in (*.ext1) do (
  echo %%f
  my_tool.exe %%f %%f.ext2
  del /f /q %%f
)

So basically what should happen here is, my_tool takes two parameters, an input file and an output file. 因此,基本上应该在这里发生的是,my_tool带有两个参数,一个输入文件和一个输出文件。 It does some processing to the input file, and outputs it as the same file, but with an additional .ext2 extension to it. 它对输入文件进行一些处理,然后将其输出为相同文件,但附加扩展名为.ext2。 (foo.ext1 becomes foo.ext1.ext2) (foo.ext1变成foo.ext1.ext2)

The problem is that %%f does not quote the fully qualified path to the file, so the tool is getting more parameters than it is expecting, and not in the proper formatting. 问题在于%% f没有引用文件的全限定路径,因此该工具获得的参数比预期的要多,并且格式不正确。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Put paramenters in double quotes, so batch will transmit them to your tool as 2 paramenters: 将参数以双引号引起来,因此批处理会将它们作为2个参数传递给您的工具:

for /r %%f in (*.ext1) do (
  echo "%%f"
  my_tool.exe "%%f" "%%f.ext2"
  del /f /q "%%f"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM