Jc Meyer
Jc Meyer

Reputation: 17

Combine add to registry with if-else

I cannot use VB Script and I have to accomplish this task in one single .bat file. Can you please help me run both items simultaneously?

  1. add registry key command
  2. carry out an IF statement and commands

Add to registry:

REGEDIT4

;@start regedit /s "%~f0"&goto :eof

[HKEY_LOCAL_MACHINE\Software\TEST]

Carry out my task:

:CheckOS

IF EXIST "%PROGRAMFILES(X86)%" (GOTO 64BIT) ELSE (GOTO 32BIT)

:64BIT

setlocal ENABLEDELAYEDEXPANSION

set filein="c:\Program Files (x86)\Premier MDC\mdt.ini"

set fileout="c:\Program Files (x86)\Premier MDC\mdtnew.ini"

set fileold="c:\Program Files (x86)\Premier MDC\mdtold.ini"

set old=10.17.8.64

set new=10.253.0.1

for /f "tokens=* delims=¶" %%i in ( '"type %filein%"') do (

set str=%%i

set str=!str:%old%=%new%!

echo !str! >> %fileout%)

move %filein% %fileold%

move %fileout% %filein%

GOTO END

:32BIT

setlocal ENABLEDELAYEDEXPANSION

set filein="c:\Program Files\Premier MDC\mdt.ini"

set fileout="c:\Program Files\Premier MDC\mdtnew.ini"

set fileold="c:\Program Files\Premier MDC\mdtold.ini"

set old=10.17.8.64

set new=10.253.0.1

for /f "tokens=* delims=¶" %%i in ( '"type %filein%"') do (

set str=%%i

set str=!str:%old%=%new%!

echo !str! >> %fileout%)

move %filein% %fileold%

move %fileout% %filein%

GOTO END

:END

I have tested these independently from each other in seperate .bat files and they work perfectly. I just need help combining them into one file and have it run both tasks. Thanks.

Upvotes: 0

Views: 878

Answers (1)

RGuggisberg
RGuggisberg

Reputation: 4750

In addition to changes suggested by @xOxxOm... You have a lot of duplicated code. This is difficult to maintain because any changes need to be made in 2 places. Change to something like this:

set "PF=%ProgramFiles%
if exist "%ProgramFiles(x86)%" set "PF=%ProgramFiles(x86)%"
setlocal ENABLEDELAYEDEXPANSION
set filein="%PF%\Premier MDC\mdt.ini"
set fileout="%PF%\Premier MDC\mdtnew.ini"
set fileold="%PF%\Premier MDC\mdtold.ini"
. . . (one set of your duplicated code here)

Upvotes: 1

Related Questions