IF loop within FOR loop PROBLEMS! Help please

February 10, 2011 at 15:16:24
Specs: Windows XP
How's it going guys. I've got an issue with a portion of a larger batch script that I can't figure out... Try to bear with me:

My goal is to achieve recursion through the entire windows registry, and so far I've gotten relatively close with a combination of REG QUERY, some file I/O, environment variables and REG ADD.

By issuing the command:

reg query hklm /s >> output.txt

I am left with a file named output.txt containing every directory with all subdirectories and key information in the HKLM portion of the registry.

The actual key information is irrelevant to me. I need to extract just the lines containing the path (minus the key name) of the directories and subdirectories in HKLM. For this, the following works:

FOR /F "tokens=* delims=" %%a IN (output.txt) DO (
ECHO %%a | FIND "HKEY_" > NUL
IF NOT ERRORLEVEL 1 ECHO %%a >> keys.txt
)

This leaves me (after a few basic error messages that can be ignored/redirected later, not important now) with a file called keys.txt containing only the strings containing registry dir/subdir names.

Problem is, the strings left in keys.txt have a trailing space that cause problems when I try to feed the line with an appended random key name (I'm creating/immediately deleting a random key recursively throughout the registry, I know it sounds weird but I promise there's a greater goal lol) Since the only information I need from the file is the full tree name without the space, I found a way to trim trailing white spaces from SET environment variables that works well (found here!):

----------
@ECHO OFF

SET MyVar=AA AAAA
ECHO MyVar=[%MyVar%]

CALL :TRIM %MyVar%

ECHO MyVar=[%MyVar%]
SET MyVar=
GOTO :lbreak

:TRIM
SET MyVar=%*
GOTO :lbreak
:lbreak
----------

What I need to do is read in a line from output.txt, make sure it's a string containing a registry path, strip the trailing space that is left at the end of the string, then output it to keys.txt

My current attempt at combining these methods (does not work, HELP!!!):

----------
@ECHO OFF

setLocal EnableDelayedExpansion

FOR /F "tokens=* delims=" %%a IN (output.txt) DO (

ECHO %%a | FIND "HKEY_" > NUL

IF NOT ERRORLEVEL 1 (

SET REG_LOC=%%a

CALL :trimVar %REG_LOC%

SET REG_LOC=

GOTO :lbreak

:trimVar

SET REG_LOC=%*

ECHO %REG_LOC% >> keys.txt

GOTO :lbreak
)
:lbreak
)
----------

When I run this, I get "The syntax of the command is incorrect."

I hope this all made sense. Any help is greatly appreciated!!! Thanks


See More: IF loop within FOR loop PROBLEMS! Help please

Report •


#1
February 10, 2011 at 15:29:50
Your actually adding the space with echo, see the "using the echo command" part of this how to:

http://www.computing.net/howtos/sho...

That being said using pipes makes things a lot simpler:

reg query hklm /s |  FIND  /i "HKEY_" >> keys.txt

Finally the errors you come from two causes, goto's and labels don't work in side for loops. Sometimes the are used to jump out of loops but that sometimes won't even work.

The last of the errors is a variable expansion issue:

http://www.computing.net/howtos/sho...


Report •

#2
February 10, 2011 at 16:02:44
Awesome. What I attempted to do between 3 functions, two files, and a bunch of ghetto CALL/GOTO/whatever crap was fixed with 1 line. Hahah hell yeah, I feel dumb but thanks a ton man -- definitely fixed my situation.

THANK YOU


Report •
Related Solutions


Ask Question