Link to home
Start Free TrialLog in
Avatar of deathbybatfile
deathbybatfile

asked on

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?
Avatar of JohnGerhardt
JohnGerhardt
Flag of Switzerland image

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..
ASKER CERTIFIED SOLUTION
Avatar of chrisdunn_6
chrisdunn_6

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chrisdunn_6
chrisdunn_6

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!




SOLUTION
Avatar of BillDL
BillDL
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deathbybatfile

ASKER

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).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Billdl I have never got choice.com to work all around it seems there are different versions floating around.
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.
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.
thanks
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 (https://www.experts-exchange.com/questions/23901802/Using-Errorlevel-0-in-a-batch-file.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:
https://www.experts-exchange.com/questions/23843700/search-and-copy.html?anchorAnswerId=22819092#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
Hey Bill....

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

See you around.


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