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.
Related
os.execute("start cmd /k cd C:/path/to/js/file/node index.js")
os.execute("start cmd /k cd C:/path/to/js/file/ & node index.js")
I need to execute a node.js script.
This does not work. Help me please!
You need to enclose the whole sequence of commands in quotes:
os.execute('start cmd /k "cd C:/path/to/js/file/ & node index.js"')
If the first command is cd you can move it into start /D option as Mofi pointed out.
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"
All I want to do is:
change to specific directory of a different drive
run a command in that directory e.g. dir
I need to do this in one line using cmd.exe starting from a different drive
I would do this like this:
c:
cd temp
dir
so in one statement so far I have:
cmd /c c: & cd\temp & dir
But this just gives me dir for the P: directory which I start from. How can I get dir returned from c:\temp?
I can't run a batch file and it must be in a one-line statement.
You may want to invoke CD with the /d option, thus not only changing the current directory on drive c: but also going there (in case you are not already on that drive).
cmd /c "cd /d c:\temp && dir"
you use && or & to separate multiple commands
if the cmd window is already opened and running from command line
c: && cd\temp && dir
or
cmd /c && c: && cd\temp && dir
You want quotes around that command line:
cmd /c "cd c:/ & dir"
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
I'm terribly new to scripting on windows. Using windows 7 64.
I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me to a certain directory.
I tried making a .bat file with
#ECHO OFF
cmd "cd C:\my\destination"
Which opens what looks like a command prompt, but doesn't seem to let me type any commands.
I then tried:
#ECHO OFF
start cmd "cd C:\my\destination"
But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.
This works for me:
#ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"
The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.
Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.
Use the /K switch:
#ECHO OFF
start cmd.exe /K "cd C:\my\destination"
But IMHO, the most useful switch is /?.
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/S Modifies the treatment of string after /C or /K (see below)
/Q Turns echo off
...
And only if it does not work, then Google it, as #Neeraj suggested :D
This could be done like that:
#ECHO OFF
cd /D "C:\my\destination"
cmd.exe
If you need to execute a file or command after you open the cmd you can just replace the last line with:
cmd.exe /k myCommand
#ECHO OFF
%comspec% /K "cd /D d:\somefolder"
The /D will change folder and drive and works on 2000+ (Not sure about NT4)
If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\" but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheck registry entry...
Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
You can create a batch file "go-to-folder.bat" with the following statements:
rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd