批处理 - 编辑文本文件中指定行 [英] Batch - edit specified line in text file

查看:415
本文介绍了批处理 - 编辑文本文件中指定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,第2行写着:0(不带引号)。我想改变这种状况0到1,而无需在文本文件中其他任何变动。

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.

例如:

从:

4.7
0
check
0
1
0
0

要:

4.7
1
check
0
1
0
0

谢谢!

推荐答案

试试这个:

@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"

注:开始用冒号线这doen't正常工作,这可能是如果需要固定

Note: this doen't work properly for lines starting with colons :, this might be fixed if needed.

这篇关于批处理 - 编辑文本文件中指定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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