Solved

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

Posted on 2011-09-03
32
526 Views
Last Modified: 2012-06-05
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?
0
Comment
Question by:compuiter
  • 10
  • 7
  • 4
  • +3
32 Comments
 
LVL 21

Expert Comment

by:yo_bee
ID: 36477982
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
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 36477983
What OS are you running this on?
0
 
LVL 4

Author Comment

by:compuiter
ID: 36478018
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.
0
 
LVL 21

Expert Comment

by:yo_bee
ID: 36478042
can you post all 4 batch files?  This may help with troubleshooting.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 36478475
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36478967
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.
0
 
LVL 4

Author Comment

by:compuiter
ID: 36479070
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.
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
ID: 36479087
If using START with a quoted executable path, you need this quirky format:

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

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36479152
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
0
 
LVL 4

Author Comment

by:compuiter
ID: 36479174
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.
0
 
LVL 4

Author Comment

by:compuiter
ID: 36479195
Forget about the waiting for: start "" "installer.exe" until if finishes i modify it and have it working.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36479302
@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
0
 
LVL 4

Author Comment

by:compuiter
ID: 36479311
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.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36479320
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:compuiter
ID: 36479356
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
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36479373
Try this in the second bat file:

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

~bp
0
 
LVL 4

Author Comment

by:compuiter
ID: 36479395
No, this is

start "" "DVDFab8092Qt.exe" /VERYSILENT
exit /b
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36479754
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.
0
 
LVL 4

Author Comment

by:compuiter
ID: 36481070
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.

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 125 total points
ID: 36481129
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
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 36481358
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

0
 
LVL 4

Author Comment

by:compuiter
ID: 36507991
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?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36508189
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
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36527480
compuiter - did you get this working how you wanted  in the end,  or need more help still?

steve
0
 
LVL 4

Author Comment

by:compuiter
ID: 36527597
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 ?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37359031
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
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38031092
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38031093
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,"
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now