Cannot run python server using batch file - windows

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.

Related

How to pause a batch script on a command

I have a below batch script:
#echo off
cd C:\Program Files (x86)\Project
CALL init_env.bat
cd C:\Users\Documents\Project\Release
START app.exe -ver 133
echo application has been stopped
This starts the app.exe. On the terminal it also says application has been stopped. Is there any way I can pause the script after executing the line START app.exe -ver 133 and only run echo application has been stopped when the app was closed or crashed for some reason.
Thanks
The command START should be removed because it starts the executable app.exe as separate process being executed parallel to cmd.exe processing the batch file. Then cmd.exe processing the batch file starts app.exe and waits with further processing of the batch file until app.exe terminated itself.
It is also recommended to always enclose a file/folder string in " even on not really being necessary. So cd /D "C:\Program Files (x86)\Project" and call "init_env.bat" should beĀ used or even better call "C:\Program Files (x86)\Project\init_env.bat" if that works also because of batch file init_env.bat is written to work independent on what is the current directory on execution. And last cd /D "C:\Users\Documents\Project\Release" and "app.exe" -ver 133 should be written in the batch file.
Run in a command prompt window cd /? for help on command CD and meaning of option /D.
start /? outputs the help for command START in cmd window.
#echo off
call "C:\Program Files (x86)\Project\init_env.bat"
cd /D "C:\Users\Documents\Project\Release"
"app.exe" -ver 133
echo application has been stopped
See also the predefined Windows environment variables.

CMD script that goes to a folder

I'm trying to make a CMD script that will do CD Desktop\Crunchyroll\pyscripts and enter it, then I want to be able to type a command. Is there a way I can make a script to start in the Desktop\Crunchyroll\pyscripts directory on my computer?
Put this into a blank text file called MyScript.CMD
Double click it.
#echo off
CMD /K CD /D "%userprofile%\Desktop\Crunchyroll\pyscripts"
To create a symbolic link on Windows, open Command Prompt (as an Administrator). Let's say your username is Jason. And the symlink we will name 'code'.
cd \Users\Jason
mklink /D code Desktop\Crunchyroll\pyscripts
cd code
dir
All your files in pyscripts will be listed. You basically have created a shortcut to your pyscripts.
Personally, I'd simply create a shortcut to cmd.exe and set Properties/shortcut/Startin to Desktop\Crunchyroll\pyscripts.
If you really want to jump to pyscripts from within an existing cmd instance, then create a textfile named 'pyscripts.bat' containing
#echo off
CD "%userprofile%\Desktop\Crunchyroll\pyscripts"
and store it somewhere in your PATH.
Then executing pyscripts from within a cmd instance will jump to the indicated directory and
....
PUSHD
call pyscripts
{do some python}
POPD
...
should allow you to execute python from that directory within a batch file.

Batch File to change directory and run a command

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.

How to end a batch file with an open command window at a certain folder location

This seems like it should be ridiculously simple but I cannot find the answer online or in these forums.
I want to run a command from a batch file, leave the command window open and end at a particular file location.
I can't seem to get both to happen in the same window. This is for a script to run an automated task everytime and leave the window open to run a 2nd task that has a variable input.
start cmd /k c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\
If I run either by itself, they work (runs the exe, ends at /desktop prompt), but in this sequence only the first runs.
this works here:
#ECHO OFF
START /b "" "path\program.exe" "parameter"
CD %UserProfile%\Desktop
Do not use setlocal in your Batch or put endlocal in the line before the CD command.
This should work:
start cmd /k "c:\users\test\desktop\dmiwtool1.1\win64\dmiwtoolx64.exe & cd c:\users\test\desktop\dmiwtool1.1\win64\"
If you leave out the quote marks, the start command and the cd command are run separately.

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.

Resources