Need a simple batch script to call or run other batch scripts

compuiter
compuiter used Ask the Experts™
on
I need a SIMPLE batch script to call or run other batch scripts.

Normally i get the console open but they dont run and the console stays open, why does this happend and how can i solve it?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
yo_beeDirector of Information Technology

Commented:
Do you have examples of what you would like to run?

You should be able to just open notepad > enter the batch file name and save it as another bat.

i.e
File 1: command
c:\Second.bat
Save it as <someaname>.bat  Then run it.
File 2: Command
Calc.exe
save it as second.bat
yo_beeDirector of Information Technology

Commented:
What OS are you running this on?

Author

Commented:
XP and 7, they run a bunch of staff.

Main.bat (inside)
"folder\batch1.bat"
"folder2\batch2.bat"
call batch3.bat
call "folder4\batch4.bat"

They keep doing the same.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

yo_beeDirector of Information Technology

Commented:
can you post all 4 batch files?  This may help with troubleshooting.
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Batch files always need to be called with  CALL, else execution will not proceed back to the calling batch.

call folder\batch1.cmd
call folder2\batch2.cmd
aso.

Commented:
May also be down to how you finish the "other" batch files... if you have EXIT in there for instance.  Also if you run other processes in those batch files, e.g. an executable then until that exe stops it will wait.  There are ways around that -- use START "" yourexe.exe and it will start and carry on the batch file at the next line for instance.

Author

Commented:
test.bat
call ".\folder\programs.bat"



 folder\programs.bat
DVDFab8092Qt.exe
rem It doesnt run

start "" "DVDFab8092Qt.exe" /VERYSILENT
rem It doesnt run

start calc.exe
rem It opens the calc fine

start "%programfiles%\Google\Picasa3\Picasa3.exe"
rem it doesnt run

"%programfiles%\Google\Picasa3\Picasa3.exe"
it opens picasa fine

exit

If i got an exit at the end it finishes right there and the main batch script it doesnt call other batch scripts but the main problem is that most of the programs dont run i dont know to solve it.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
If using START with a quoted executable path, you need this quirky format:

start "" "%programfiles%\Google\Picasa3\Picasa3.exe"

~bp

Commented:
As Bill says if you put a filename in quotes because of spaces then it treats that as the window title for START so you have to specify a title in quotes  first, or just the "" as I showed above too.

As to the other ones.  Is dvdfab8092qt.exe in the same dir. as the programs.bat, does it say "is not recognized as an internal or external command" or similar or what?

As to exit.  If you use

exit /b

it will close the batch file and return to the calling batch

If you do just exit it will close the batch and stop the cmd.exe process.

Steve

Author

Commented:
Yes dvdfab8092qt.exe say "is not recognized as an internal or external command" or similar. What can i do so i can call them and dont have this problem?

I use a lot start "" "installer.exe" but some times i need the script to wait untill the installer finishes and then continue with the script.

Should i use exit /b on all my scripts?, some times i use them directly or sometimes i call them with a main script.

Author

Commented:
Forget about the waiting for: start "" "installer.exe" until if finishes i modify it and have it working.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
@compuiter

==> Yes dvdfab8092qt.exe say "is not recognized as an internal or external command" or similar.
==> What can i do so i can call them and dont have this problem?

Where is that EXE located, sounds like it's not in the PATH, or in the current directory when the BAT runs, so you will need to include the path to the EXE for it to run it.

~bp

Author

Commented:
The path is right, if i run the main.bat i get that message "is not recognized as an internal or external command" or similar.

If i run the called programs.bat directly on its folder it works fine.  Only when i call it i get that message.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Is main.bat in a different folder than programs.bat?

I'd need to see both bat files, and also know how you execute each.  What directories are they in, and what is the current directory when you run each.

~bp

Author

Commented:
C:\main.bat
call "programs\DVDFab\DVDFab.bat" (This doesnt work i get the error message)
exit

C:\programs\DVDFab\DVDFab.bat (This works fine)
C:\programs\DVDFab\DVDFab8092Qt.exe
start "" "DVDFab8092Qt.exe" /VERYSILENT
exit /b

I try to make it very simple to see whats wrong, i have only this files with this scripts now i get a windows msg that it cant find DVDFab8092Qt.exe on the main script, on the DVDFab.bat script works fine.

screenshot.JPG
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Try this in the second bat file:

start "" "C:\programs\DVDFab\DVDFab8092Qt.exe" /VERYSILENT

~bp

Author

Commented:
No, this is

start "" "DVDFab8092Qt.exe" /VERYSILENT
exit /b
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
In main.bat you forgot to add the complete path. You only gave a relative path, and we do not know which the current dir is the script is called for.

C:\main.bat:
call "C:\programs\DVDFab\DVDFab.bat"
exit /b

Similar with the call of the EXE - it is unsafe and prone to failure if you do not set the working folder pre execution. You are calling the EXE twice - that sounds incorrect, and further, it is once with path and once without.

Author

Commented:
In main it has the complete path but i dont have exit /b only exit.

This are all mi files and folders and the exact info inside the scripts:

All the files:
C:\main.bat
C:\programs\DVDFab\DVDFab.bat
C:\programs\DVDFab\DVDFab8092Qt.exe

Location of the main.bat script and what it contains:
C:\main.bat
call "programs\DVDFab\DVDFab.bat"
exit

Location of the DVDFab.bat script and what it contains:
C:\programs\DVDFab\DVDFab.bat
start "" "DVDFab8092Qt.exe" /VERYSILENT
exit /b

---

If i run it with enter or double click directly in C:
C:\main.bat i get the problem same as the screenshot, it doesnt run.

If i run it with enter or double click directy in its own foler C:\programs\DVDFab
C:\programs\DVDFab\DVDFab.bat it installs fine.

How do i set the working folder pre execution? or can you fix the script on what i wrote wrong?

In the main script i am calling only de DVDFab.bat and that should do the work like when i run it directly with enter or double click in its own folder or i cant understand what is wrong in the script.

Commented:
Like has been said above  main.bat script  should be:

call "c:\programs\dvdfab\dvdfab.bat"

or if you want it relative to the current directory then you COULD use ".\programs\dvdfab\dvdfab.bat" but just specify the full path.

However I imagine your problem is that you are assuming when you call the batch file from the subdir that the current directory is the directory of the batch file. It is not.  It is the directory it was before, i.e. c:\ in this case.

You either need to specifically cd into the directory in the main.bat or dvdfab.bat or specify in the exe line the path.  You can see this by adding

echo %cd% to the line above your start command, i.e.

In dvdfab\dvdfab.bat

@Echo off
echo Directory is %cd%
pause
exit /b

You can change dir to the one containing the batch file with

cd "%~dp0"

which does a change dir. to the path of the batch file.  

So then you end up with:

@echo off
echo In the dvdfab batch file using %cd%
cd "%~dp0"
echo In the dvdfab batch file using %cd% after changing it
dvdfab8092qt.exe /verysilent
exit /b

Sorry if layout / formatting is bad above, browser showing only one line of this box for some reason!

hth

Steve
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
The cd stuff above is correct, but I prefer (for many reasons) to use pushd / popd.
* pushd changes the folder AND drive (like cd /d does)
* popd changes the folder back to what it has been when pushd was called.

BTW, if it is getting more complicated, you should use the CODE tags for code snippets. One of the reasons is that it is more clear what is part of the batch file, and the formatting is maintained.

C:\programs\DVDFab\DVDFab.bat:
@echo off
pushd %~dp0
dvdfab8092qt.exe /verysilent
popd
exit /b

Open in new window

Author

Commented:
It looks that its working i am changeing my scripts and doing some tests..

Do i always have to call the second script from the main script or i can set just the path where the second script is?

On the second scripts i always have to set pushd %~dp0 at start just below @echo off and popd exit /b at the end?

Commented:
yes... Or cd like i said.   the difference is that cd changes the dir to the path mentioned.  pushd maps a temorary deive (starting with z) to the directory you run it from, even if it has been run from a unc, i.e. \\server\share\batch.cmd.  popd then just puts you back where you were

if you dont use call then it will run the second script and then not return to the rest of the first.

So yes always use call unless you want it not to continue for some reason (e.g. It won't  matter for the last entry).

And you need to add the pushd or cd IF you are expecting the current directory to be the directory where the batch file is stored.  if all files were in the same directory, or you used full patg to access anything you run from them you don't need to.

Steve

Commented:
compuiter - did you get this working how you wanted  in the end,  or need more help still?

steve

Author

Commented:
Hi Steve, actually im out of town right now but just before that i got it working with Qlemos notes, i want to test it with your notes this week but i need to understand it a little bit more on how this works with a real examples with my real  paths, code, etc. so i can see what works best and easy to manage for my diferent scripts. Can you give me a real and short example on the simple script i wrote the main.bat calling DVDFab.bat ?

Commented:
I missed the reply there http:#36527597 from compuiter at the time.  If you are still around and want more input then ask, but as far as I can see there is enough in the various comments, I suppose these would be reaonable ones to select as contributors after a quick scan:

qlemo http:#36478475
billprew http:#36479087
qlemo http:#36481358
myself http:#36481129

Steve
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.

Commented:
The asker did say http:#36527597
"Hi Steve, actually im out of town right now but just before that i got it working with Qlemos notes,"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial