How do I run another command in a cmd batch file? - windows

I have this command that i need to run
cd /d C:\leads\ssh & C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby192\bin\setrbvars.bat
this opens the command prompt
but I need to run this command in the prompt
ruby C:\lead\leads.rb
which will fire off a script.....but i have no idea what to add to my bat file to do this
i tried the -f flag to tell it to run the command but no go....any ideas to what to do to make this run
cd /d C:\leads\ssh & C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby192\bin\setrbvars.bat -f ruby C:\lead\leads.rb
pause

Try the following batch file:
#echo off
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\lead\leads.rb

Related

Batch script does not close cmd after execution

I have the below code which starts the siebel tools from a batch script:
#ECHO OFF
D:
CD D:\Siebel\8.1.1.16.0\Tools\BIN\
ECHO Starting Siebel Tools
siebdev.exe /c D:\Siebel\8.1.1.16.0\Tools\CFG\tools.cfg /d DataSrc /u ADMIN /p PASSWORD
cmd
But this does not close the command prompt. Please help.
this alone should be fine, without cmd in the end line
#ECHO OFF
CD /d D:\Siebel\8.1.1.16.0\Tools\BIN\
ECHO Starting Siebel Tools
siebdev.exe /c D:\Siebel\8.1.1.16.0\Tools\CFG\tools.cfg /d DataSrc /u ADMIN /p PASSWORD
just to add,
/d can be used after cd instead of D: and then using cd

batch windows command written in prompt but no execution

To avoid to repeat a task too often, I am setting up a batch file (in WINDOWS 10). It opens several CMD PROMPT to a specific Directory and launch a command.
For one case, I want the CMD PROMPT to open, to go to the specific directory and to set the COMMAND in the PROMPT without launching it. Then I'd just have to click on ENTER to launch that command whenever I want later on.
Here is my code:
setlocal ENABLEEXTENSIONS
set CordovaProjPath="C:\MyPath\"
start cmd /k "cd /d %CordovaProjPath% && cordova build android"
With this code it launches the command "cordova build android".
If I go with start cmd /k "cd /d %JCACordovaProjPath% instead of start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" it gives me the PROMPT with: "C:\MyPath>", I'd like to write: "cordova build android" behind it without launching the command.
Any idea?
To provide repeatable execution (as mentioned in comments) you can put the relevant commands in a loop with a "quit" option:
#Echo Off
setlocal
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
:loop
Cd /D %CordovaProjPath%
Echo %CommandToRun%
set QUIT=
set /p QUIT=Press ENTER to run command or 'Q' to quit:
if /i "%QUIT%" == "Q" goto :eof
%CommandToRun%
goto :loop
Unlike the original, this runs the target command in the same command-window as the repeating loop. Depending on what the command in question does, this may be more attractive (less windows popping-up). However, some commands may cause the main window to close; if this is the case, you can revert to running the command in its own window in one of two different ways. In each case, replace the line:
...
%CommandToRun%
...
Run in own window and remain open
...
start "%CommandToRun%" /wait cmd /k %CommandToRun%
...
Using /k will leave the command-prompt window open after the target command has run -- this may be appropriate if you need to see the output of the command and it does not have its own pause.
Run in own window then close
...
start "%CommandToRun%" /wait cmd /c %CommandToRun%
...
Using /c will mean the command-prompt will close after the target command has run. This may be appropriate if you do not need to see the output of the command, or if it has its own pause.
Would something like this do you:
#Echo Off
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
Start "%CommandToRun%" Cmd /K "Cd /D %CordovaProjPath%&Echo %CommandToRun%&Pause>Nul&%CommandToRun%"
Below is an alternative which may allow for your alternative double-quoting method:
#Echo Off
Set CordovaProjPath="C:\MyPath"
Set CommandToRun="cordova build android"
Start %CommandToRun% Cmd /K "(Cd /D %CordovaProjPath%)&(Echo %CommandToRun%)&(Pause>Nul)&(%CommandToRun%)"

Open cmd, change directory and execute command

I'm trying to write a simple batch file to initialize some tools I need on windows startup.
This is what I've at the moment:
#echo off
start /d "C:\Program Files\Sublime Text 3" sublime_text.exe
start cmd.exe /k cd /d "D:xampp/htdocs/webshop"
What I'd like to do is to execute the command compass watch once the directory has changed.
I tried start cmd.exe /k cd /d "D:xampp/htdocs/webshop" /k "compass watch" but it refers to the cd command then and thus throws me an error message (The system cannot find the path specified).
Any suggestions?
EDIT
To clarify what I need:
Open console
cd to the relevant directory
Execute the command compass watch (in that directory)
I normally do this by manually typing in the commands into the console as listed above. What I'd like to have is a simple .bat file that does exactly that with just one click.
You state in the comments that you don't need a separate interpreter. In which case I believe you can do it like this:
#echo off
start /d "C:\Program Files\Sublime Text 3" sublime_text.exe
start /d D:\xampp\htdocs\webshop compass watch
This an example to open Firefox.exe.So you should do like this, for your programs
#echo off
echo Try to open Firefox ....
CD /D %programfiles%\Mozilla Firefox\ & Start Firefox.exe
Pause
Try This :
#echo off
CD /D %programfiles%\Sublime Text 3 & Start sublime_text.exe
CD /D D:\xampp/htdocs/webshop & Start compass watch
Pause
You have to use Backslash in your path "\".
Did you try :
#echo off
"C:\Program Files\Sublime Text 3\sublime_text.exe"
cd /d "D:\xampp\htdocs\webshop"
"D:\xampp\htdocs\webshop\compass watch.exe"
This should work to launch the exe, and change folder, then use cmd /k to execute the compass command and leave the console open.
#echo off
start "" /d "C:\Program Files\Sublime Text 3" sublime_text.exe
cd /d "D:xampp/htdocs/webshop"
cmd.exe /k "compass watch"

cmd.exe /k switch

I am trying to switch to a directory using cmd and then execute a batch file
e.g.
cmd /k cd "C:\myfolder"
startbatch.bat
I have also tried (without success)
cmd cd /k cd "C:\myfolder" | startbatch.bat
Although the first line (cmd /k) seems to run ok, but the second command is never run. I am using Vista as the OS
Correct syntax is:
cmd /k "cd /d c:\myfolder && startbatch.bat"
ssg already posted correct answer. I would only add /d switch to cd command (eg. cd /d drive:\directory). This ensures the command works in case current directory is on different drive than the directory you want to cd to.
cmd cd /k "cd C:\myfolder; startbatch.bat"
or, why don't you run cmd /k c:\myfolder\startbatch.bat, and do cd c:\myfolder in the .bat file?
I can't see an answer addressing this, so if anyone needs to access a directory that has space in its name, you can add additional quotes, for example
cmd.exe /K """C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"" & powershell.exe"
From PowerShell you need to escape the quotes using the backquote `
cmd.exe /K "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat`" & powershell.exe"
Notice the escaped quotes
`"
inside the path string:
"`"C:\my path\`""
This will execute the proper command in cmd, i.e. the path surrounded with quotes which should work.
The example command above will initialise the MSVC developer command prompt and go back to PowerShell, inheriting the environment and giving access to the MSVC tools.
You can use & or && as commands separator in Windows.
Example:
cmd cd /K "cd C:\myfolder && startbatch.bat"
I give this as an answer because I saw this question in a comment and cannot comment yet.
cmd /k "cd c:\myfolder & startbatch.bat"
works, and if you have spaces:
cmd /k "cd "c:\myfolder" & startbatch.bat"
As I understand it, the command is passed to cmd as "cd "c:\myfolder" & startbatch.bat", which is then broken down into cd "c:\myfolder" & startbatch.bat at which point the remaining " " takes care of the path as string.
You can also use &&, | and || depending on what you want to achieve.

How to set working directory of cmd, at the same time as executing a program

I currently have a command that I thought did this:
START /WAIT cmd.exe /d tester test.cmd
Although it does not appear to work, it only changes the directory, not executing test.cmd
Try with:
start /wait /d tester cmd.exe "/c test.cmd"
Or simply:
start /wait /d tester test.cmd

Resources