If strings match insert newline

November 27, 2009 at 11:52:12
Specs: Windows XP
I need to check a variable %%a to determine if it matches a string. If it does match then I want to insert a newline.

The string that I'm trying to match always starts with:

SERVICE_NAME:


I have tried this, but it fails:
...for loop here....
if "%%a"=="SERVICE_NAME:" (
echo.
)else(
echo %%a
)
)


See More: If strings match insert newline

Report •


#1
November 27, 2009 at 12:14:33
Two things:

)else(

You must insert a space either side of "else" for it to be syntactically valid.

) else (

Also you have opened two brackets and closed three, I guess it's probably from the for loop though.....

[edit]
Actually it looks like all of the standard delims work(though I wouldn't suggest using "=")

if,t==t,(echo,t),else,(echo,noos!)

Standard delims={<tab>, <space>, <comma>, <equals>, <semi-colon>}
[edit]


Batch Variable how to


Report •

#2
November 27, 2009 at 12:32:29
Yep, all those items are correct in the real batch. The batch works fine, except I cannot get the string matching to work.

Report •

#3
November 27, 2009 at 12:41:11
When you say:

"The string that I'm trying to match always starts with:

SERVICE_NAME:"

Will it have other data after it?

If so there are a couple of options, you can nest another for loop to test the value, ie:

...for loop here....
    for /f "tokens=1 delims=:" %%h in ("%%a") do (
        if "%%h"=="SERVICE_NAME" (
            echo.
        ) else (
            echo %%a
        )
    )
)

Their is also the slightly lazier approach:

...for loop here....
    echo %%a|findstr /b /l "SERVICE_NAME:" > nul
    if not errorlevel 1 (
        echo.
    ) else (
        echo %%a
    )
)


Edit: I screwed up the second script, using else on &&, ick - I need sleep.....


Batch Variable how to


Report •

Related Solutions

#4
November 27, 2009 at 12:49:55
Both your examples worked fine. THANKS!!

Report •

#5
November 27, 2009 at 12:54:00
Are you sure they both worked fine??

I didn't test the second one and just realized that I screwed it up(can't use else on &&)......

Anyway the first one should work just fine.....


Batch Variable how to


Report •

#6
November 27, 2009 at 13:13:22
Ok, I only tested the first one, which worked great.

I need to perform the same task in another section of the batch. My string this time is: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

How do I check for that string in this loop?

for loop...do (
call :sub1 "%%a"
)
GOTO:EOF

:sub1
echo.%~1
GOTO:EOF


Report •

#7
November 27, 2009 at 13:46:44
Well it depends, it it the same deal about having data after it?
If it does have data after it will there be a back slash
after run?

If it has the slash, then:

:sub1
for /f "tokens=1-6 delims=\" %%a in (%1) do (
    if /i "%%a%%b%%c%%d%%e%%f"=="HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun" (
        echo %~1
    ) else (
        echo.
    )
)
GOTO :EOF

And the "find" version:

:sub1
echo %1|find /i "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" > nul
if errorlevel 1 (
    echo.
) else (
    echo %~1
)
GOTO :EOF


Batch Variable how to


Report •

#8
November 28, 2009 at 06:46:31
I tried both examples, but for some reason double entries (output) for certain lines occur.

The final output line of each individual section end with:
<SHA-1>

Is there a method to insert a new line after that text?


Report •

#9
November 28, 2009 at 14:18:50
The double-output might be due to data after the "\Run", might
try adding another token, change from:

for /f "tokens=1-6 delims=\" %%a in (%1) do (
if /i "%%a%%b%%c%%d%%e%%f"=="HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun" (
echo %~1
) else (

to:
for /f "tokens=1-7 delims="\" %%a in (%1) do (
if "%%g" equ "" (
if /i "%%a%%b%%c%%d%%e%%f"=="HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun" (
echo %~1
)
) else ( ...


<SHA-1>

Is there a method to insert a new line after that text?

this might work:
if /i "%%a" equ "<SHA-1>" echo.


Report •

#10
November 28, 2009 at 18:47:41
Can we see a few lines of this output?


Batch Variable how to


Report •

#11
November 30, 2009 at 07:03:41
Below are a couple of output entries. After more testing I now think that the best route is to insert a line break after each SHA-1 entry. This will allow for separation between entries.

I have tried this:

if /i "%%a" equ "<SHA-1>" (
echo.
) else (
echo %~1
)

Which didn't work. For some reason the code above strips off the ending ">" from the SHA-1 and MD-5 lines.


Vidalia
"C:\Program
0.01.0014.0000
c:\program files\vidalia bundle\vidalia\vidalia.
5dead54902509d554c19ff5a998be6f0 <MD5>
3fc486afdbba19f432c3a59a7fcbeb1a4a26f3a7 <SHA-1>
ATI DeviceDetect
"C:\Program
ATI Device Detection Application
ATI Technologies Inc.
9.14.0000.0001
c:\program files\ati multimedia\main\atidtct.exe
629010d4918c0a5182081a9bd205ecba <MD5>
8d78c8560f0908088ea45f360d86f13f442b08e8 <SHA-1>


Report •

#12
November 30, 2009 at 08:32:33
Presuming the output has line breaks as they are above and all you want is an extra line after each line the contains "<SHA-1>".

for /f "usebackq delims=" %%a in ("your file") do (
    >> "newfile" echo %%a
    echo "%%a" | > nul find "<SHA-1>" && >> "newfile" echo.
)


Batch Variable how to


Report •

#13
November 30, 2009 at 09:30:52
Isn't there a method for doing this without writing the output to a file, reading that file contents and then writing the results to another file?

Report •

#14
November 30, 2009 at 18:08:54
You can just echo it to the console if you like(just remove ">> "newfile""), but don't expect to merely edit a file with batch script.

for /f "usebackq delims=" %%a in ("your file") do (
    echo %%a
    echo "%%a" | > nul find "<SHA-1>" && echo.
)



Batch Variable how to


Report •


Ask Question