2

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 :)

3
  • It can be done in bat ... but why did you chose it? Commented Nov 17, 2010 at 22:23
  • This can easily be accomplished in plain old DOS. I've done it a 1000 times if not more. I will return to this question this evening - christmas is upon us! Commented Dec 19, 2010 at 12:49
  • Can you provide a sample file so that we can see if there is any text formatting which needs to be preserved ie, blank lines, indentations etc... or can you state with certainty that this is not an issue we need concern ourselves with? Commented Dec 19, 2010 at 22:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty much what I was doing by now, fixed it myself :) - but +1 :)
0

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.

2 Comments

I do know this, the issue is however, that I cannot have a dependency as sed or awk, which otherwise would be the right thing to do ofc. - I updated the main with a bit more code, that seems to work, it now finds the 3 important lines, but how do i prefix them with a '#'?
@Skeen - can you have a dependency on PowerShell or something more capable of text processing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.