在批处理或VBS脚本中逐行读取文本文件?

问题描述:

我有一个文本文件,其中包含要创建的文件名列表(不包括扩展名).我想运行一个快速批处理文件命令或VBS脚本,该命令将遍历列表,然后根据名称创建文件.

I have a text file that contains a list of filenames, minus the extension, that I need to create. I want to run a quick batch file command or VBS script that will iterate through the list and then create a file based on the name.

文本文件看起来像这样:

The text file looks something like this:

PRXI0000466
PRXI0000564
PRXI0000636
PRXI0000681
PRXI0001092

所以我想遍历每一行,然后执行"echo . > %file%.txt"(假设%file%包含文本文件中的行).

So I want to loop through each line, then do an "echo . > %file%.txt" (assuming %file% contains the line from the text file).

有人可以告诉我一种快速的方法吗?

Can anyone show me a quick way to do this?

好吧,这是cmd中的单行代码:

Well, this is a one-liner in cmd:

for /f %i in (textfile.txt) do @echo . > %i.txt

for /f在文本文件中逐行循环,并在此过程中进行标记化.幸运的是,您的行中没有空格,否则在该行中也需要类似"delims="的内容.

for /f loops line-wise hrough a text file, and doing tokenizing on the way as well. Luckily for you there are no spaces in your lines, otherwise this would have called for something like "delims=" on there as well.

这将在每个新文件中放置一行包含点的单行,后跟一个空格.如果只想空行,则必须使用echo.>%i.txt.如果只想创建每个文件(UNIX极客可能只使用touch(1)),则可以使用copy NUL %i.txt.如果要在批处理文件中使用此文件(而不是直接从命令行使用),则必须使用%%i而不是%i.

This would put a single line containing a dot follows by a space in each new file. If you just want a blank line you'd have to use echo.>%i.txt. If you just want to create each file (UNIX geeks might just use touch(1)) you can use copy NUL %i.txt. If you want to use this in a batch file (and not directly from the command line) then you'd have to use %%i instead of %i.

我认为您真的想创建空文件,因此以下方法可以解决问题:

I think you really want to create empty files, so the following should do the trick:

for /f %i in (textfile.txt) do @copy nul %i.txt