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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I can't think of a reason why not to do this..
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..
why use regfiles just use batch.

@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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That works, though if all your machines are XP and up then it's more elegant to use the REG command.

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

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Yeah and Im not sure but I dont think you can force a regfile if the key exsists with a different value can you?
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!

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:

"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.


@="\"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 " " :


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?

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).
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.
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. allowed you to use the Errorlevel returned to determine which Menu option was being chosen like this:

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

The important bit was the order of the 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 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:
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:

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.

Billdl I have never got to work all around it seems there are different versions floating around.

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:

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.
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.
deathbybatfileAuthor Commented:
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 (, 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:

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.


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" :-)

Hey Bill....

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

See you around.

deathbybatfileAuthor Commented:
You should have done - I gave 125 each to be fair including yourself
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,
Thank you very much deathbybatfile
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.