• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

Is there any reason not to include a reg file inside a bat file?

I have compiled an executable from a bat file and a program that changes settings for me. I have written the reg file into the code as follows:

Echo Windows Registry Editor Version 5.00>"c:\Program Files\Player\battcad.reg"
Echo: >>"c:\Program Files\Player\battcad.reg"
Echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]>>"c:\Program Files\Player\battcad.reg"
Echo "AutoAdminLogon"="2">>"c:\Program Files\Player\battcad.reg"
Echo: >>"c:\Program Files\Player\battcad.reg"
Echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]>>"c:\Program Files\Player\battcad.reg"
Echo "NoDriveTypeAutoRun"=dword:000000ff>>"c:\Program Files\Player\battcad.reg"

regedit.exe /s battcad.reg

This works but I wondered if it was bad practise or any other reason it's best not to do it like this?
0
deathbybatfile
Asked:
deathbybatfile
  • 5
  • 4
  • 4
  • +3
4 Solutions
 
JohnGerhardtCommented:
I can't think of a reason why not to do this..
BUT
I can think of a reason to do it..!

If is a common GPO setting to turn of the use of registry editing tools.. IE if you just double click the reg file then it would tell the user that they are not allowed to edit the registry.
There is a second setting that says to allow "editing of the registry silently" (or something similiar!) and this means that this batch file would work without an error

I think that this gives a little bit of security to admins but allows you to make changes the the registry via batch files if needed (this is especially useful if you are wnating the edit a user hive etc.) I certianly  work like this..
0
 
chrisdunn_6Commented:
why use regfiles just use batch.

Example:
@echo Creating Registry Values
SET KEY1="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
REG ADD %KEY1% /V AutoAdminLogon /D "1" /F
REG ADD %KEY1% /V DefaultPassword /F
REG ADD %KEY1% /V DefaultUserName /D "User" /F
CLS
0
 
Shift-3Commented:
That works, though if all your machines are XP and up then it's more elegant to use the REG command.
http://www.ss64.com/nt/reg.html


reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 2 /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v NoDriveTypeAutoRun /t REG_DWORD /d 0xff /f

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
chrisdunn_6Commented:
Yeah and Im not sure but I dont think you can force a regfile if the key exsists with a different value can you?
0
 
t0t0Commented:
I can see where you're coming from but consider the following question....why hardcode registry entries in a batch file which then only serves to create a reg file when you can just as easily directly hardcode a reg file itself - it doesn't make much point.

If you were writing dynamic data to the registry file (data that changes) then the use of batch files could be justified however, if that was the case then I would favour accessing the registry file using DOS' reg commands if they are available to you. This way, you are less likely to make mistakes which could compromise the inegrity of your registry file.

Having said that, you have grasped the concept of redirection to a file with good understanding as well as demonstrating a knowledge of how reg files are composed.

I would suggest you look at DOS' reg command by typing:

REG /?

inside a DOS box.

On the other hand, could it be the case your compiler does not recognise DOS' reg commands?

Programming is a creative process, so be creative. Welcome to DOS!




0
 
BillDLCommented:
Hi deathbybatfile

On some versions of Windows *.reg files do not import to the registry on if there is not at least one empty line after the last line of text.  This was certainly the case with Windows 98 and probably ME.  Windows XP doesn't seem to care about the existence of blank lines, but I cannot be sure about Windows 2000.  If you export a *.reg file from Regedit in all versions of Windows from 98 onwards you will be able to see that 2 blank lines are always added at the end.

So if there is a possibility that your executable may be run on older Windows versions may I suggest that you err on the side of safety and echo 2 blank lines to the *.reg file at the end.  It won't do any harm if run on Windows XP.

echo.  >> "c:\Program Files\Player\battcad.reg"
echo.  >> "c:\Program Files\Player\battcad.reg"

Although it doesn't affect you with your current batch/reg file, one thing to always remember is that directory paths contained in a registry value will export with double-backslashes, and therefore must be given in the *.reg file with double-backslashes.

Example of a directory path in a standard StringValue:

[HKEY_CURRENT_USER\Software\Avg\Avg8\AVGToolbar]
"IE"="C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE"

Yet another thing to be aware of is that some registry values that contain directory values enclose the path in " " and this ends up complicating the issue.

Example:

[HKEY_CLASSES_ROOT\.dl_\shell\uniextract\command]
@="\"C:\\Program Files\\Universal Extractor\\uniextract.exe\" \"%1\""

The actual command, if this was issued at the command line, would be:

"C:\Program Files\Universal Extractor\uniextract.exe" "%1"

The Path and the Variable are separately enclosed in " ".

Because all String Values exported from Regedit (and therefore an imported *.reg file must also comply) enclose a StringValue with " " :

eg.
"ValueName"="ValueData"
@="ValueData"

to have the additional double-quoted directory path and variable (%1)  treated literally, they must each be preceded by a \

Look again and you'll follow the logic:

@="\"C:\\Program Files\\Universal Extractor\\uniextract.exe\" \"%1\""

Wherever you see a single backslash it is being used to force the character immediately after it to be treated literally.

One last thing I noticed is that you seem to have used a colon to echo a blank line to the *.reg file.

Echo: >>"c:\Program Files\Player\battcad.reg"

I know from experimentation that there are a few symbols that work and do the same job in the end, but the colon can also cause some quite undesirable results if, for example, you accidentally leave a space.  I suggest that you stick to the:
echo. >> "c:\Program Files\Player\battcad.reg"

Just one last thing if you sometimes need to add explanatory comments to your batch files while testing.  A REM at the start of a line Remarks it out.  Using :: also remarks out that line, but there is a difference.  If you use certain symbols (specifically redirectors like | and > ) in your line that is remarked out with REM, then that line may still work in some ways like a command and end up creating zero-byte files, and other things.  The :: and everything after the space following it is totally ignored, so not only is there no chance of accidentally having commands run, but those lines are skipped over and the batch file executes faster than if it had a lot of REM prefixed lines.

Just out of curiosity, what did you use to compile the batch file into an executable?

Bill
0
 
deathbybatfileAuthor Commented:
Hey thanks for the response guys - I was curious about my use of the echo command here and wanted input from you more experienced guys :D

ChrisDunn - do like the reg add command - will look at changing to that as it's not only tidier it obviously doesn't require me to write to another file/call it/delete it so would be an improvement to my existing code.

t0t0 - the reason i've included the reg file within the batch is that the compiler I have (bat_to_exe converter) only accepts one additional file to create an executable and I have to include another program already. This was my workaround for that problem. I DO have another compiler which accepts more than one but that doesn't allow the other program to run properly. All good fun of course.

BillDL - some great tips there mate thanks. All my machines are XP but I take your point about "best practice" to include the space at the end. I will also check out your point about the full stop - I'm literally googling all of this to learn how it's done and using trial and error to get it all working (never a bad way of learning though).
0
 
t0t0Commented:
To come back to you on this.... I think your comments to ChrisDunn seems to confirm the point I was trying to make.

ChrisDunn's solution is tidier (and I would say a whole lot clearer too) and this goes back to what I was trying to assert earlier regarding errors - the clearer your code is the less likely mistakes will creep in. Also, it is easier to hardcode reg entries directly in your batch file than redirecting them to another file.

Looking back at your original question, you ask if your method is: "bad practise or any other reason it's best not to do it like this?"

Even though your method works well, using reg commands makes your code tidier, more compact, clearer and above all, less prone to errors - and it is this last fact that is critical when manipulating registry file entries.

In short, it is better to use the tools provided wherever compatibility allows.
0
 
BillDLCommented:
Hi deathbybatfile

It looks like you may have to change your Experts-Exchange login name some time soon or it may mislead others into believing that you hate "DOS" batch programming ;-) You're obviously enjoying it, and your capacity to assimilate, interpret, and personalise a lot of your information from web searches in a fairly short space of time deserves some personal praise.

I used to use an old bat-to-com compiler, and then a com-to-exe compiler to create some working programs for Windows 98, but that process occasionally ended up creating some undeletable files with unusual characters in the directory from where I ran the program.  If the compiler you have successfully used is immune from this, and if it is freeware (or cheap), I don't suppose you could let me know a download site for it.  I would love to wrap up some of my old batch files into executables.

Seeing as you now enjoy batch files so much, here's one other useful thing to know.  I used to write a lot of multi-purpose batch files for Win9x that used a Menu something like this:

      Windows Thingumyjig Switcher

         1. Turn OFF
         2. Turn ON
         Q. QUIT

       Key Option 1, 2, or Q ...

That depended on the presence of the Windows 9x CHOICE.COM executable being in the C:\Windows\Command folder, and the way CHOICE was used is similar to a SWITCH/CASE statement in older C Programming.  The character generated by the keypress is read and the batch process jumps to different places depending on which key you pressed.
http://www.computerhope.com/batch.htm#1
Choice.com allowed you to use the Errorlevel returned to determine which Menu option was being chosen like this:

------------------------------------------------------------------
@echo off
:MENU
cls
echo.
echo       Windows Thingumyjig Switcher
echo.
echo             1. Turn OFF
echo             2. Turn ON
echo             Q. QUIT
echo.
echo       Key Option 1, 2, or Q ...
echo.
choice /C:12Q /N
::
if errorlevel 3 goto :END
if errorlevel 2 goto :ACTIVATE
if errorlevel 1 goto :SUPPRESS
::
:ACTIVATE
cls
if exist Thingumyjig_ON.reg start /wait regedit /s Thingumyjig_ON.reg
echo.
echo      *** Windows Thingumyjig ACTIVE ***
echo.
echo           Press any key to return to menu...
PAUSE > NUL
goto :MENU
::
:SUPPRESS
cls
if exist Thingumyjig_OFF.reg start /wait regedit /s Thingumyjig_OFF.reg
echo.
echo       *** Windows Thingumyjig DEACTIVATED ***
echo.
echo           Press any key to return to menu...
PAUSE > NUL
goto :MENU
::
:END
cls
echo.
echo       ****** Terminated ******
echo.
EXIT
------------------------------------------------------------------

The important bit was the order of the choice.com parameters, and the order that the IF ERRORLEVEL tests were given:

choice /C:12Q /N
::
if errorlevel 3 goto :END
if errorlevel 2 goto :ACTIVATE
if errorlevel 1 goto :SUPPRESS

In this case, Errorlevel 3 is the 3rd parameter (Q), Errorlevel 2 is the 2nd (2), and Errorlevel 1 is the 1st (1) as laid out in choice /C:12Q  (the /n just suppressed the standard prompt allowing you to show your own).

The goto :LABELNAME does just what it says, and you should be able to follow the example above to see how it jumps to one of them, clears the screen (cls), does something and shows screen output, then prompts for an "any key press" to jump back to the :MENU label where it clears the screen to show the menu again.

Very useful indeed, but sadly Windows XP DOES NOT have Choice.com for use in this way.  A lot of people were badly disappointed about this apparent ommission.  I believe I transplanted a Win98 *.com or maybe an NT/2000 *.exe file into some of my XP systems for backward compatibility:
http://www.ss64.com/nt/choice.html
http://www.dynawell.com/download/reskit/microsoft/win2000/choice.zip
until I found the workaround for XP.  Note that I believe Windows Server 2003 comes installed with Choice.exe and it works in XP.

There are a few reasons I'm mentioning this:

1. It's possible at some point that you may want one of your batch files to pause and give the option to escape before doing something - a kind of "are you sure" step
2. You may wish to include other options within the batch file to make it more multi-purpose
3. It's likely you will come across the choice command on an Internet resource and may be disappointingly led to believe it is an Internal command usable in XP rather than an old External Win9x/DOS command.

The workaround for XP is to use the SET command with the /P switch ("Prompt String"):
SET /P variable=[promptString]
The /P switch allows you to set the value of a variable to a line of input entered by the user.  It displays the specified promptString before reading the line of input.  The promptString can be empty.

It's explained well with an example batch file here:

How to use the set command as a substitute for the choice command in Windows 2000 and Windows XP:
http://www.computerhope.com/sethlp.htm#04

Whether or not your bat-to-exe compiler will recognise the set /p usage is something you would have to test out, and I have a feeling you will be giving it a shot if only out of curiosity.

Cheers
Bill
0
 
chrisdunn_6Commented:
Billdl I have never got choice.com to work all around it seems there are different versions floating around.
0
 
t0t0Commented:
BillDL

Wow! Talk about sucking up....  If you're that desperate for the points Bill, I'll give you the points myself!

I genuinely thought your first comment was insightful, but your last comment was unnecessarily long, way off the point and added little or nothing to the current topic.

1) If you care to read deathbybatfile's previous comment you'll note he mentions bat_to_exe converter and I suspect he is refering to Bat To Exe Converter v1.4.1 which can be downloaded from:

http://www.f2ko.de

Bat To Exe Converter v1.4.1 is freeware and compatible with Vista, 2003, XP, 2K, 98 and ME.

2) Your demonstration of CHOICE and ERRORLEVEL would have been more appropriate elsewhere.

deathbybatfile asked a simple question and I feel an attempt has been made by both chrisdunn_6 and myself in providing an acceptable solution.

3) For your information, the following two-line batch file compiles and executes with no problems using Bat To Exe Converter v1.4.1:

@set /p text=Enter some text:
@echo You entered [%text%]

I hope you find this useful.
0
 
deathbybatfileAuthor Commented:
Thanks for all the input guys - my damn pc's have XP embedded and don't have REG.EXE so I'm unable to simplify the program as per the options stated short of writing another program to load it onto them all which I'm loath to do.
It works so I'm happy but I take your point on keeping it as simple as possible.
Wouldn't everything be simpler if there were only one OS!!!
Cheers to all.
0
 
deathbybatfileAuthor Commented:
thanks
0
 
BillDLCommented:
Thanks for your blatantly accurate and honest critique, t0t0.  Yes, I was a bit verbose, but it was in direct response to the question author's self-appraisal of his own knowledge level, and the fact that he has been "literally googling all of this to learn how it's done and using trial and error to get it all working".

My "sucking up" was actually a comment made with genuine intent, because it's not the easiest way to learn a subject.  Without intending to demean deathbybatfile in any way, his knowledge level of batch programming is somewhat sketchy (http://www.experts-exchange.com/Q_23901802.html), and my suggestion of using a "choice" option was more in context than would immediately appear.  I felt that I explained my full motive for posting this additional information for the author's assistance in my 3 points under the paragraph headed by "There are a few reasons I'm mentioning this", and the first reason is blatantly obvious in context with something that always is inherently dangerous, ie. messing with the registry.  Especially so while testing a batch file.

The word "Thingumyjig" annoyed you, didn't it?  ;-)
It was "Update" before modification, but I didn't want anyone assuming that I regard "Windows Update" flippantly.

~ "If you're that desperate for the points Bill, I'll give you the points myself" ~

By the look of things you can't really afford to do that, and in any case you and the initial contributors thoroughly deserve all the points, having fully answered the question at a fairly early stage.

From what I saw of the question author's feedback earlier, he clearly understood that my first comment contained useful "TIPS", which was the sole purpose of my contribution.  Call them "safety tips" if you will, or to quote the question author, "best practice" tips.  Yes, "safety" tips, and that was the primary reason for my 2nd comment.

To create a *.reg file on the fly and import it, or modify the registry directly using commands, all without requesting user confirmation is all fine and well for someone with experience.  However, a "hold up, are you sure you wanna do this?" prompt is, in my opinion, "best practice" for someone with intermediate knowledge currently being gleaned from other peoples' internet pages.  Yes, I am aware that a Winlogon registry policy disabling autorun on all drives would not be ideal with a "Y / N" option offered, but my 2nd comment was offered as an alternative usage option for OTHER types of batch files, given that this one may just have been a singular example of other future batch files.

So, I trust that I have made you aware that I neither need nor chase points, and that my presence here echoes AmazingTech's graceful response in this comment:
http://www.experts-exchange.com/Programming/Q_23843700.html#a22819092

Thank you for clarifying the issue about whether the SET /P command would compile, and providing the name and version of the compiler you "suspect" the question author to have been referring.  Silly me, although I read the author's earlier statement, I thought "bat_to_exe converter" was just a generic description for a program that does what it says on the can.  Problem is I've tested a great many programs named "bat to exe" before, all by different authors, so it was a rather generic name - to me anyway.  Thanks for the link, and I mean that sincerely.  I will find it useful.

deathbybatfile:

I'm sorry to have spoiled your question page with what may be superfluous tat.  I was only trying to help out a bit in areas that I wish I had had explained at an early stage when getting to grips with DOS.

I do hope that you understand I was not glory hunting and deliberately attempting to dilute the excellent, and most relevant, answers/suggestions posted by the others before and after I butted in.

Damn, so much for attempt to make this less verbose.  In the words of the popular (and perhaps rather nutty) female singer: "Whoops, I did it again" :-)

Regards
Bill
0
 
t0t0Commented:
Hey Bill....

I got an 'Excellent'. Didn't get any points though - like you said....

See you around.


0
 
deathbybatfileAuthor Commented:
You should have done - I gave 125 each to be fair including yourself
0
 
t0t0Commented:
Yep, just checked my emails.... Wow! points as well as an 'A'!...

Thank you deathbybatfile for accepting my comment as part-solution to your question and I hope you're well on your way to solving your problem.

Bill, your last comment was huge but thanks for being a good sport,
0
 
BillDLCommented:
Thank you very much deathbybatfile
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now