We help IT Professionals succeed at work.

Need Batch File (.bat) to run RegEdit.exe ... with results.

I need a .BAT file to run the following command:

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg"

This will add our required Trusted Locations for Access 2010.  No problem here.  Works fine.

However, notice the /s for Silent. I want to minimize  the number of messages the user sees, especially about 'modifying the Registry'.

BUT ... If the operation should fail for any reason, a message needs to be displayed to that effect.
Conversely, if operation is successful, a message is displayed to that effect.

So, in the end ... there is only one message ... success/no success.

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg"
If <error> then
    message "Error ..."
Else
   message "Success"
End

Hopefully there is a way to grab an error code from regedit.exe (or reg.exe?) if a problem occurs?

Also ... ideally this .bat file will become an .exe.  Supposedly, this can be done by using the  utiltily at  This Site

Has anyone used this and/or can you recommend one that you know for SURE works and is legitiment ?

thx.mx
Comment
Watch Question

Commented:
Try 'reg add /f' instead
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
I need the full bat file ... all commands.  I have not written a bat file before.

mx
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Give this a try:

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg" || (
    echo "Error ..."
) && (
   echo "Success"
)

Open in new window

~bp
Distinguished Expert 2019

Commented:
Are you using this as a GPO login script or a GPO startup script?

It all depends on which settings are being altered.

Another option might be to push the registry keys with a GPO.
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
You can use the %ERRORLEVEL% variable to create the if ... construction. Basically, like this:
@echo off
regedit.exe /s "\\SomePath\A2010TrustedLoc.reg"
IF %ERRORLEVEL% neq 0 (
    echo Something went wrong
) ELSE (
    echo Everything OK
)

Open in new window


This way your output would just be text in a command window, would this work for you or do you want another way of displaying the output?
erobbySenior Systems Engineer

Commented:
Something like this?

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg"
set errlvl==%errorlevel%
If %errlvl%==0 (
       Echo Successful
) else (
       echo The process did not complete with the following error code:  %errlvl%
)
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
@arnold:

"Are you using this as a GPO login script or a GPO startup script?"

No. Users will get an email and they will double click on the .bat (or .exe) file ... Done!

@gerwinjansen:
"This way your output would just be text in a command window, would this work for you or do you want another way of displaying the output? "

Preferrably *not* in the command window ... easy for user to miss.

FYI ... sorry, I have a meeting for a 1-2 hrs, but I WILL be back ...

thx.mx
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
You could create a simple popup using wscript, like this for example: (basic idea)
echo WScript.Echo ("Something went wrong") > %TEMP%\msg.vbs 
wscript %TEMP%\msg.vbs
del %TEMP%\msg.vbs

Open in new window

Distinguished Expert 2019

Commented:
Many anti-virus filter on email system will capture/quaranteen .bat, vbs, scr, etc  files send through them.

Depending on the system to which this is being done, windows security may prevent this from running as well.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
ok .. I'm back.
@arnold: ...  RE: "antivirus ... ".
That will not be an issue.

OK ... I *should* have mentioned that the Success/No Success message needs to be a regular Windows popup message, not something that appears in the command window (sorry).
And the user needs to click OK in either case. And it really should be system modal such that the user MUST click OK before doing anything else.  
And for that matter, ideally you would never even see the command window, although not a big deal

That being said ...

@gerwinjansen >> 
echo WScript.Echo ("Something went wrong") > %TEMP%\msg.vbs
wscript %TEMP%\msg.vbs
del %TEMP%\msg.vbs


Well, that does pop up a message, but .... it comes up even when successful. So, how do you tie that  into this paradigm:

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg"
If <error> then
    Windows popup message "Error ..."
Else
  Windows popup message "Success"
End






Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
This should put the two things together.

@echo off

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg" || (
    call :ShowMsg "Error ..."
) && (
   call :ShowMsg "Success"
)
exit /b

:ShowMsg [message]
  echo WScript.Echo ("%~1") >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\msg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b

Open in new window

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
@billprew:
Well, that does not show a message either way ... success or failure (if I purposely put in say a bad path)

All I see is the command window flash for a moment.

?

mx
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
To see if you are getting an error put the following line before the first "exit /b"

pause

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Forcing an error with a bogus file name ...

No error showing in Command window ..

regedit.exe /s "\\SomePath\BogusA2010TrustedLoc.reg" || (
    call :ShowMsg "Error ..."

just the pause and "Press any key to continue ..."
But I can't find the 'any' key (just kidding!)

My suspicion is - based on a couple of other ways I've tried is ... regedit is not generating a trappable error ... ?  But there must ... be a way ...


Note that with this - I do get Good To Go - using good path/file name. But if I force an error, I get no message ...

regedit.exe /s "\\SomePath\A2010TrustedLoc.reg"    
set errlvl==%errorlevel%
If %errlvl%==0 goto success

:success
    echo WScript.Echo ("Good To Go!") > %TEMP%\msg.vbs
    wscript %TEMP%\msg.vbs
       
goto cleanup      
 
:failure
    echo WScript.Echo ("Something went wrong") > %TEMP%\msg.vbs
    wscript %TEMP%\msg.vbs
       
:cleanup
del %TEMP%\msg.vbs

mx
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
From what I can see ...  set errlvl==%errorlevel%  is not really working with RegEdit.

That variable is always zero ( 0), even if I force an error ...

Isn't there also a Reg.exe .... ?

mx
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, this seems to work better.

@echo off

set RegFile=\\SomePath\A2010TrustedLoc.reg

regedit.exe /s "%RegFile%"
if %ERRORLEVEL% EQU 0 (
  call :ShowMsg "Success"
) else (
  call :ShowMsg "Error ..."
)
exit /b

:ShowMsg [message]
  echo WScript.Echo ("%~1") >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\_showmsg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b

Open in new window

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Sadly, same result.

Bill ... try with this:

set RegFile=\\SomeBOGUSPath\A2010TrustedLoc.reg

I still get the Success message ...

mx
Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
Okay, give this a try, probably a better command for a BAT script usage anyway.

@echo off

set RegFile=\\SomePath\A2010TrustedLoc.reg

reg import "%RegFile%" >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
  call :ShowMsg "Success"
) else (
  call :ShowMsg "Error ..."
)
exit /b

:ShowMsg [message]
  echo WScript.Echo ("%~1") >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\_showmsg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b

Open in new window

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
ok ... that works. cool.

For the error case, how can we display the error code.  I tried

Call :ShowMsg "Error ... " & %ERRORLEVEL%
Call :ShowMsg "Error ... " & "%ERRORLEVEL%"

no work - does not display error number.

Also, so ... what is this version doing ... re 'import'?
\mx
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Here's a way to report the error, but it's just a number, not too meaningful.

I didn't understand the other part of your last post, about the "import" version?

@echo off

set RegFile=\\SomePath\A2010TrustedLoc.reg

reg import "%RegFile%" >NUL 2>&1
set SaveError=%ERRORLEVEL%
if %SaveError% EQU 0 (
  call :ShowMsg "Success"
) else (
  call :ShowMsg "Error [%SaveError%]"
)
exit /b

:ShowMsg [message]
  echo WScript.Echo ("%~1") >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\_showmsg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b

Open in new window

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Yeah ... I see the code seems to be either zero or one (0,1). So, right ... not really useful.  I'm not really too worried about errors occurring.  That s/b rare.

RE "Import" I meant

set RegFile=\\SomePath\A2010TrustedLoc.reg

reg import "%RegFile%" >NUL 2>&1

What made this work, whereas the others did not.  What is Import doing ?

And one last thing if you don't mind ... re in my Q:

Also ... ideally this .bat file will become an .exe.  Supposedly, this can be done by using the utiltily at  This Site  or similar.

Has anyone used this and/or can you recommend one that you know for SURE works and is legitiment ?
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Thanks for hanging with this Bill.

mx
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
btw ... score s/b 10 ...  system just doesn't see it that way :-(

mx
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
The REGEDIT command that you/we were using originally didn't seem to have useful error returns in a silent command line mode.  That program is really designed for a GUI interface to the registry anyway.

REG on the other hand is simply a command line tool for getting info into and out of the registry, and seems to report errors better.  The IMPORT option of that command is what tells it to load the contents os a REG text file in.  You can see the options of the REG command by doing this at a command line:

REG /?
REG IMPORT /?

I don't have much experience with converting BAT scripts to EXE files, there are certainly tools out there that do that.  The more respectable tools are likely not free, such as:

http://www.scriptcode.com/
http://www.itripoli.com/
http://www.sapien.com/software/primalscript

But here's another free one to take a look at:

http://www.f2ko.de/programs.php?lang=en&pid=b2e

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Damn ... I didn't EVEN notice we were not using RegEdit ... duh

reg import "%RegFile%" >NUL 2>&1

And before your last (working post), I was looking at REG ... trying to see how to use it with a Reg file ...
And at the same time, I found something that said that RegEdit does *not* return an error code in Silent mode.

Anyway, thanks again.

mx
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Bill fyi ... the F2KO Batch to Exe works very well.

mx
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Great, glad that helped.

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Is there a way to change the default title from Windows Script Host to MyTitle ... in the code snipped you provided ?

mx
Capture1.gif
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
No "easy" way to do this, you would have to launch this VBS from either another BAT file, or another VBS, and set the title there.  I'm pretty sure there's no way to change the title of the running script by itself.

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Yeah, been looking around on WSH stuff ... not seeing much.
thx
Gerwin Jansen, EE MVETopic Advisor
Most Valuable Expert 2016

Commented:
message.vbs:
MsgBox "Contents of the popup message", 0, "Title of the popup message"

Open in new window

wscript message.vbs
 popup
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
ok.  but how would you tie that into the code @ http:#a37229591 ?

mx
Distinguished Expert 2019

Commented:
You would create the example message.vbs and then use it

from http:#a37247044


message.vbs

MsgBox $message, 0, $title
but I have not coded one to provide as an example
http://www.powershell.nu/2009/12/16/running-scripts-with-arguments-in-powershell/ might help in that.

in http:#a37229591

replace the showmesg and call this message.vbs "message" "title"
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Props to gerwinjansen for the idea, try this.

@echo off

set RegFile=\\SomePath\A2010TrustedLoc.reg

reg import "%RegFile%" >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
  call :ShowMsg "Success" "My Title"
) else (
  call :ShowMsg "Error ..." "My Title"
)
exit /b

:ShowMsg [message] [title]
  echo MsgBox "%~1", vbOkOnly, "%~2" >"%TEMP%\_showmsg.vbs"
  if exist "%TEMP%\_showmsg.vbs" (
    wscript //nologo "%TEMP%\_showmsg.vbs"
    del "%TEMP%\_showmsg.vbs"
  )
  exit /b

Open in new window

~bp
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Author

Commented:
Need I say more ...

thanks guys. I owe you !

mx
Capture1.gif
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
That's better!

~bp