Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to run a command in cmd within a directory

Tags:

batch-file

I want to have a batch file(must be placed on desktop) which does the following;

  • opens cmd
  • navigates to a directory, e.g. C:\activiti-5.9\setup
  • runs a command within the directory, e.g. ant demo.start (this command runs the activiti server)

I tried the following to reach to the directory but how to run command, "ant demo.start"?

START cmd.exe /k "cd C:\activiti-5.9\setup"

Thank you for the help.

EDIT:

Referring to zb226's answer below: One more question if you can answer me is how to make that cmd to be run as administrator? will the following work?

START cmd /K "runas /user:administrator & cd C:\activiti-5.9\setup & ant demo.start"

like image 974
nommyravian Avatar asked Aug 28 '12 22:08

nommyravian


4 Answers

Chain arbitrary commands using & like this:

command1 & command2 & command3 & ...

Thus, in your particular case, put this line in a batch file on your desktop:

START cmd.exe /k "cd C:\activiti-5.9\setup & ant demo.start"

You can also use && to chain commands, albeit this will perform error checking and the execution chain will break if one of the commands fails. The behaviour is detailed here.

Edit: Intrigued by @James K's comment "You CAN chain the commands, but they will have no effect", I tested some more and to my surprise discovered, that the program I was starting in my original test - firefox.exe - while not existing in a directory in the PATH environment variable, is actually executable anywhere on my system (which really made me wonder - see bottom of answer for explanation). So in fact executing...

START cmd.exe /k "cd C:\progra~1\mozill~1 && firefox"

...didn't prove the solution was working. So I chose another program (nLite) after making sure that it was not executable anywhere on my system:

START cmd.exe /k "cd C:\progra~1\nlite && nlite"

And that works just as my original answer already suggested. A Windows version is not given in the question, but I'm using Windows XP, btw.


If anybody is interested why firefox.exe, while not being in PATH, is executable anywhere on my system - and very probably on yours as well - this is due to a registry key where applications can be registered to be available everywhere. See this SU answer for details.

like image 94
zb226 Avatar answered Nov 13 '22 11:11

zb226


For me, the following is working and running activiti server as well as opening the explorer in browser (with the help of zb226's answer and comment);

START "runas /user:administrator" cmd /K "cd C:\activiti-5.9\setup & ant demo.start"

START /wait localhost:8080/activiti-explorer

like image 40
nommyravian Avatar answered Nov 13 '22 12:11

nommyravian


This question is 5 years old. I wonder why still nobody has found the /d switch to set the working folder:

start /d "c:\activiti-5.9\setup" cmd /k ant demo.start
like image 11
Stephan Avatar answered Nov 13 '22 10:11

Stephan


CMD.EXE will not execute internal commands contained inside the string. Only actual files can be launched with that string.

You will need to actually call a batch file to do what you want.

BAT1.bat

start cmd.exe /k bat2.bat

BAT2.bat

cd C:\activiti-5.9\setup
ant demo.start

You may want to create a folder called BAT, and add it's location to your path. So if you create C:\BAT, add C:\BAT\; to the path. The path is located at:

    click -> Start -> right-click Computer -> Properties ->
    click -> Avanced System Settings -> Environment Variables
   select -> Path (From either list. User Variables are specific to 
                   your profile, System Variables are, duh, system-wide.)
    Click -> Edit
Press the -> the [END] or [HOME] key.
     Type -> C:\BAT\;
    Click -> OK -> OK

Now place all your batch files in C:\BAT and they will be found, regardless of the current directory.

like image 3
James K Avatar answered Nov 13 '22 10:11

James K