Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

How to read a text file line by line and modify every line using a batch file?

Let's say there's a text file that contains:

L1
L2
L3

I want to have a batch file that will read each line from that file, and then make them into variables. For example, these will be the variables:

line1 = L1
line2 = L2
line3 = L3

How can this be done with a batch file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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%System32findstr.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...