Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 7 Command Prompt: How do I execute a batch script from the command line?

I'm using Windows 7, and my problem is running this file from a console (cmd.exe):

W:\software\projects\myproject\build\msvc\build.bat

When I move into the folder containing the file manually and run it from there using the following command sequence, it works:

W:\>cd software
W:\software>cd projects
W:\software\projects>cd myproject
W:\software\projects\myproject>cd build
W:\software\projects\myproject\build>cd msvc
W:\software\projects\myproject\build\msvc>build.bat

However, when I try to run the file from the root directory in any of these ways:

W:\>software\projects\myproject\build\msvc\build.bat
W:\>call software\projects\myproject\build\msvc\build.bat
W:\>@call software\projects\myproject\build\msvc\build.bat
W:\>"software\projects\myproject\build\msvc\build.bat"
W:\>call "software\projects\myproject\build\msvc\build.bat"
W:\>@call "software\projects\myproject\build\msvc\build.bat"

I get the following error message:

The system cannot find the path specified.

I'm pretty sure you didn't have to navigate to the folder containing the file in order to run it when I was using Windows XP (though I could be wrong, of course), but this apparently seems to be the case with Windows 7. Or am I missing something?

like image 549
Dragonion Avatar asked Sep 06 '12 06:09

Dragonion


1 Answers

You are correct. You do not need to navigate to the batch scripts folder before executing. The error "The system cannot find the path specified." is most likely caused by something inside your batch-file.

Try to add

cd W:\software\projects\myproject\build\msvc
w:

or in a single command (as suggested by James K, Thanks!)

cd /d W:\software\projects\myproject\build\msvc

Searched a bit more and found this generic solution:

cd /d %~dp0

at the top of your batch file to set the working directory to the directory of the script to check whether this is the cause.

If you execute your file from W:\ this is where the commands are executed (working directory). It is most likely that your script cannot find some file it uses in this location.

like image 166
konqi Avatar answered Nov 18 '22 16:11

konqi