Batch can't find .exe files inside C:\Program Files (x86) - windows

This is the code i'm using:
cd "D:\HigherFolder\FolderX"
start executable1.exe
cd "C:\Program Files (x86)\FolderY\"
start executable2.exe
cd "C:\Program Files (x86)\FolderZ\bin\"
start executable3.exe
exit
I want to start one .exe after the another, or at the same time, but this does not work with executables 2 and 3; command prompt says it can't find the files and that i need to certify that their names are correct (which i did, multiple times).

Change it to cd /d on command #2. (You can add it to commands 1 & 3 also for safety.) Your code operates on two separate drives, but CD only changes directories on the current drive without the /d switch.
cd /d "D:\HigherFolder\FolderX"
start executable1.exe
cd /d "C:\Program Files (x86)\FolderY\"
start executable2.exe
cd /d "C:\Program Files (x86)\FolderZ\bin\"
start executable3.exe
exit
Run cd /? from a command prompt for more info. An excerpt shows:
Use the /D switch to change current drive in addition to changing current
directory for a drive.

Related

How can I create a windows 10 script which runs a command under specified path?

I created a npmruns.bat file with a content:
C:\myfolder>npm run s
I wanted to create a script file which run a command: npm run s under specified location: C:\myfolder , but it doesn't work. I run it by double click (I have an admin rights).
I tried to create a script which can be executed from any location (other than C:\myfolder).
pushd and cd /d will be the options for this:
#echo off
pushd "C:\myfolder"
call npm.cmd run s
popd
This will push to the path of the batch-file as the working directory, popd is not required if you do not need to go back to the starting working directory, which as admin, will be "%systemroot%\system32"
Alternatively you can run:
cd /d "c:\myfolder"

how to change directory before running command in windows command prompt

# run.bat
cmd /k python.exe "C:\Program Files (x86)\XXX\test.py"
cmd /k cd "C:\Program Files (x86)\XXX\" & python.exe "C:\Program Files (x86)\XXX\test.py"
cmd /k "cd "C:\Program Files (x86)\XXX\" & python.exe "C:\Program Files (x86)\XXX\test.py""
Question> I need to change the working directory to C:\Program Files (x86)\XXX\ then run the command python.exe "C:\Program Files (x86)\XXX\test.py". Several methods have been tested and none of them give me the desired result. They all end up run the command without changing the working directory.
For example,
If I run c:\temp\run.bat, I expect the script first to change to directory of C:\Program Files (x86)\XXX\ then run the python script.
Step 1: Find the short names for the directory with space
dir /X
PROGRA~2 Program Files (x86)
Step 2: Quote all commands
cmd /k "cd C:\PROGRA~2\XXX\ & python.exe C:\PROGRA~2\XXX\test.py"

How to run a .bat (that runs an .exe that is in the same directory) as administrator from a pendrive?

I'm able to run a .bat (that runs an .exe that is in the same directory) as administrator: I right-click in the bat file and select "Run as administrator".
To be able to do that, I used the following answer: Run exe from current directory in batch
Here's the code:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
start %1myprogram.exe
exit
However, this will only work if the .bat file and the program are in the system drive.
Because if they are, for instance, in a pendrive, and I right-click and select "Run as Administrator", I get the error:
"Windows cannot find 'myprogram.exe'. Make sure you've typed the name correctly, then try again."
Why this happens and how can I fix it?
I thought that by using cd %~dp0 it would always point to the folder in which the bat .file resides.
Thanks in advance.
Solution
Change cd %~dp0 to cd /d %~dp0
Explanation
When you run something with administrator privileges, the working directory changes to:
'C:\Windows\System32'
Although %~dp0 still points to the drive and the directory containing the batch file, cd %~dp0 does not work, because it only changes the directory, but stays on the same drive.
Using the /d parameter, you can tell the cd-command to change the drive, too.
You may need to tell cd to also change drives:
cd /d %~dp0
If the current drive is C: (e.g., the prompt says C:\>), and you do CD D:\FOO, the current directory on drive D: is set to \FOO, but you will still be "on" drive C:. Try the following:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
%~d0
start %1myprogram.exe
exit
(also, why %1myprogram.exe instead of just myprogram.exe, or even just myprogram? If you're right-clicking on the batch file to run it, there isn't going to be a %1.)

Navigate to a specified path in cmd file

Now I am currently in C:\WINDOWS\system32>
I need to mention it in the cmd file
How can I navigate to a specific path in cmd file.
Please provide the lines.
#echo off
setlocal
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
cd "C:\hadoop-2.6.0-src"
mvn package -Pdist,native-win,docs -DskipTests -Dtar
pause
popd
This is my cmd file.The mvn command is not getting executed in the specified path. two separate command prompts is getting opened but it is not running and get closed immediately
Thanks in advance.
PUSHD Change the current directory/folder and store the previous folder/path for use by the POPD command
#echo off
setlocal
pushd "C:\hadoop-2.6.0-src"
cd
pause
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
cd
pause
popd
Additional cd commands (without parameters) should display the current drive and directory.
Read more

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"

Resources