Jump to content

  • Log In with Google      Sign In   
  • Create Account


BASICFreak

Member Since 07 May 2006
Offline Last Active Jun 15 2014 10:11 PM

#4860159 First complex BATCH file

Posted by BASICFreak on 10 September 2011 - 06:10 PM

This was my first time to use any commands in a BATCH file, I never thought it could get so confusing.

It took me 13 whole hours of trial and error and research.
I couldn't find a tool to do exactly what I wanted and figure it may be of use to others.

The script has no output while running, and can take a very long time to process depending on your registry size

FILE NAME = REGKEYEXTRACTOR.CMD


@ECHO OFF
setlocal enableextensions enabledelayedexpansion
COLOR 1F
CLS
SET ARGS=0
SET OPTS=0
SET OPTIONS=
FOR %%x IN (%*) DO SET /A ARGS+=1
IF %ARGS% LSS 1 GOTO ERROR
IF "%1" == "" GOTO ERROR
IF "%1" == "/?" GOTO ERROR
SET RtKy=%1
SHIFT
IF NOT X%1X==XX (
	SET OPTIONS=%1
	SET OPTION_ALL=%1
	SET /A OPTS+=1
)
IF NOT X%OPTIONS%X==XX GOTO LOOP
:LOOP
SHIFT
IF X%1X==XX GOTO ENDLOOP
SET OPTIONS=%OPTIONS% %1
SET OPTION_ALL=%OPTION_ALL%~%1
SET /A OPTS+=1
GOTO LOOP
:ENDLOOP
IF %OPTS% GEQ 1 GOTO GETOPTIONS
GOTO :EOF

:GETOPTIONS
FOR /F "USEBACKQ TOKENS=1-%OPTS% DELIMS=~" %%A IN ('%OPTION_ALL%') DO (
IF %OPTS% GEQ 1 SET OPTION_1=%%A
IF %OPTS% GEQ 2 SET OPTION_2=%%B
IF %OPTS% GEQ 3 SET OPTION_3=%%C
IF %OPTS% GEQ 4 SET OPTION_4=%%D
IF %OPTS% GEQ 5 SET OPTION_5=%%E
IF %OPTS% GEQ 6 SET OPTION_6=%%F
IF %OPTS% GEQ 7 SET OPTION_7=%%G
IF %OPTS% GEQ 8 SET OPTION_8=%%H
IF %OPTS% GEQ 9 SET OPTION_9=%%I
IF %OPTS% GEQ 10 SET OPTION_10=%%J
IF %OPTS% GEQ 11 SET OPTION_11=%%K
IF %OPTS% GEQ 12 SET OPTION_12=%%L
IF %OPTS% GEQ 13 SET OPTION_13=%%M
IF %OPTS% GEQ 14 SET OPTION_14=%%N
IF %OPTS% GEQ 15 SET OPTION_15=%%O
IF %OPTS% GEQ 16 SET OPTION_16=%%P
IF %OPTS% GEQ 17 SET OPTION_17=%%Q
IF %OPTS% GEQ 18 SET OPTION_18=%%R
IF %OPTS% GEQ 19 SET OPTION_19=%%S
IF %OPTS% GEQ 20 SET OPTION_20=%%T
)
IF "%OPTION_1%"=="/I" (
	IF %OPTS% EQU 2 (
		IF NOT X%OPTION_2%X==XX (
			SET INFILE=%OPTION_2%
			GOTO INPUTTEXT
		) ELSE (
			GOTO ERROR
		)
	) ELSE (
		GOTO ERROR
	)
)ELSE IF "%OPTION_1%"=="/i" (
	IF %OPTS% EQU 2 (
		IF NOT X%OPTION_2%X==XX (
			SET INFILE=%OPTION_2%
			GOTO INPUTTEXT
		) ELSE (
			GOTO ERROR
		)
	) ELSE (
		GOTO ERROR
	)
) ELSE (
	GOTO QUERY
)
GOTO :EOF


:INPUTTEXT
FINDSTR /B /C:"HKEY" %INFILE% > TEMP.TXT
ECHO Windows Registry Editor Version 5.00 > %RtKy%-FINAL.REG
ECHO. >> %RtKy%-FINAL.REG
FOR /F "TOKENS=1* DELIMS=\" %%A IN (TEMP.TXT) DO (
REG EXPORT "%RtKy%\%%B" TMP.REG /Y
TYPE TMP.REG > TEMP.REG
DEL TMP.REG /Q
FOR /F "SKIP=2 TOKENS=*" %%a IN (TEMP.REG) DO @ECHO %%a >> %RtKy%-FINAL.REG
DEL TEMP.REG /Q
)
DEL TEMP.TXT
GOTO :EOF

:QUERY
REG QUERY %RtKY% %OPTIONS% > QUERYTEMP.TXT
SET INFILE=QUERYTEMP.TXT
GOTO INPUTTEXT
GOTO :EOF

:ERROR
ECHO REGKEYEXTRACTOR.CMD
ECHO By Brian T. Hoover A.K.A. BASIC.Freak
ECHO.
ECHO USSAGE  REGKEYEXTRACTOR.CMD RootKey [OPTIONS]
ECHO EXAMPLE REGKEYEXTRACTOR.CMD HKLM /S /F "FOO bar" /Z
ECHO     	REGKEYEXTRACTOR.CMD HKLM /I hklm.TXT
ECHO.
ECHO RootKey - [ HKLM ^| HKCU ^| HKCR ^| HKU ^| HKCC ]
ECHO.
ECHO [OPTIONS]    -- WARNING DO NOT USE A '~' IN OPTIONS! --
ECHO /I "InputFile"  -  USE A PRE-OUTPUTED REG QURY FILE
ECHO.
ECHO   ----------------------------  OR  ----------------------------
ECHO.
ECHO /S      Queries all subkeys and values recursively (like dir /s).
ECHO.
ECHO /se 	Specifies the separator (length of 1 character only) in
ECHO     	data string for REG_MULTI_SZ. Defaults to "\0" as the separator.
ECHO.
ECHO /f      Specifies the data or pattern to search for.
ECHO     	Use double quotes if a string contains spaces. Default is "*".
ECHO.
ECHO /k      Specifies to search in key names only.
ECHO.
ECHO /d      Specifies the search in data only.
ECHO.
ECHO /c      Specifies that the search is case sensitive.
ECHO     	The default search is case insensitive.
ECHO.
ECHO /e      Specifies to return only exact matches.
ECHO     	By default all the matches are returned.
ECHO.
ECHO /t      Specifies registry value data type.
ECHO     	Valid types are:
ECHO       	REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ,
ECHO       	REG_DWORD, REG_QWORD, REG_BINARY, REG_NONE
ECHO     	Defaults to all types.
ECHO.
ECHO /z      Verbose: Shows the numeric equivalent for the type of the valuename.
GOTO :EOF



enjoy Posted Image


PARTNERS