Windows XP批处理文件连接
我正在尝试完成以下荒谬的任务:
我有一个包含一组完全限定的文件名的文本文件。我想迭代该文件并将每一行附加到一个公共变量,该变量可以传递给命令行工具。例如,该文件可能是:
C:\dir\test.txt
C:\WINDOWS\test2.txt
C:\text3.txt
我想将它们分配给某个变量“a”,这样:
a = "C:\dir\test.txt C:\WINDOWS\test2.txt C:\text2.txt"
第二个问题是 - 什么是好的批处理文件参考?我在 Windows 材料和许多本土网站中找到了一些东西,但没有什么特别完整的。
I'm trying to accomplish the following ridiculous task:
I have a text file containing a set of fully qualified filesnames. I want to iterate through the file and append each line to a common variable, that can be passed to a command line tool. For example, the file might be:
C:\dir\test.txt
C:\WINDOWS\test2.txt
C:\text3.txt
and I'd like to assign them to some variable 'a' such that:
a = "C:\dir\test.txt C:\WINDOWS\test2.txt C:\text2.txt"
A secondary question is - what is a good batch file reference? I'm finding some stuff in the Windows material, and a lot of home-grown websites, but nothing particularly complete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
至于参考,SS64.com 还不错。 Rob van der Woude 也经常被链接。
至于您的问题,这很简单:
更深入的解释:
我们在这里启用延迟扩展。这一点至关重要,否则我们将无法在随后的
for
循环中操作文件列表。for /f
迭代文件中的行,这正是我们所需要的。在每次循环迭代中,我们将下一行附加到LIST
变量中。请注意使用!LIST!
而不是通常的%LIST%
。这表示延迟扩展并确保每次运行此命令时都会重新评估变量。通常,一旦读取并解析了一行,
cmd
就会将变量扩展为它们的值。对于cmd
来说,单行要么是一行,要么是算作的所有内容,这恰好适用于由括号分隔的块,就像我们在这里使用的那样。因此,对于 cmd 来说,完整的块是一个单独的语句,无论循环内部运行的频率如何,它都会被读取和解析一次。如果我们在这里使用
%LIST%
而不是!LIST!
,那么该变量将立即被其值替换(此时为空),并且循环将具有看起来像这样:显然这不是我们想要的。延迟扩展可确保仅在真正需要变量值时才扩展变量。在这种情况下,当循环内部运行并构造文件名列表时。
之后,变量
%LIST%
或!LIST!
(现在使用哪个已经不再重要)包含文件中的行列表。有趣的是,
set
命令的帮助正是包含这个延迟扩展的示例:As for references, SS64.com isn't bad. Rob van der Woude gets linked fairly often, too.
As for your problem, that's easy:
More in-depth explanation:
We're enabling delayed expansion here. This is crucial as otherwise we wouldn't be able to manipulate the list of files within the
for
loop that follows.for /f
iterates over lines in a file, so exactly what we need here. In each loop iteration we append the next line to theLIST
variable. Note the use of!LIST!
instead of the usual%LIST%
. This signals delayed expansion and ensures that the variable gets re-evaluated every time this command is run.Usually
cmd
expands variables to their values as soon as a line is read and parsed. Forcmd
a single line is either a line or everything that counts as a line, which happens to hold true for blocks delimited by parentheses like the one we used here. So forcmd
the complete block is a single statement which gets read and parsed once, regardless of how often the interior of the loop runs.If we would have used
%LIST%
here instead of!LIST!
then the variable would have been replaced immediately by its value (empty at that point) and the loop would have looked like this:Clearly this isn't what we wanted. Delayed expansion makes sure that a variable is expanded only when its value is really needed. In this case when the interior of the loop runs and constructs a list of file names.
Afterwards the variable
%LIST%
or!LIST!
(now it doesn't really matter anymore which to use) contains the list of lines from the file.Funnily enough, the help for the
set
command includes exactly this example for delayed expansion:您可以使用 FOR /F 命令来完成您想要的操作。
这是我多次使用的好资源:
http://www.robvanderwoude.com/batchfiles.php
What you're after can be done with a FOR /F command.
Here's a good resource I've used many times:
http://www.robvanderwoude.com/batchfiles.php
一本好书:Tim Hill 的《Windows NT Shell 脚本》。我的版本是 1998 年发布的,但它对于 Windows 2008 中的 Windows 命令程序仍然有效。
A good book: Windows NT Shell Scripting by Tim Hill. The edition I have was published in 1998 but it is still valid for Windows command programs in Windows 2008.
操作系统:Windows Server 2003
OS: WINDOWS SERVER 2003