使用CMD逐行读取文本文件并复制到另一个文件并进行一些更改

问题描述 投票:0回答:1

我正在使用以下批处理脚本逐行读取文本文件,根据某些条件进行一些更改,并将结果输出到新文件。

@echo off
set source=mysourcefile.txt

type NUL > new_%source%

FOR /f "usebackq delims=" %%b IN (".\%source%") DO (
 REM make some changes to the line and output the result to the new file
 set "outline=%%b"
 REM echo %outline% >> new_%source%
 echo %outline%>>new_%source%
)

问题是当我使用比较工具比较新旧文件时,我看到行结尾不同,并且新文件在每行末尾都有一个额外的空格。

如何解决这个问题?

更新: 我修复了 echo 语句以删除空格。但是,我注意到新文件中没有创建空行。如何解决这个问题?

windows batch-file cmd
1个回答
0
投票

以下脚本将解决该问题:

@echo off
set source=mysourcefile.txt

type NUL > new_%source%

FOR /f "delims=" %%l IN ('findstr /N "^" ".\%source%"') DO (
 set "line=%%l"
 set "line=!line:*:=!"
 echo(!line!>>new_%source%
)

我们需要使用“findstr”命令在输出中添加行号前缀,然后从包括该列的输出中删除行号。

© www.soinside.com 2019 - 2024. All rights reserved.