如何逐行读取文本文件并使用批处理文件修改每一行? [英] How to read a text file line by line and modify every line using a batch file?

查看:262
本文介绍了如何逐行读取文本文件并使用批处理文件修改每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个包含以下内容的文本文件:

L1
L2
L3

我想要一个批处理文件,该文件将从文件中读取每一行,然后将它们变成变量.例如,这些将是变量:

line1 = L1
line2 = L2
line3 = L3

如何使用批处理文件完成此操作?

解决方案

Windows命令处理器旨在运行命令和应用程序.它不适用于编辑文本文件.实际上,每种脚本或编程语言都比Windows命令处理器cmd.exe执行的批处理文件更好.

对于使用字符编码(每个字符一个字节)的简单文本文件示例, UTF-8 可以使用简单的批处理文件:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "LineNumber=0"
del "%TEMP%\TextFile.tmp" 2>nul

for /F "usebackq delims=" %%I in ("TextFile.txt") do (
    set /A LineNumber+=1
    echo line!LineNumber! = %%I
) >>"%TEMP%\TextFile.tmp"

if exist "%TEMP%\TextFile.tmp" move /Y "%TEMP%\TextFile.tmp" "TextFile.txt"
if errorlevel 1 del "%TEMP%\TextFile.tmp"
endlocal

但是此批处理文件无法处理正确的行

  • 不包含任何字符(空行),
  • 以分号;开头
  • 包含一个或多个感叹号!.

更好但较慢的批处理文件为:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LineNumber=0"
del "%TEMP%\TextFile.tmp" 2>nul

for /F delims^=^ eol^= %%A in ('%SystemRoot%\System32\findstr.exe /N "^" "TextFile.txt" 2^>nul') do (
    set "Line=%%A"
    set /A LineNumber+=1
    setlocal EnableDelayedExpansion
    echo line!LineNumber! = !Line:*:=!
    endlocal
) >>"%TEMP%\TextFile.tmp"

if exist "%TEMP%\TextFile.tmp" move /Y "%TEMP%\TextFile.tmp" "TextFile.txt"
if errorlevel 1 del "%TEMP%\TextFile.tmp"
endlocal

如何逐行读取和打印文本文件的内容上查看我的答案?为什么使用 FOR 循环要复杂得多,以便更好地处理文本文件.

带有 UTF-16 解决方案

The Windows command processor is designed for running commands and applications. It is not designed for editing text files. Really every scripting or programming language would be better for this task than a batch file executed by Windows command processor cmd.exe.

For your simple text file example with using a character encoding with one byte per character or UTF-8 a simple batch file can be used:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "LineNumber=0"
del "%TEMP%\TextFile.tmp" 2>nul

for /F "usebackq delims=" %%I in ("TextFile.txt") do (
    set /A LineNumber+=1
    echo line!LineNumber! = %%I
) >>"%TEMP%\TextFile.tmp"

if exist "%TEMP%\TextFile.tmp" move /Y "%TEMP%\TextFile.tmp" "TextFile.txt"
if errorlevel 1 del "%TEMP%\TextFile.tmp"
endlocal

But this batch file fails to process a line correct which

  • does not contain any character (empty line),
  • starts with a semicolon ;,
  • contains one or more exclamation marks !.

A better but slower batch file would be:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LineNumber=0"
del "%TEMP%\TextFile.tmp" 2>nul

for /F delims^=^ eol^= %%A in ('%SystemRoot%\System32\findstr.exe /N "^" "TextFile.txt" 2^>nul') do (
    set "Line=%%A"
    set /A LineNumber+=1
    setlocal EnableDelayedExpansion
    echo line!LineNumber! = !Line:*:=!
    endlocal
) >>"%TEMP%\TextFile.tmp"

if exist "%TEMP%\TextFile.tmp" move /Y "%TEMP%\TextFile.tmp" "TextFile.txt"
if errorlevel 1 del "%TEMP%\TextFile.tmp"
endlocal

See my answer on How to read and print contents of text file line by line? why the FOR loop is much more complex to process text files better.

A text file with UTF-16 or UTF-32 character encoding cannot be processed with those two batch files.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • move /?
  • set /?
  • setlocal /?

See also the Microsoft article about Using command redirection operators.

这篇关于如何逐行读取文本文件并使用批处理文件修改每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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