使用批处理脚本处理字符串 [英] String processing using Batch Script
问题描述
我目前正在创建一个批处理脚本,它必须循环遍历文件中的行,检查一些字符串,以及如果匹配前缀字符串与一个#(注释掉)。
我完全不熟悉批处理脚本,所有这一切都是:
如果%% j==Extensions_AntTweakBar(
echo lol1
)
if(%% j(CMakeLists.txt) j==Extensions_Inspection(
echo lol2
)
if%% j==Extensions_InspectionBar(
echo lol3
)
)
所以我现在的问题是,我不知道如何在批处理脚本中对字符串进行操作。如果有人能够帮助我,那将不胜感激:)
你可以使用你想追加的文本,然后是你的变量一般。
C:\> set MY_VAR = Hello world!
C:\> echo#%MY_VAR%
#Hello world!
C:\> set MY_VAR =#%MY_VAR%
C:\> echo%MY_VAR%
#Hello world!
如果你只是在做echo,没关系。 echo#%% j
会做你所需要的。
但是如果你想把行设置成一个变量,您必须启用延期扩展。将 setlocal ENABLEDELAYEDEXPANSION
添加到文件顶部,然后用!
而不是 %
。例如(并注意到我已经添加了 delims =
来将整行放在 %% j
中,而不是第一个字就行了):
@echo off
setlocal ENABLEDELAYEDEXPANSION
set LINE =
for / fdelims =%% j in(CMakeLists.txt)do(
set LINE = %% j
if%% j==扩展名AntTweakBar(
set LINE =#%% j
)
if%% j==Extensions Inspection(
set LINE =#%% j
如果%% j==Extensions InspectionBar(
set LINE =#%% j
)
echo!LINE!
给定这个输入文件:
扩展AntTweakBar
一些文本
扩展检查
扩展什么?
更多文本
扩展InspectionBar
扩展InspectionBar这行不匹配,因为delims =取得所有文本
甚至更多文本
上面的脚本产生这个输出:
C: \> comment.bat
#Extensions AntTweakBar
某些文本
#Extensions检查
扩展什么?
更多的文本
#Extensions InspectionBar
扩展InspectionBar这行不匹配,因为delims =需要所有的文本
甚至更多的文本
当然,删除 @echo off
会帮助您调试问题。
但是,所有这些都说明了,你正在使用批处理字符串处理能达到的极限。如果您仍想使用批处理命令,则可能需要开始将行写入临时文件,并使用 findstr
和正则表达式。
I'm currently creating a batch script that has to loop through the lines in a file, checking for some string, and if theres a match prefix that string with a '#' (comment it out).
I'm perfectly new to batch script, all I got this far is:
for /f %%j in (CMakeLists.txt) do (
if "%%j"=="Extensions_AntTweakBar" (
echo lol1
)
if "%%j"=="Extensions_Inspection" (
echo lol2
)
if "%%j"=="Extensions_InspectionBar" (
echo lol3
)
)
So my current issue is, I don't know how to operate on string within batch scripts. If someone could help me out that would be appreciated :)
解决方案 You can just use the text you want to append followed by your variable generally.
C:\>set MY_VAR=Hello world!
C:\>echo #%MY_VAR%
#Hello world!
C:\>set MY_VAR=#%MY_VAR%
C:\>echo %MY_VAR%
#Hello world!
If you're just doing echo, that's fine. echo #%%j
will do what you need.
But if you want to set the line to a variable, you have to enable delayed expansion. Add setlocal ENABLEDELAYEDEXPANSION
to the top of your file and then surround your variables with !
instead of %
. For example (and notice that I've added delims=
to put the entire line in %%j
instead of the first word on the line):
@echo off
setlocal ENABLEDELAYEDEXPANSION
set LINE=
for /f "delims=" %%j in (CMakeLists.txt) do (
set LINE=%%j
if "%%j"=="Extensions AntTweakBar" (
set LINE=#%%j
)
if "%%j"=="Extensions Inspection" (
set LINE=#%%j
)
if "%%j"=="Extensions InspectionBar" (
set LINE=#%%j
)
echo !LINE!
)
Given this input file:
Extensions AntTweakBar
some text
Extensions Inspection
Extensions What?
some more text
Extensions InspectionBar
Extensions InspectionBar this line doesn't match because delims= takes all text
even more text
The above script produces this output:
C:\>comment.bat
#Extensions AntTweakBar
some text
#Extensions Inspection
Extensions What?
some more text
#Extensions InspectionBar
Extensions InspectionBar this line doesn't match because delims= takes all text
even more text
And of course removing @echo off
will help you debug problems.
But all that being said, you're about at the limit of what you can accomplish with batch string processing. If you still want to use batch commands, you may need to start writing lines to temporary files and using findstr
with a regex.
这篇关于使用批处理脚本处理字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!