0

我需要替换目录中多个文件的空行。我可以为单个文件执行此操作,但我无法为文件夹中的多个文件执行此操作。

这是适用于单个文件的代码

@echo off
for /F "tokens=* delims=" %%A in (input.txt) do echo %%A >> output.txt

请帮助我,因为我对批处理编程完全陌生

4

1 回答 1

1

感谢您发布这行代码,我正在寻找它,并且有点急于自己推理它:)

要将其用于一系列文件,您可以执行以下操作:(您可以将整个代码复制到单个批处理文件中)

:: Say you have several files named Input1.txt, Input2.txt, Input3.txt, etc 
:: this will call a subroutine within the same batch file, called :Strip
:: using each file as parameter:

for %%A in ("input*.txt") do call :Strip %%A
Goto End

:Strip
:: The subroutine starts here
:: First we take the name of the input file and use it to generate
:: the name of an output file, Input1.txt would output to output_(Input1).txt, etc
For %%x in (%*) do set OutF=output_(%%~nx).txt

:: I now erase the output file it it already exists, so if you run this twice
:: it won't duplicate output
del %OutF%

:: Now comes the line you already supplied
for /F "tokens=* delims=" %%B in (%*) do echo %%B >> %OutF%

:: and now we return from the subroutine
Goto :EOF

:End
于 2013-01-13T19:17:12.573 回答