批处理 For 循环转义星号

发布于 2024-12-06 05:14:58 字数 652 浏览 2 评论 0原文

我正在尝试创建一个批处理 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 技术交流群。

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

发布评论

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

评论(2

假扮的天使 2024-12-13 05:14:58

星号按照其预期的方式工作,在您的情况下,

@FOR %%A IN (A* B) DO @ECHO %%A

将 A* 扩展到以 A 开头的所有文件。

实现您想要的操作的一种可能方法就是使用此扩展

@ECHO off
PUSHD C:\bar
SET FILE_LIST=A.txt B.txt C*.txt
FOR %%A IN (%FILE_LIST%) DO (
  IF EXIST %%A COPY %%A C:\Foo\
)
POPD

the asterisks works the way its intended, in your case,

@FOR %%A IN (A* B) DO @ECHO %%A

expands A* to all the files that begin with A.

A possible way to do what you want, is just to use this expansion

@ECHO off
PUSHD C:\bar
SET FILE_LIST=A.txt B.txt C*.txt
FOR %%A IN (%FILE_LIST%) DO (
  IF EXIST %%A COPY %%A C:\Foo\
)
POPD
人间不值得 2024-12-13 05:14:58

这可能有帮助:

@echo off
set "it=a*b .txt-b*.txt-c*.txt-d.txt"
set /a i=0,fn=3
:startLoop
set /a i=i+1
for /f "tokens=%i%delims=-" %%m in ("%it%") do echo %%m
if %i% lss %fn% goto:startLoop

This may help:

@echo off
set "it=a*b .txt-b*.txt-c*.txt-d.txt"
set /a i=0,fn=3
:startLoop
set /a i=i+1
for /f "tokens=%i%delims=-" %%m in ("%it%") do echo %%m
if %i% lss %fn% goto:startLoop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文