ByteArray123
ByteArray123

Reputation: 101

How to call a .bat file from any location in CMD on Windows

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?

Upvotes: 10

Views: 24760

Answers (6)

zulc22
zulc22

Reputation: 337

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.

Upvotes: 1

Sababado
Sababado

Reputation: 2532

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.

Upvotes: 0

Jagmag
Jagmag

Reputation: 10356

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.

Upvotes: 0

Rawheiser
Rawheiser

Reputation: 1218

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

Upvotes: 9

Russ
Russ

Reputation: 4163

You would need to set the PATH environment variable to include the path to your batch file

Upvotes: 0

wfoster
wfoster

Reputation: 791

You could just put it in your c:\windows\system32 directory, as its always in the system path.

Upvotes: 1

Related Questions