开发者

String processing using Batch Script

开发者 https://www.devze.com 2023-01-26 03:39 出处:网络
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 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开发者_如何学Go
    )
    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.


Without a better understanding of what you want inside your loop or what your CMakeLists.txt file looks like, try this on for starters:

FINDSTR "SOMETHING" %%J && ECHO #%%J || ECHO %%J

The && makes the second command (the ECHO) conditional on the first command exiting without an error state, and the || is like a logical OR and it runs when the first one doesn't.

Really, for modifying the internals of a text file you are probably going to be much better off using either sed or awk - win32 binaries can be found in the UnxUtils project.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号