问题
For example, line number 2 reads: "0" (without quotes). I want to change that 0 to a 1, without changing anything else in the text file.
I know what line the values will be on, so all i need to know is how to change that value to another value that I specify. I also will have to to the same with strings, not just numbers.
Example:
From:
4.7
0
check
0
1
0
0
To:
4.7
1
check
0
1
0
0
Thanks!
回答1:
try this:
@ECHO OFF &SETLOCAL
SET "file=file"
SET /a Line#ToSearch=2
SET "Replacement=0"
(FOR /f "tokens=1*delims=:" %%a IN ('findstr /n "^" "%file%"') DO (
SET "Line=%%b"
IF %%a equ %Line#ToSearch% SET "Line=%Replacement%"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(!Line!
ENDLOCAL
))>"%file%.new"
TYPE "%file%.new"
Note: this doen't work properly for lines starting with colons :, this might be fixed if needed.
来源:https://stackoverflow.com/questions/18395013/batch-edit-specified-line-in-text-file