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

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?

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.

Related

Command prompt from C: drive, how to start executable in another drive?

I have a Windows machine with the current user in C:\Users\User.
I have an executable in another drive, let's say at D:\Folder\MyProg.exe.
Opening command prompt, it starts in the directory C:\Users\User
I type the command: start D:\Folder\MyProg.exe or D:\Folder\MyProg.exe
The exe fails to open, with a pop-up: MyProg has encountered an error
In order to run start the .exe from command prompt, I have to cd to the other directory and then start the exe.
Opening command prompt, it starts in the directory C:\Users\User
I type the command: cd /d D:\Folder && start MyProg.exe
The exe successfully opens.
Is there a better way to, from C:, start an executable in another drive?
Reproducing
Windows 10 Pro, v1809 (I don't think the version really matters)
My real use case is industrial automation, but one can observe the same result with convert.exe (cnet download link)
As commented by #Mofi, I realized the answer is most likely this:
But some programs are not good coded. Such programs depend on files in directory of the program and do not use appropriate code to reference those files from within the program with program files path, but use instead a relative path
As he instructed in the next comment, start provides a /d parameter that lets you specify a startup directory. Thus, a concise command would be:
start "" /d D:\Folder MyProg.exe
Note: the "" is for the <Title> field. The .exe I am opening is a GUI application (not a console application), so it is not necessary in this case, I just included in case other viewers find this useful in their application.
I have a Windows machine with the current user in C:\Users\User.
I have an executable in another drive, let's say at D:\Folder\MyProg.exe.
Opening command prompt, it starts in the directory C:\Users\User I type the command: start D:\Folder\MyProg.exe The exe fails to open.
In order to run start the .exe from command prompt, I have to cd to the other directory and then start the exe.
Maybe not. Try:
PATH D:\Folder;%Path%
"D:\Folder\MyProg.exe"

Running a batch file through command prompt - system cannot find the path specified

I am trying to uninstall Oracle on this Windows 7 (64 bit) machine by downloading a standalone tool from Oracle, I need to run a batch file that is supposed to uninstall but I am unable to run it.
I tried to open command prompt as administrator and I am trying to run this as below:
As you can clearly see from the screenshot, I am doing a "dir" on the directory and can clearly see the file right there. Not sure what's going on here.
I also tried to run the batch file by double clicking from Windows Explorer and a terminal window opens and closes quickly but the batch file is not doing what it is supposed to do (it is clearly not executing from Windows Explorer).
Can anyone help me with this?
As theB pointed out above in a comment, this worked for me:
Open the bat file in notepad. I'll bet it starts with #echo off, and
that the error is actually coming from inside the batch file. The
error if the batch file itself wasn't found is 'X' is not recognized
as an internal or external command, operable program or batch file
'Run as Administrator' changes the current directory. See my answer here
Difference between "%~dp0" and ".\"?

Exporting Entire Registry To Relative Path

Using the following code
REGEDIT /E C:\output.reg
A registry dump file is created in C directory. It overwrites automatically when a file with the same name exists. When I try changing the output directory to relative path like this
REGEDIT /E output.reg
it wouldn't work.
No file is created but processing takes just as long as usually. Which code can export the whole registry using relative path?
Like requested, the .bat code including debug code:
ECHO %CD%
REGEDIT /E output.reg
PAUSE
Command line output:
C:\Users\Username\Desktop\New_folder>ECHO C:\Users\Username\Desktop\New_folder
C:\Users\Username\Desktop\New_folder
C:\Users\Username\Desktop\New_folder>REGEDIT /E output.reg
C:\Users\Username\Desktop\New_folder>PAUSE
Press any key to exit . . .
Folder before (and after) .bat file execution:
The syntax of the command given in your question is already fine. If you supply a relative path for the output file name then the file will be created relative to the current working directory. This is easily verified from an interactive command prompt.
Whatever is going wrong with your batch script, the problem is not as you have currently surmised. Some of the more obvious explanations include:
The working directory is not what you think it is. Verify it by adding debug code to your script to emit the working directory.
The file is locked for some reason and so regedit cannot write to the file.
You don't have sufficient rights to write to the working directory.
Update
Thanks a lot for your question update. I tried to re-create your scenario (I'm on Windows 7 but I doubt that matters) and indeed I find the same issue as you. The command works fine from an interactive console window, but not when executing in a batch script.
I've no idea why this is, but here a simple enough workaround is to supply a full path:
REGEDIT /E %cd%\output.reg

how to start executable from command line in windows

I have a program when I click it, it opens fine.But when I run it command prompt it gives an error that is related to a dll that my program uses.I do in cmd
"Path_to_program\program.exe"
And a note:my program is installed my D: drive
But it gives an error related to dll.the dll is in the same directory with program.I guess it is related to dll.is there an option to give the dll as dependency to my command?
You need to change to the same folder that the program is in so that your working directory matches.
Try
cd Path_to_program
program.exe

sqlite from command prompt- not recognized command

I am trying to run sqlite from the command prompt.
So I have this downloaded sqlite-shell-win32-x86-3071100.zip from this website http://www.sqlite.org/download.html
Now there is a set up.exe from which v can run sqlite commands.
When I say
C:\Users\..>'sqlite3' is not recognized as an internal or external com
''sqlite3'' is not recognized as an internal or external command,
operable program or batch file.
Any hints.
Thank you
Sun
From your windows command prompt, you can start sqlite3 either with:
cd c:\Stuff
sqlite3.exe
or with:
c:\Stuff\sqlite3.exe
Either way, I assume from your comment that sqlite3.exe is in c:\Stuff.
As Michael mentioned, you can also add the path of the directory containing sqlite3.exe to your PATH. Fro a quick search I found this guide: http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx
I assume that you already have sqlite in your system path since the instructions on how to do this are clear.
The likely problem is that you have created a folder called sql3 and have put the three executable files in a folder within that folder.
Check that all three executable files are within the file itself.
Go to an environment variable and add new environment variable C:\sqlite as I have saved the sqlite3 files under sqlite folder of C drive. It worked for me
It maybe possible that when u have extracted files from the downloaded zip folder,2 folders are created of the same name one inside the other. So in the cmd prompt u have to enter that folder 2 times by using .
E.g-C:....\sqlite-tools-win32-x86-3330000\sqlite-tools-win32-x86-3330000
and then use sqlite3
If your sqlite3.exe is in C:\Stuff, you can check really quick to see if it's in your PATH.
echo %PATH% will give you your full path. If you don't see ;C:\Stuff there (probably near the end), that's why windows can't find sqlite3.exe.
If you don't see it, run:
setx path "%PATH%;C:\Stuff"
That should be problem solved. Open a new command prompt (it only updates PATHS when the terminal opens), run echo %PATH% and you should now see ;C:\Stuff at the end.
Congrats. sqlite should work in the terminal moving forward.

Resources