Batch File to change directory and run a command - windows

I want to execute two cmd commands.This is my batch file:
set MW_HOME=D:\wls1211_dev
set JAVA_HOME=D:\jdk1.6.0_45
set JAVA_VENDOR=Oracle
set root=D:\wls1211_dev
set pathname=D:\WLSDomain
CD /D %root%
%MW_HOME%\wlserver\server\bin\setWLSEnv.cmd
CD /D %pathname%
startWebLogic.cmd
But after executing the setWLSEnv.cmd command its not moving to the next directory where it has to execute the startWebLogic.cmd .
Thanks for your help.

If you directly invoke a batch file from inside a batch file, the execution is transfered to the called file and does not return.
You need to use call %MW_HOME%\wlserver\server\bin\setWLSEnv.cmd so when setWLSEnv.cmd ends, execution continues in your first batch file.

Related

CMD .bat file include by "sub.bat" vs "CALL sub.bat" [duplicate]

I'm using a batch file in folder1/folder2/file.bat
There is a batch file in parent folder folder1, that I want to open through file.bat
I have tried using:
start ..\..\code.bat
But this results in an error message, because file couldn't be found.
Any ideas?
I want to explain better what should be used with an example as the answers posted up to now work only with current working directory being the directory containing the batch file file.bat.
There is a directory structure as follows:
C:\
Temp
Folder 1
Folder 2
Example.bat
Parent.bat
The current working directory is C:\Temp on executing Example.bat either with
"Folder 1\Folder 2\Example.bat"
or with
"C:\Temp\Folder 1\Folder 2\Example.bat"
The batch file Parent.bat contains for example:
echo %0 is active.
pause
The batch file Example.bat contains already:
#echo off
echo Calling Parent.bat ...
rem How to run Parent.bat here?
echo %0 is active.
pause
The really working solutions in this scenario with the current working directory being a different directory than directory containing Example.bat are as follows.
Continue batch processing with Parent.bat
"%~dp0..\Parent.bat"
%0 references argument 0 on execution of the batch file which is always the name of the batch file as specified in parent process on starting the batch file.
But wanted is the drive and path of the batch file without double quotes. Therefore the expression %~dp0 is used to get C:\Temp\Folder 1\Folder 2\ from argument 0.
On this path the string ..\Parent.bat is appended, and additionally the entire new file name
C:\Temp\Folder 1\Folder 2\..\Parent.bat is enclosed in double quotes because of the spaces.
There is no return to Example.bat after processing of Parent.bat finished.
Call Parent.bat like a subroutine
call "%~dp0..\Parent.bat"
Command call results in execution of batch file Parent.bat in same command process (console window) with halting the execution of Example.bat until Parent.bat finished.
The batch execution continues on next line in Example.bat after processing of Parent.bat finished.
Exception:
Parent.bat contains command exit without switch /B because this results in an immediate exit of command line interpreter cmd.exe processing Example.bat and Parent.bat.
Execute call /? or help call in a command prompt window for short help on command call.
Start Parent.bat as parallel process
start "Parent Batch" "%~dp0..\Parent.bat"
Command start without any parameter with the exception of the optional title results in execution of batch file Parent.bat by a separate command process in a separate console window without halting the execution of Example.bat.
Therefore both batch files run at same time (more or less).
Note:
Command start interprets first string in double quotes as title. Therefore it is necessary to define explicitly a title in double quotes when the batch file or application to start, or any argument of the started batch file / application must be specified in double quotes because of 1 or more spaces.
Execute start /? or help start in a command prompt window for short help on command start.
Call Parent.bat as separate process
start "Parent Batch" /wait "%~dp0..\Parent.bat"
Command start with optional parameter /wait results in execution of the started batch file / application as separate process (additional console window for a batch file or console application), but halting the execution of the current batch file until the started process (Windows application or batch file / console application executed in a new console window) terminates itself.
..\ is used to go one level up. your case requires two levels up
Try:
start ..\..\code.bat
You could just:
cd..
start Code.bat
And that would start code.bat from its own directory
try to use this:
start ../code.bat

Cannot run python server using batch file

Here are the commands in batch:
D:
cd D:\Startup\venv\Scripts
activate
cd D:\Startup\
python manage.py runserver
Commands after "activate" are not executed for some reasons. I tried to put "cmd /k activate" instead "activate" but result is still the same except the command line is still open. What is wrong here
I suppose activate is a batch file and therefore needed is:
cd /D D:\Startup\venv\Scripts
call activate.bat
cd D:\Startup
python.exe manage.py runserver
Without command call the processing of a batch file continues on other batch file without returning which is the reason why the last two lines where never executed. Run in a command prompt window call /? and read output help and for more details see for example also answer on How to call a batch file in the parent folder of current batch file?
Command CD with parameter /D makes it possible to change directory and drive at the same time as cd /? executed in a command prompt window explains.
In batch files it is advisable to specify batch files and executables with file extension.

Building a batch file to run exe files sequentially

I just start to learn how to build batch file. ( on the windows 7 environment)
I want to build the batch file which is able to run .exe files sequentially .
Run batch files sequentially
I was trying to apply above idea but I am not really sure how to apply it
For example, there are three file on the D:/
In "D:/" there are three .exe files.
MyDriver.exe
YouDriver.exe
Mysoftware.exe
And I would like to build batch file which is running three exe files sequentially
Possible scenario is..
Run batch file
Run MyDriver.exe
MyDriver file's install GUI pops up and then user start to install Mydriver
Done with MyDriver.exe
Run YouDriver.exe
YouDirver file's install GUI pops up and then user start to install YouDriver
Done with YouDriver.exe
Run MySoftware.exe
MySofrware install interface pops up and then user start to install MySoftware
Done exit batch file.
I am not really sure if batch files can do it or not...
if it is impossible , is there any other options to build it ???
thanks
You actually don't need to do anything special to make this happen; batch files are synchronous by default, so execution of the batch file will pause when an executable is launched, and resume when it exits. Something as simple as this should do:
#echo off
REM "#echo off" prevents each line from being printed before execution,
REM and is optional
REM "REM" introduces a comment line
D:\MyDriver.exe
D:\YouDriver.exe
D:\MySoftware.exe
Of course, if you're interested in checking the return values of the programs, to see whether they succeeded or failed to install (assuming the installer provides that information), then things become slightly more complicated; if that's what you need, mention it in a comment, and I'll expand my answer accordingly.
This will start each file and wait for it to complete and then launch the next one.
#echo off
start "" /w /b "d:\MyDriver.exe"
start "" /w /b "d:\YouDriver.exe"
start "" /w /b "d:\Mysoftware.exe"
start MyDriver.exe
start YouDriver.exe
start MySoftware.exe
If you want the batch file in a different dir you would have to do:
cd D:\
start MyDriver.exe
start YouDriver.exe
start MySoftware.exe
If you want a more flexible system:
echo Welcome to EXE starter!
set /p dir = DIR:
set /p exe = EXE1:
set /p exe1 = EXE2:
set /p exe 2 = EXE3:
cd DIR
start exe
start exe1
start exe2
There you go!
To do it squentially:
call YouDriver.exe
call MeDriver.exe
call Mysoftware.exe
call will halt the batch file until program has closed.
Try and put it in the same directory of the files you want to run. If you can't, use cd C:\Directory\Name to change it to the directory where the MyDriver.exe file is. Then just do MyDriver.exe- you don't need a call or start statement.
MyDriver.exe
YouDriver.exe
MySoftware.exe
use cd at the start if you neeed to.

Restore default working dir if bat file is terminated abruptly

I have a scenario where during execution of a batch file, it navigates to a different folder (say to "../asdf"); and at the end of execution it will set the current working dir as the same folder from where the user called the .bat file.
But if the user terminates the batch processing before it is complete, the cmd show the current working dir (say "../asdf").
But in my case, I need to restore the working dir to the default/predefined one. Is it possible?
Batch file is written by me, so I can modify it.
CMD is opened through a desktop shortcut to CMD, which I have control of; so properties like working dir or passing args to CMD etc can be done there.
In your batch script use setlocal to encapsulate the running environment of your batch session. If the user terminates the script before you cd or popd to return, your script will still exit in the directory in which it started. Here's a brief test:
#echo off
setlocal
pushd c:\Users
cd
exit /b
Output:
C:\Users\me\Desktop>test.bat
c:\Users
C:\Users\me\Desktop>
Notice I didn't popd or cd %userprofile%\Desktop, but I still ended up back at my Desktop after the script exited.
Additionally, setlocal keeps you from junking up your environment with orphaned variables that mean nothing outside of your batch script. It's just good practice. At the console, type help setlocal for more info.

Running vbscript from batch file

I just need to write a simple batch file just to run a vbscript. Both the vbscript and the batch file are in the same folder and is in the SysWOW64 directory as the vbscript can only be execute in that directory. Currently my batch file is as follows:
#echo off
%WINDIR%\SysWOW64\cmd.exe
cscript necdaily.vbs
But the vbscript wasn't executed and just the command prompt is open. Can anyone tell me how can i execute the vbscript when i run this batch file?
You can use %~dp0 to get the path of the currently running batch file.
Edited to change directory to the VBS location before running
If you want the VBS to synchronously run in the same window, then
#echo off
pushd %~dp0
cscript necdaily.vbs
If you want the VBS to synchronously run in a new window, then
#echo off
pushd %~dp0
start /wait "" cmd /c cscript necdaily.vbs
If you want the VBS to asynchronously run in the same window, then
#echo off
pushd %~dp0
start /b "" cscript necdaily.vbs
If you want the VBS to asynchronously run in a new window, then
#echo off
pushd %~dp0
start "" cmd /c cscript necdaily.vbs
This is the command for the batch file and it can run the vbscript.
C:\Windows\SysWOW64\cmd.exe /c cscript C:\Windows\SysWOW64\...\necdaily.vbs
Just try this code:
start "" "C:\Users\DiPesh\Desktop\vbscript\welcome.vbs"
and save as .bat, it works for me
Batch files are processed row by row and terminate whenever you call an executable directly.
- To make the batch file wait for the process to terminate and continue, put call in front of it.
- To make the batch file continue without waiting, put start "" in front of it.
I recommend using this single line script to accomplish your goal:
#call cscript "%~dp0necdaily.vbs"
(because this is a single line, you can use # instead of #echo off)
If you believe your script can only be called from the SysWOW64 versions of cmd.exe, you might try:
#%WINDIR%\SysWOW64\cmd.exe /c call cscript "%~dp0necdaily.vbs"
If you need the window to remain, you can replace /c with /k
Well i am trying to open a .vbs within a batch file without having to click open but the answer to this question is ...
SET APPDATA=%CD%
start (your file here without the brackets with a .vbs if it is a vbd file)
You should put your .bat file in the same folder as your .vbs file and call the following code inside the .bat file.
start cscript C:\filePath\File.vbs

Resources