I would like to run my batch script from anywhere, for example:
C:\>test123
And that command line executes my batch script C:\Documents\test123.bat.
I tried to do it with environment variable path, but it doesn't seem to work.
Add your bat to PATH and make sure PATHEXT contains .BAT
How to add your program to the PATH env variable
Note, that the current working directory will be whereever you called it from.
If you want relative paths to where the bat is stored, use %~dp0
Please post the output of echo %PATHEXT% %PATH% and tell us the exact location of your bat file and more error info why it failed in your case?
Adding to C:\Windows\System32
Worked for Me.
System info:
Windows 10 with Admin access.
Related
I have this simple batch file (test.bat):
echo %CD%
pause
No matter where I run this .bat file from (i.e. C:\some\dir\test.bat), my system tells me that I'm in C:\ . This is obviously neither the intended nor the desired behavior, and if I try the exact same script on a different machine, it behaves as expected, giving me current directory of the .bat file. I've been googling for hours now and haven't been able to find anything relevant to my issue. Perhaps I missed up a windows setting somewhere? I have no idea, anyone have any ideas?
Found the issue: there was an AutoRun registry entry that would change directory to C:\ whenever a shell was opened. facepalm
I have a very simple Windows .BAT file:
set PATH=c:\xxx;%PATH%
call foo.pl
set VAR=true
I thought "call" will start a new batch process, without affecting the current one. However, the batch file exited immediately after the foo.pl finished executing. The set VAR=true has never been called.
Is there a way to fix it?
foo.pl is not a batch file, it is a Perl script.
So you need to use
path c:\xxx;%PATH%
"Path to\Folder With\perl.exe" "foo.pl"
rem Additional batch code executed after Perl script execution finished.
In other words you have to run the console application perl.exe best with full path, or with just perl.exe if program files folder of Perl is not the same on all computers on which this batch file is used and hopefully PATH contains also the directory containing perl.exe.
If you specify on a command line or in a batch file just foo.pl, Windows looks in Windows registry which application is associated with .pl for action Open. If there is such a file association, Windows runs this application in a separate process like when using command start.
So using call foo.pl is like using start foo.pl.
PATH is not only an environment variable, but also an interal command written for changing the value of environment variable PATH at any time within a batch file. This is the reason why I removed set from first line. It is better to use internal command path for modifying environment variable PATH.
I'm working with installshield and have a group of batch files that I want to run as part of the install process. Instead of executing each batch file from installshield I want to create one batch file that executes all of the batch files.
The issue I have is that the calling batch file sits two directories up from the others. When the batch file tries to call the others they fail to run because they can not find the resources that they need. It seems that when they are executed from the batch file two directories up they are for some reason using the relative path of the calling batch file. Is my assumption correct?
One of the batch files that I am calling is a batch file to star an h2 database the call looks like this:
call h2\bin\h2.bat
If I go to the /h2/bin directory in a command prompt the h2.bat runs fine but once I run it from the calling batch file this is the error that I get.
Error: Could not find or load main class org.h2.tools.Console
How do I call one batch file from another without using the calling batch files path?
Explanation
It seems that when they are executed from the batch file two
directories up they are for some reason using the relative path of the
calling batch file. Is my assumption correct?
Yes your assumption is correct. Calling a batch file will not change the current working directory. The main batch file will be found because you are providing the correct relative path, but all the other relative paths will be seen from the perspective of your current working directory, not from the directory that contains the main batch file.
%~dp0 is your friend, it yields the drive letter and path to the batch file containing that character sequence. Use it as a basis for relative paths and your batch files will work no matter who calls them from where.
Example:
Fictitious h2.bat that won't work:
#echo off
h2.exe start
Working h2.bat:
#echo off
"%~dp0\h2.exe" start
See What does %~dp0 mean, and how does it work? for more explanations on %~dp0
Try setting the directory:
cd ht\bin\
call h2.bat
cd %HOMEPATH%
REM just reset to where ever you were before.
If that doesn't work, try using the C:// prefix in your path. That might/might not work.
Good Luck!
Suppose current .bat file is running in C drive and you want to run .bat file placed in D: directory then in first .bat write.
D:
cd "D:/folder/folder2/"
call batFile.bat
It might be because you don't have permission. M facing the same problem and i found the solution like this -
Right click on your task than properties.
In properties click on General tab and then click on 'User Group or User' and select appropriate user.
Or create a another bat file to call your bat file and schedule that file. you can create the bat file like this -
open Notepad and give your original bat file path and then call bat file with name like -
D:
cd "E:/ABC/FirstJob/main/"
call main_run.bat
Now save this file with .bat extension.
if your bat file is correct, try cmd command as below and hit enter(tried in windows 10):
"\h2.bat"
e.g: "C:\Users..\bin\h2.bat"
I tried :
pushd h2\bin\
call h2.bat
=> It 's okay.
My emacs.bat rest in the same bin directory as emacs.exe. This directory is included in the PATH variable (Windows).
Content of emacs.bat:
#echo off "%~dp0emacsclientw.exe" -na "%~dp0runemacs.exe" "%1"
However, whenever I use this command, emacs.exe gets executed instead of the .bat file. How would I go about fixing this?
It seems you could put the emacs.bat in another directory and place that directory earlier in the PATH. Note: if you were to run "emacs" while in the directory containing emacs.exe then the .exe would take precedence.
Edit: Alternatively, you could just type emacs.bat instead of emacs at the command prompt.
Edit 2: As seen in this Answer, you could set the PATHEXT environment variable. Something along the lines of:
PATHEXT=.BAT;.COM;.EXE;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.RB;.RBW
I have a Batch file which I want to execute in CMD from any directory. Something like this:
File name: MyBatch
Path: C:\MyBatch.bat
Open CMD:
c:\Program Files> MyBatch
How can I accomplish this?
Set that location in your PATH environmental variable.
I wouldn't put it the root or the system directory.
I keep a directory with all my scripts in C:\DRR\CMD
and either set it in the MyComputer GUI or run at the command script:
set PATH=%PATH%;C:\DRR\CMD
You could just put it in your c:\windows\system32 directory, as its always in the system path.
How about...
"%MyBatch%"? (the double qoutes are intended) That should work!
to change your Variable, use set MyBatch="Path\Whatever.bat"
and to ask the user for a String, use set /p MyBatch="Question? "
-- or, you can use a BAT-to-EXE converter to run the batch in a Executable.
You would need to set the PATH environment variable to include the path to your batch file
If you are talking Windows, then the PATH Environment variable is what you need to set.
The path where your bat file is placed should be appended to the PATH variable.
In your example append "C:\;" in the value for Path environment variable.
Then you can execute MyBatch.bat from anywhere on the command line.
Create a folder called Batches (lets say in your C drive).
Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.