Windows XP中的批处理文件CONCAT [英] Windows XP batch file concat

查看:140
本文介绍了Windows XP中的批处理文件CONCAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成以下任务可笑的:

我有一个包含了一套完全合格filesnames的文本文件。我想通过文件来迭代和每行追加到共同的变量,可以传递到一个命令行工具。例如,该文件可能是:


  

C:\\目录\\ test.txt的


  
  

C:\\ WINDOWS \\的test2.txt


  
  

C:\\ text3.txt


和我想将它们分配给某个变量'一'这样:

  A =C:\\目录\\ test.txt的C:\\ WINDOWS \\的test2.txt C:\\ text2.txt

一个次要的问题是 - 什么是好的批处理文件的参考?我发现在Windows材质一些东西,很多本土的网站,但没有特别完整。

谢谢!


解决方案

至于参考, SS64.com 是不坏。 罗布·范德Woude 被挂相当频繁了。


至于你的问题,那很简单:

 关闭@echo
SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion集列表=
FOR / F %%倍In(yourfile.txt)做(
    集列表=!LIST! %%X
)回声%LIST%ENDLOCAL


更深入的解释:

  SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion

我们正在实现的延迟扩展的位置。这是至关重要的,否则我们不会将能够操纵下面的循环中的文件列表。

  FOR / F %%倍In(yourfile.txt)做(
    集列表=!LIST! %%X

FOR / F 遍历文件中的行,所以正是我们需要在这里。在每个循环迭代,我们追加下一行的列表变量。注意使用!LIST的!而不是通常的%LIST%。这个信号延迟扩展,并确保该变量被重新评估每一此命令运行时

通常 CMD 尽快扩大变量的值作为行被读取并解析。对于 CMD 单行或者是一条线,或一切的计数的一条线,这恰好是由像一个括号分隔块抱真我们在这里使用。因此,对于 CMD 完整的块是被读取并解析一次一个语句,不管多久内部的循环运行的。

如果我们会用%LIST%在这里,而不是!LIST!那么变量将被替换立刻被它的价值(在这一点上是空的)和循环将是这个样子的:

  FOR / F %%倍In(yourfile.txt)做(
    集列表=%% X

显然,这不是我们想要的。延迟扩展可以确保真正需要它的值时,变量只扩大。在这种情况下,当循环运行的内部和构造的文件名的列表。

之后变量%LIST%!LIST!(现在它并不真正的问题了其中使用)包含了文件中的行列表。

有趣的是,对于设置命令的帮助包括正是这个例子延迟扩展:


  

最后,支持延迟
  环境变量扩张
  被添加。这种支持始终是
  默认被禁用,但也可以是
  通过启用/ V命令/禁用
  行开关CMD.EXE。请参阅CMD /?


  
  

延迟环境变量扩充
  是各地越来​​越有用
  目前的扩张限制
  这发生在文本行
  阅读,而不是在执行的时候。该
  下面的示例演示
  直接变量问题
  扩展:

 组VAR =前
如果%VAR%==之前(
    组VAR =后
    如果%VAR%== @echo后,如果你看到这一点,它的工作


  
  

绝不会显示消息,因为
  在这两个IF语句中的%VAR%是
  当第一IF取代的
  声明被读取,因为它在逻辑
  包括中频,这是身体
  复合语句。因此,IF
  里面的复合语句是
  真的比较之前和之后
  它永远不会相等。同样的,
  下面的例子将无法正常工作
  预计:

 集列表=
用于%I IN(*)并设置LIST =%LIST%%我
回声%LIST%


  
  

,它不会建了一个列表
  在当前的目录中的文件,但
  而不是只会设置列表
  变量的最后一个文件中。
  再次,这是因为%LIST%是
  扩大只是一次当FOR
  语句被读出,并在该时间
  List变量是空的。所以
  实际的,因为我们正在执行循环是:

 为%i的(*)并设置LIST =%I


  
  这个循环继续将LIST到


  最后的文件中找到。


  
  

延迟环境变量扩充
  允许您使用不同的
  字符(惊叹号),以
  扩大环境变量在
  执行时间处理时间。如果延迟变量
  扩充被启用,上述
  实施例可以写成如下
  按预期工作:

 组VAR =前
如果%VAR%==之前(
    组VAR =后
    如果!VAR! == @echo后,如果你看到这一点,它的工作
)集列表=
用于%I IN(*)并设置LIST =!LIST! %一世
回声%LIST%


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.

Thanks!

解决方案

As for references, SS64.com isn't bad. Rob van der Woude gets linked fairly often, too.


As for your problem, that's easy:

@echo off
setlocal enableextensions enabledelayedexpansion

set LIST=
for /f %%x in (yourfile.txt) do (
    set LIST=!LIST! "%%x"
)

echo %LIST%

endlocal


More in-depth explanation:

setlocal enableextensions enabledelayedexpansion

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 %%x in (yourfile.txt) do (
    set LIST=!LIST! "%%x"
)

for /f iterates over lines in a file, so exactly what we need here. In each loop iteration we append the next line to the LIST 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. For cmd 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 for cmd 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:

for /f %%x in (yourfile.txt) do (
    set LIST= "%%x"
)

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:

Finally, support for delayed environment variable expansion has been added. This support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed. The following example demonstrates the problem with immediate variable expansion:

set VAR=before
if "%VAR%" == "before" (
    set VAR=after
    if "%VAR%" == "after" @echo If you see this, it worked
)

would never display the message, since the %VAR% in BOTH IF statements is substituted when the first IF statement is read, since it logically includes the body of the IF, which is a compound statement. So the IF inside the compound statement is really comparing "before" with "after" which will never be equal. Similarly, the following example will not work as expected:

set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%

in that it will NOT build up a list of files in the current directory, but instead will just set the LIST variable to the last file found. Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty. So the actual FOR loop we are executing is:

for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:

set VAR=before
if "%VAR%" == "before" (
    set VAR=after
    if "!VAR!" == "after" @echo If you see this, it worked
)

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%

这篇关于Windows XP中的批处理文件CONCAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆