Execute multi command in batch - batch-file

I tried to run multi-windows commands inside one opened window from a batch file.
I want the opened command window to perform two things in sequence:
Switch volume
Direct to a directory in that volume.
Here's what I wrote:
start cmd /k C: && cd 'C:\Program Files (x86)\aaa\'
However, this only switches volume. The second thing is not executed.
Can anyone please show me the way?

Well, you have at least 2 options...:
1st, make sure your && is passed to new cmd...
start cmd /k "C: && CD c:\temp"
2nd, use /d switch on cd to "get there" in one step...
start cmd /k cd /d c:\temp
KR
Bartek

What don't you just open your cmd at the needed directory? Like^
start /dc:\temp cmd

If you want to change directory to another drive you can use
cd /d C:\
but if your changing directory within the same drive you shouldn't need to switch drives, just change to that directory:
cd "C:\Program Files (x86)\aaa"
Remember to put quotes around paths with spaces, possibly why your command didn't work earlier.
Also, you shouldn't really need start and cmd. What you doing doesn't really need to be threaded as such. If it's a batch file you can just use pause at the end instead of using cmd /k.
Your complete batch file would then look like this:
cd "C:\Program Files (x86)\aaa"
pause >nul
or using cmd /k for one line (in case of command line use):
cmd /k cd "C:\Program Files (x86)\aaa"
Hope this helps!

Related

Cannot start .exe file propetly because config

So...I have "launcher"..
Its .bat file and i want it to start /ffa/server.exe
but..In ffa/ i have config file.
When i start server via launcher it starts server but it makes new config file
in directory of launcher..How can i fix this?
And "server" its: https://github.com/OgarProject/Ogar
start cmd /k %~dp0\ffa\server.exe
Please help me, its really frustrating..Thanks <3!
You can use pushd to move to the correct directory, start the server, and then pop back:
pushd %~dp0\ffa
start cmd /k server.exe
popd
I'm not familiar with the exact folder structure you're working with, and exactly how you call the script, but you definitely could use push/popd for this.
There are more possible solutions to change working directory of started command prompt window:
In calling script
pushd %~dp0\ffa
start "" cmd /k server.exe
popd
In started cmd itself: note properly escaped & character (see redirection)
start "" cmd /k pushd %~dp0\ffa^&server.exe
Using /D parameter of start command
start "" /D "%~dp0\ffa" cmd /k server.exe

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Multiple commands from cmd.exe /k with & does not work

Apologies if this is answered somewhere, been searching for 30+ mins to no avail.
So I have a batch file, from within it I call:
%comspec% /K "CD ..\..\test\java_6_86 & "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java"
However that does not work.
What I am trying to do is:
(Within a batch file)
Open a new command prompt
Change the Current Directory to the directory where Test1.java lives
Then call the java compiler and have it compile Test1.java
Also, is it possible to tell command where to start, instead of having to do a CD as the fist command?
Thanks,
DoW
I do not know what your %comspec% is but suppose it is 'cmd' this should do the trick (the cmd /K has to be in front of the javac call)
cd ..\..\test\java_6_86 & cmd /K "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java
By the way, a two liner would be much more readable
cd ..\..\test\java_6_86
cmd /K "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java
You can of course use absolute pathes to specify your classpath and java Source file and skip the directory change.
To answer your second question, you could use pushd/popd. As name suggests, those allow to store current directory, then change to given path (pushd path) and then popd pops/restores the original current dir. It's useful if you need to temporarily change current directory to do some processing or you want to shield your code from unwanted directory changes (eg. if you call another batch). It's also handy with network paths as it auto-creates a drive letter for it. Help pushd will give you full info.

Start cmd.exe and change directory?

I'd like a .bat file which is stored on the desktop to perform 2 simple tasks:
1. Start cmd.exe
2. Change directory to c:\executionsdktest_10.2.2
I have:
#echo off
start cmd.exe \k
cdsdad c:\ExecutionSDKTest_10.2.2
But when I double click the .bat file, this starts cmd.exe but cd to c:\users\qestester\desktop. ANy ideas?
You can use
cmd /k "cd /d c:\ExecutionSDKTest_10.2.2"
And you wouldn't need a batch file for that. This can be put in a normal shortcut.
And if you have a normal shortcut you can just specify its working directory and run cmd directly without any arguments.
This worked for me:
start cmd.exe #cmd /k "cd /d C:\Users\Michael && node test.js"
What I needed ... was from a localhost page served in PHP, open a terminal, change directory, and start a node script. Achieved like this:
pclose(popen("start /B ". $cmd, "r"));
.. where $cmd is the first string above.
start cmd.exe /k "C: && cd \ExecutionSDKTest_10.2.2"
I wanted to:
open a command prompt
change directory (go to another directory)
run a command from there
solution that worked for me:
#echo off
d:
cd\Path\to\wherever
my command
Notes:
the d: after #echo off tells that the path is on the D drive. Write a c instead of the d and You will be on drive C.
third line starts with the mandatory cd, after which starts the path, with a preceding \.
my command can be anything you want, and can contain more that one word (or only one). I used it to run jupyter notebook.

Resources