Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creating an MS-DOS Batch File

Posted on 2010-01-05
19
Medium Priority
?
641 Views
Last Modified: 2012-08-14
I am trying to create a .bat file to handle this scenario:

I have three ini files:  M2Mwin.ini, M2MwinAT.ini, and M2MwinNS.ini, all located in the C:\Windows directory.
I have one executable, M2M.exe, located in the C:\Program Files\M2M directory.

In order for the executable file, C:\Program Files\M2M\M2M.exe, to run, it has to find a file named M2Mwin.ini in the C:\Windows directory.

I have two different environments in which I want to run the executable.  One will use M2MwinAT.ini and the other will use M2MwinNS.ini. Each of these two .ini files contain different parameters that M2M.exe needs to have to execute M2M in the correct environment - but, for M2M.exe to run in the correct environment, the .ini file for 'AT' or 'NS' will have to be renamed to M2Mwin.ini, in the same C:\Windows directory location.

That is, the chosen environment will have to rename M2MwinAT.ini or M2MwinNS.ini (whichever is selected from the batch file menu prompt) to M2Mwin.ini.  After this is done, then the batch needs to run the M2M.exe executable.  Upon closing/exiting the M2M program, the batch file needs to undo the renaming action it performed before it executed M2M.exe to put back the M2Mwin.ini file that was there before M2MwinAT.ini or M2MwinNS.ini was renamed M2Mwin.ini - then close the batch file when this has been done.

So - the user clicks on the batch file icon. The user sees a selection option "Enter 1 for AirTeq M2M, 2 for Norshield M2M or END to quit:".  

If '1' is chosen, then the batch file will rename M2Mwin.ini to M2MwinTMP.ini, rename M2MwinAT.ini to M2Mwin.ini, and then run M2M.exe.  

If '2' is chosen, then the batch file will rename M2Mwin.ini to M2MwinTMP.ini, rename M2MwinNS.ini to M2Mwin.ini, and then run M2M.exe.  

In either case, when the user exits the M2M program, this exit action returns to the batch file and the batch file will reverse the renaming it did before executing M2M.exe. It will put the original M2Mwin.ini file (the one it renamed M2MwinTMP.ini) back into the C:\Windows directory and will rename the  .ini file it renamed to M2Mwin.ini (either M2MwinAT.ini or M2MwinNS.ini whichever the user chose when first clicking the batch file Icon) back into the C:\Windows directory as either M2MwinAT.ini or M2MwinNS.ini (whichever one of the two it previously had renamed C:\Windows\M2Mwin.ini).  At that point, the batch file will close and return the user to the desktop.

I know how to begin this batch file, but I can't figure out how to actually make it do what I want it to.  I started off with the code below, but it returns invalid path statements after making the initial 1, 2, or END choices.

How do I fix the code to make it do the steps outlined above?


@echo off

setlocal

set /p mypath=" Enter 1 for AirTeq M2M, 2 for Norshield M2M or enter END to quit: "

if "%mypath%"=="1" goto AT
if "%mypath%"=="2" goto NS
if "%mypath%"=="END" goto END

echo "Invalid path selection: %mypath%"
goto END

:AT

set mypath=C:\WINDOWS
rename "%mypath%\M2Mwin.ini %mypath%\M2MwinTMP.ini
rename "%mypath%\M2MwinAT.ini %mypath%\M2Mwin.ini

start /wait ""C:\Program Files\M2M.exe"

rename "%mypath%\M2Mwin.ini %mypath%\M2MwinAT.ini
rename "%mypath%\M2MwinTMP.ini %mypath%\M2Mwin.ini

goto END

:NS

set mypath=C:\WINDOWS
rename "%mypath%\M2Mwin.ini %mypath%\M2MwinTMP.ini
rename "%mypath%\M2MwinNS.ini %mypath%\M2Mwin.ini

start /wait ""C:\Program Files\M2M.exe"

rename "%mypath%\M2Mwin.ini %mypath%\M2MwinNS.ini
rename "%mypath%\M2MwinTMP.ini %mypath%\M2Mwin.ini

goto END

:END

endlocal

Open in new window

0
Comment
Question by:Glenn Stearns
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 26180610
In both of your   start  statements, you have an extraneous double-quote:

  start /wait ""C:\Program Files\M2M.exe"

should be:

  start /wait  "C:\Program Files\M2M.exe"
0
 
LVL 6

Expert Comment

by:LeeeRussell
ID: 26180979
And all the rename lines have an unpaired quote at the start of the argument list.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 26181206
good catch -- what he means is this:

   rename "%mypath%\M2Mwin.ini %mypath%\M2MwinTMP.ini

should be:

   rename  "%mypath%\M2Mwin.ini"  "%mypath%\M2MwinTMP.ini"
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:Glenn Stearns
ID: 26181280
I made the change, but get the error that they syntax is incorrect (see attachment). Since the error message is showing twice, I assume it is in regard to the start /wait lines in the batch. What do you think is wrong with the syntax of those lines?  Also, M2M.exe is running on an XP 64-bit machine, so the name of the program files directory is actually C:\Program Files (x86)\. I inadvertently left the (x86) out of the script included initially with this question.

Below is the AT segment of the script showing how it looks after the corrections, but I made the same changes to the NS segment, too:

:AT

set mypath=C:\WINDOWS
rename "%mypath%\M2Mwin.ini %mypath%\M2MwinTMP.ini
rename "%mypath%\M2MwinAT.ini %mypath%\M2Mwin.ini

set mypath="C:\Program Files (x86)"
start /wait "C:\Program Files (x86)\M2M.exe"

set mypath=C:\WINDOWS
rename "%mypath%\M2Mwin.ini %mypath%\M2MwinAT.ini
rename "%mypath%\M2MwinTMP.ini %mypath%\M2Mwin.ini

goto END
Batch-File-Error.pdf
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 26181291
see preceding comments ...
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 26181463
Also, if I may suggest an alternative to renaming files (which can get you into trouble if the batch file is interrupted)...
@echo off

 setlocal

 copy/y  "%mypath%\M2Mwin.ini"  "%mypath%\M2Mwin.bak"

 set mypath=C:\WINDOWS
 set /p response=" Enter 1 for AirTeq M2M, 2 for Norshield M2M or enter END to quit: "

 if "%response%"=="1"  copy/y  "%mypath%\M2MwinAT.ini"  "%mypath%\M2Mwin.ini"
 if "%response%"=="2"  copy/y  "%mypath%\M2MwinNS.ini"  "%mypath%\M2Mwin.ini"
 if /i "%response%"=="END" goto END
 if not "%response%"=="1" if not "%response%"=="2" goto :INVALID

 start /wait "C:\Program Files\M2M.exe"
 goto END

:INVALID
@echo Invalid response: %response%

:END

 copy/y  "%mypath%\M2Mwin.bak"  "%mypath%\M2Mwin.ini"

 endlocal

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 26181685
You'll need an extra set of quotes with START.

start "" /wait "C:\Program Files\M2M.exe"
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 26181714
FYI

Renames does not work with full path in the new name. However, I would also recommend using copy as knightEknight has shown.

:AT

set mypath=C:\WINDOWS
rename "%mypath%\M2Mwin.ini" M2MwinTMP.ini
rename "%mypath%\M2MwinAT.ini" M2Mwin.ini

set mypath="C:\Program Files (x86)"
start /wait "C:\Program Files (x86)\M2M.exe"

set mypath=C:\WINDOWS
rename "%mypath%\M2Mwin.ini" M2MwinAT.ini
rename "%mypath%\M2MwinTMP.ini" M2Mwin.ini

goto END
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 26181724
Thanks .. and I just noticed that with a late addition to my last post, I re-created the very problem I was trying to work around -- namely what happens when the script is interrupted after copying over the original file, but before it restores it at the end!  Stand by for corrections...
0
 
LVL 16

Expert Comment

by:t0t0
ID: 26181732
glennes

Shouldn't your code look something like this?


@echo off
setlocal

:loop
   set option=
   set /p option=Please select (1) AirTeq M2M, (2) Norshield M2M, (Q) Quit [1,2,Q]:  

   if "%option: =%"=="1" (
      copy /y c:\windows\M2MwinAT.ini c:\windows\M2Mwin.ini >nul
      start /wait "c:\Program Files\M2M.exe"
      goto :loop
   )
      
   if "%option: =%"=="2" (
      copy /y c:\windows\M2MwinNS.ini c:\windows\M2Mwin.ini >nul
      start /wait "c:\Program Files\M2M.exe"
      goto :loop
   )

   if /i "%option: =%"=="q" (
      echo Goodbye
      goto :eof
   )

   echo Invalid selection. Please try again...
   echo.
goto :loop
0
 
LVL 16

Accepted Solution

by:
t0t0 earned 1000 total points
ID: 26181782
glennes

Or should it look something like this?


@echo off
setlocal

:loop
   set option=
   set /p option=Please select (1) AirTeq M2M, (2) Norshield M2M, (Q) Quit [1,2,Q]:  

   if "%option: =%"=="1" (
      copy /y c:\windows\M2MwinAT.ini c:\windows\M2Mwin.ini >nul
      start /wait "c:\Program Files\M2M.exe"
      exit /b 0
   )
     
   if "%option: =%"=="2" (
      copy /y c:\windows\M2MwinNS.ini c:\windows\M2Mwin.ini >nul
      start /wait "c:\Program Files\M2M.exe"
      exit /b 0
   )

   if /i "%option: =%"=="q" (
      echo Goodbye
      exit /b 0
   )

   echo Invalid selection. Please try again...
   echo.
goto :loop
0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 1000 total points
ID: 26181801
This version is a slight change from my last, and will at least preserve the original in a .bak file in case something unexpected happens to it ...

@echo off

 setlocal

 if not exist "%mypath%\M2Mwin.bak"  copy/y  "%mypath%\M2Mwin.ini"  "%mypath%\M2Mwin.bak"
 copy/y  "%mypath%\M2Mwin.ini"  "%mypath%\M2Mwin.prev"

 set mypath=C:\WINDOWS
 set /p response=" Enter 1 for AirTeq M2M, 2 for Norshield M2M or enter END to quit: "

 if "%response%"=="1"  copy/y  "%mypath%\M2MwinAT.ini"  "%mypath%\M2Mwin.ini"
 if "%response%"=="2"  copy/y  "%mypath%\M2MwinNS.ini"  "%mypath%\M2Mwin.ini"
 if /i "%response%"=="END" goto END
 if not "%response%"=="1" if not "%response%"=="2" goto :INVALID

 start /wait "C:\Program Files\M2M.exe"
 goto END

:INVALID
@echo Invalid response: %response%

:END

 copy/y  "%mypath%\M2Mwin.prev"  "%mypath%\M2Mwin.ini"

 endlocal

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
ID: 26181919
glennes

There is ABSOLUTELY no need to shunty files around using a TMP file in the process. All you really need to do is COPY the contents of whichever file - the AT or NS file, to the M2Mwin.ini file, depending on the user's selection.

I wouldn't even muck about using the %mypath% variable as it doesn't really make your code any portable than what it already is - it's not as though you're likely to change the location of the INI files is it?

Furthermore, you don't need double-quotes on the COPY lines as there are no spaces in the filespec. The START lines do however require double-quotes as can been seen in my code.

The 'SET option=' just after the ':loop' ensures option does not pick up a spurious value.

The rest is easy-peasy!

Oh, by the way, the 'Goodbye' bit at the end is a nice touch as are words like 'Please' and 'Thank you' - from a user's view point, it de-mechanises programs and even computers themselves.

Oh, a last thought... Your GOTOs and %mypath%s just clouded the logic and clarity of your program.
 
0
 
LVL 16

Expert Comment

by:t0t0
ID: 26182182
knightEknight

that's one of the worst examples of code i have seen for a long time....

sorry mate!
0
 

Author Comment

by:Glenn Stearns
ID: 26182580
knight:
I modified your file to take out the %mypath% part of the statements and replaced it with C:\Windows,  removed the /i from line 12, and also modified the start /wait line and now the batch file executes exactly as it should and runs like I need it to do.  Thanks for getting me on the right track with this...your suggestions were just what I needed!

Here's the batch file as it looks now:

@echo off
 
 setlocal
 
 copy/y  "C:\Windows\M2Mwin.ini"  "C:\Windows\M2Mwin.bak"
 
 
 set /p response=" Enter 1 for AirTeq M2M, 2 for Norshield M2M or enter END to quit: " 
 
 if "%response%"=="1"  copy/y  "C:\Windows\M2MwinAT.ini"  "C:\Windows\M2Mwin.ini"
 if "%response%"=="2"  copy/y  "C:\Windows\M2MwinNS.ini"  "C:\Windows\M2Mwin.ini"
 if "%response%"=="END" goto END
 if not "%response%"=="1" if not "%response%"=="2" goto :INVALID
 
 start /D "C:\Program Files (x86)\Made2Manage" /wait /I M2M.exe
 goto END
 
:INVALID
@echo Invalid response: %response%
 
:END
 
 copy/y  "C:\Windows\M2Mwin.bak"  "C:\Windows\M2Mwin.ini"
 
 endlocal
0
 

Author Comment

by:Glenn Stearns
ID: 26182676
toto:

Your batch file runs as it should also - the only change I had to make was to the start /wait line.  Like in knight's code, the batch won't launch m2m.exe unless I change it to:

    start /D "C:\Program Files (x86)\Made2Manage" /wait /I M2M.exe

which properly launches m2m.exe.

Also, I very much like the logic you used in constructing the batch file for all the reasons you highlighted above.  Thanks so much for your valuable help!
0
 

Author Closing Comment

by:Glenn Stearns
ID: 31672898
Outstanding help, guys! You showed me several new batch file writing techniques that I'm sure I'll use again and again.  And thanks, too, for hanging with me till we got it working!
0
 
LVL 16

Expert Comment

by:t0t0
ID: 26182970
glennes

I do hope you accepted my solution instead of knightEknight's and that the points/status is not just an accident of circumstance.

My code is VASTLY superior to knightEknight's.... I very much dislike the spehetti-style use of GOTOs.

You mentioned 'logic'.... clarity and performance are also major factors. KnightEknight tried not to use two STARTs in his code however, for the sake of clarity, logic and performance, it is sometimes preferable to use repetitive code.

To some extent we are all experts. I only hope knightEknight takes my criticisms constructively rather than as insulting comments.

I am just fed up of junk code from so-say experts....

(That last comment was NOT aimed at knightEknight - it is a general comment)

Thank you for the points and grade.


 
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 26183737
I generally discourage the use of GOTO statements, and I never use them in real applications.  But this is a batch script that is small enough to fit on a single screen (even in 800x600 res), so I don't see it as an issue.

Besides, in this context using  exit/b 0  is functionally the same as using  GOTO :EOF

Finally, as glennes points out, both scripts work equally well, despite your "VASTLY superior" code.

Thanks glennes! I hope this helps!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question