批处理 For 循环转义星号
我正在尝试创建一个批处理 for 循环(Windows XP 和较新的命令提示符),该循环遍历包含一个或多个星号的字符串。我该怎么做?这是一个例子:
@FOR %%A IN (A* B) DO @ECHO %%A
预期的输出(我想要得到的)如下:
A*
B
但是,我使用上面的命令实际上得到的是 B 并且只有 B。出于某种原因,带有星号的所有内容都会被忽略环形。我尝试使用 1-4 个脱字符 (^)、反斜杠 (\)、百分号 (%) 和其他星号 (*) 转义星号,但均无济于事。预先感谢您对我的启发。
如果您需要更多信息:
这样做的目的是从空格分隔的部分路径列表中解析路径。例如,我想使用以下方法将 C:\Bar\A.txt、C:\Bar\B.txt 和 C:\Bar\C*.txt 复制到 C:\Foo\:
@SET FILE_LIST=A B C*
@FOR %%A IN (%FILE_LIST%) DO @COPY C:\Bar\%%A.txt C:\Foo\
如果还有另一个如果有替代方法(最好不要键入每个复制命令,因为有大约 200 个文件,这也是我不想存储每个文件的完整路径的原因),我将不胜感激。再次感谢,
-杰夫
I am attempting to create a batch for loop (Windows XP and newer command prompts) that iterates through a string containing one or more asterisks. How can I do this? Here is an example:
@FOR %%A IN (A* B) DO @ECHO %%A
The expected output (what I am trying to get) is the following:
A*
B
However, what I am actually getting with the command above is B and only B. For some reason, everything with an asterisk is being ignored by the loop. I have attempted escaping the asterisk with 1-4 carets (^), backslashes (\), percent signs (%), and other asterisks (*), all to no avail. Thanks in advance for illuminating me.
IN CASE YOU WANT MORE INFORMATION:
The purpose of this is to parse a path out of a list of space-separated partial paths. For example, I want to copy C:\Bar\A.txt, C:\Bar\B.txt, and C:\Bar\C*.txt to C:\Foo\ using the following approach:
@SET FILE_LIST=A B C*
@FOR %%A IN (%FILE_LIST%) DO @COPY C:\Bar\%%A.txt C:\Foo\
If there is another alternative way to do this (preferably without typing each and every copy command since there are ~200 files, which is the same reason I don't want to store the full path for every file), I would appreciate the help. Thanks again,
-Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
星号按照其预期的方式工作,在您的情况下,
将 A* 扩展到以 A 开头的所有文件。
实现您想要的操作的一种可能方法就是使用此扩展
the asterisks works the way its intended, in your case,
expands A* to all the files that begin with A.
A possible way to do what you want, is just to use this expansion
这可能有帮助:
This may help: