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

时间:2022-05-25 23:31:19

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.

我有一个文本文件,其中包含我需要创建的文件名列表,减去扩展名。我想运行一个快速批处理文件命令或VBS脚本,它将迭代列表,然后根据名称创建一个文件。

The text file looks something like this:

文本文件看起来像这样:

PRXI0000466
PRXI0000564
PRXI0000636
PRXI0000681
PRXI0001092

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

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

Can anyone show me a quick way to do this?

有人能告诉我一个快速的方法吗?

3 个解决方案

#1


Well, this is a one-liner in cmd:

嗯,这是cmd中的单线程:

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

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.

for / f循环遍历文本文件,并在路上进行标记。幸运的是,你的行中没有空格,否则就会在那里调用类似“delims =”的东西。

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.

这将使包含点的单行跟在每个新文件中的空格之后。如果你只想要一个空白行,你必须使用echo。>%i.txt。如果您只想创建每个文件(UNIX geeks可能只使用touch(1)),您可以使用copy NUL%i.txt。如果要在批处理文件中使用它(而不是直接从命令行),那么你必须使用%% i而不是%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

#2


Are you just trying to create empty text files? If so, this .vbs script will do it

你只是想创建空文本文件吗?如果是这样,这个.vbs脚本就会这样做

Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile("list.txt")
do while not listFile.AtEndOfStream 
    fName =  listFile.ReadLine()
    fso.CreateTextFile(fName + ".txt")
loop

#3


The text file t.txt contains:

文本文件t.txt包含:

PRXI0000466
PRXI0000564
PRXI0000636
PRXI0000681
PRXI0001092

This loop should solve your problem.

这个循环应该解决你的问题。

for /f %%a in (t.txt) do %%a > %%a.txt

#1


Well, this is a one-liner in cmd:

嗯,这是cmd中的单线程:

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

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.

for / f循环遍历文本文件,并在路上进行标记。幸运的是,你的行中没有空格,否则就会在那里调用类似“delims =”的东西。

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.

这将使包含点的单行跟在每个新文件中的空格之后。如果你只想要一个空白行,你必须使用echo。>%i.txt。如果您只想创建每个文件(UNIX geeks可能只使用touch(1)),您可以使用copy NUL%i.txt。如果要在批处理文件中使用它(而不是直接从命令行),那么你必须使用%% i而不是%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

#2


Are you just trying to create empty text files? If so, this .vbs script will do it

你只是想创建空文本文件吗?如果是这样,这个.vbs脚本就会这样做

Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile("list.txt")
do while not listFile.AtEndOfStream 
    fName =  listFile.ReadLine()
    fso.CreateTextFile(fName + ".txt")
loop

#3


The text file t.txt contains:

文本文件t.txt包含:

PRXI0000466
PRXI0000564
PRXI0000636
PRXI0000681
PRXI0001092

This loop should solve your problem.

这个循环应该解决你的问题。

for /f %%a in (t.txt) do %%a > %%a.txt