文本编辑批处理文件忽略换行符和以“;”开头的行 [英] text editing batch file ignors newlines and rows starting with ";"

查看:202
本文介绍了文本编辑批处理文件忽略换行符和以“;”开头的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试编写一个批处理窗口,以便以下一种方式编辑文本文件:



在[Server]标题的末尾添加Value = 2,如果Value = *存在,请将其替换为上面提到的。

so一个看起来像这样的文件:

----------- file.txt ----------

;评论

;评论



[用户]

Bla = a



[服务器]

bla = a

价值= 1

bla2 = b

-----------------------------

将会是这样的:

----------- file.txt ----------

;评论

;评论



[用户]

bla = a



[服务器]

bla = a

bla2 = b

价值= 2

--------------- -------------



到目前为止我已经接近了:



Hi all,

I am trying to script a batch for windows to edit a text file in the next manner:

add "Value=2" at the end of a "[Server]" header, and if "Value=*" exists, replace it with the mentioned above.
so that a file that looks like this:
-----------file.txt----------
;comment
;comment

[User]
Bla=a

[Server]
bla=a
Value=1
bla2=b
-----------------------------
will turn out like this:
-----------file.txt----------
;comment
;comment

[User]
bla=a

[Server]
bla=a
bla2=b
Value=2
----------------------------

so far I have gotten close, with:

Quote:



@echo off

SETLOCAL = ENABLEDELAYEDEXPANSION



for / f %% a in(file.txt)do(

if NOT% %a!==!值= 1 echo %% a>> file.txt.tmp

if %% a == [Server] echo .Value = 2>> file.txt.tmp

if %% a ==echo。)

del file.txt

rename file.txt.tmp file.txt


@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION

for /f %%a in (file.txt) do (
if NOT %%a!==!Value=1 echo %%a >> file.txt.tmp
if %%a==[Server] echo .Value=2 >> file.txt.tmp
if %%a=="" echo.)
del file.txt
rename file.txt.tmp file.txt







这不够好,因为它只会忽略Value = 1而不是所有行以Value =开头,它在[Server]下方插入Value = 2,这不是我想要的,但我遇到的两个主要问题是:



1.由于某种原因,它会忽略以;开头的注释行。

2.它忽略了换行符,并将它们混合在一起。 (我写的最后一行并不能解决这个问题......:\)



似乎是一个非常简单的任务,但它这是我的第一个文本编辑批次。任何脚本大师都足以提供一些帮助吗?



thx,

Ron。




that''s not really good enough because it only ignores "Value=1" and not all lines starting with "Value=" and it inserts "Value=2" right beneath "[Server]" which is not what I wanted, but the 2 main problems I have are:

1. it, for some reason, ignores the comment lines starting with ";"
2. it ignores newlines, and huddles it all together. (the last line i''ve written doesn''t work to solve that problem... :\)

seems like a pretty simple task, but it''s my first text editing batch ever. any scripting masters kind enough to offer some assistance?

thx,
Ron.

推荐答案

尝试这个,它远非完美,但似乎做了你想要的大部分。

Try this, it''s far from perfect but seems to do most of what you want.
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION

set val2="N"
for /f "eol=^" %%a in (file.txt) do (
if NOT %%a!==!Value=1 echo %%a >> file.txt.tmp
if %%a==[Server] set val2="Y"
if %%a=="" echo. >> file.txt.tmp
)
if %val2% == "Y" echo Value=2 >> file.txt.tmp
del file.txt
rename file.txt.tmp file.txt



eol = ^阻止解析器将注释行(即以分号开头的那些行)视为空格。我认为行如果%% a ==echo。 >> file.txt.tmp 是多余的,因为文件解析器不返回空白或注释行中的项目。


The "eol=^" stops the parser from treating comment lines (i.e. those beginning with a semi-colon) as blanks. I think the line if %%a=="" echo. >> file.txt.tmp is redundant as the file parser does not return items in blank or comment lines.


这篇关于文本编辑批处理文件忽略换行符和以“;”开头的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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