Windows bat script directory

How do I run a BAT script without changing directories?

How do I run a BAT script without changing directories?

I am in ./a and the script cd’s into ./a/bc . If I need to terminate my script for whatever reason I am now in bc instead of a . How do I run the script and not have my folder change?

Also, I don’t like how it asks me if I’d like to terminate my script. Can I disable that and let it terminate?

4 Answers 4

If you don’t need to propagate environment variable changes into the current environment and cannot touch the batch file (to use the pushd / popd variant which I usually use), you can still spawn a new instance of cmd :

Also has the nice property of leaving your original batch file running even if the called batch throws up errors. I do that in my batch unit testing framework, for example, to ensure that a failing batch file won’t stop the tests from executing.

You can run your script with start yourscript.bat . This makes it run in a new command window, and therefore does not affect the working directory of the command prompt that started the script.

Another possibility is to not use cd and use absolute paths instead.

The first question is: why do you need to change the directory? Can you simply work with paths relative to the one of your batch file? (e.g. using %

dp0\a\bc to reference the directory)

But if you really, really need to do that, you can do the following:

Get current batchfile directory

Firstly, I saw this topic but I couldn’t understand that.

Question :

There is a batch file in D:\path\to\file.bat with following content :

It must be D:\path\to

What am I doing wrong?

4 Answers 4

System read-only variable %CD% keeps the path of the caller of the batch, not the batch file location.

You can get the name of the batch script itself as typed by the user with %0 (e.g. scripts\mybatch.bat ). Parameter extensions can be applied to this so %

dp0 will return the Drive and Path to the batch script (e.g. W:\scripts\ ) and %

f0 will return the full pathname (e.g. W:\scripts\mybatch.cmd ).

You can refer to other files in the same folder as the batch script by using this syntax:

This can even be used in a subroutine, Echo %0 will give the call label but, echo «%

nx0″ will give you the filename of the batch script.

When the %0 variable is expanded, the result is enclosed in quotation marks.

Читайте также:  Linux nano очистить файл

dp0 will return path to batch location. echo %

f0 will return path to the batch with filename. – Stoleg Jun 12 ’13 at 12:03

dp0 ? – Ivailo Bardarov May 5 ’17 at 10:06

dp0 as first line of batch file and worked – mkb Oct 25 ’17 at 4:03

dp0″ rather than %

dp0 . Quotes will guarantee the path is taken as one token, even if there are spaces, and also protects against poison characters like & . – dbenham Dec 20 ’19 at 3:53

Within your .bat file:

You can now use the variable %mypath% to reference the file path to the .bat file. To verify the path is correct:

For example, a file called DIR.bat with the following contents

run from the directory g:\test\bat will echo that path in the DOS command window.

Here’s what I use at the top of all my batch files. I just copy/paste from my template folder.

Setting current batch file’s path to %batdir% allows you to call it in subsequent stmts in current batch file, regardless of where this batch file changes to. Using PUSHD allows you to use POPD to quickly set this batch file’s path to original %batdir%. Remember, if using %batdir%ExtraDir or %batdir%\ExtraDir (depending on which version used above, ending backslash or not) you will need to enclose the entire string in double quotes if path has spaces (i.e. «%batdir%ExtraDir»). You can always use PUSHD %

dp0. [https: // ss64.com/ nt/ syntax-args .html] has more on (%

Note that using (::) at beginning of a line makes it a comment line. More importantly, using :: allows you to include redirectors, pipes, special chars (i.e. | etc) in that comment.

Of course, Powershell does this and lots more.

What is the current directory in a batch file?

I want to create a few batch files to automate a program.

My question is when I create the batch file, what is the current directory? Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?

9 Answers 9

From within your batch file:

dp0 refers to the full path to the batch file’s directory (static)
%

f0 both refer to the full path to the batch directory and file name (static).

dp0 will always give the full path to the executing batch file. – dbenham Jun 12 ’13 at 11:19

dp0 gives the full path to the directory that the executing batch file is in. %

dpnx0 (which is equivalent to %

f0) gives the full path to the batch file. See robvanderwoude.com/parameters.php for more details. – deadlydog Jul 11 ’13 at 20:08

dp0 is the working directory not the batch files directory, Found this out the hard way. – trampster Jan 29 ’18 at 22:44

Читайте также:  Hp laserjet p3015 driver windows 10 x64

dp0 gives the batch file directory with trailing slash. – icc97 Feb 27 ’18 at 9:10

It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you’r in cmd, and your current directory is c:\dir3 , you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3 .

In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:

So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:

That would open Myprog from the current folder.

The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:

dp0 is more consistent. – icc97 Feb 27 ’18 at 9:14

Say you were opening a file in your current directory. The command would be:

I hope I answered your question.

It is the directory from where you run the command to execute your batch file.

As mentioned in the above answers you can add the below command to your script to verify:

It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3 , then run the batch, the current directory will be c:\dir3 .

Just my 2 cents. The following command fails if called from batch file (Windows 7) placed on pendrive:

But this does the job:

dp0 – Ammar Mohammad Aug 3 ’19 at 12:49

%__CD__% , %CD% , %=C:%

There’s also another dynamic variable %__CD__% which points to the current directory but alike %CD% it has a backslash at the end. This can be useful if you want to append files to the current directory.

With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven’t accessed the drive on the current cmd session.

How to get the path of the batch script in Windows?

I know that %0 contains the full path of the batch script, e.g. c:\path\to\my\file\abc.bat

I would path to be equal to c:\path\to\my\file

How could I achieve that ?

dpf0″ would be more reliable for this case. – eckes Jan 14 ’17 at 17:59

8 Answers 8

To remove the final backslash, you can use the :n,m substring syntax, like so:

I don’t believe there’s a way to combine the %0 syntax with the :

n,m syntax, unfortunately.

Читайте также:  Windows installer cleanup utility from microsoft

0\.. — knew there had to be a better way! Also, you will probably want to enclose %

dp0 in double quotation marks ( «» ) in case there’s spaces in the directory name, etc. – Cameron Sep 30 ’10 at 3:56

dp0 contains the « at the end. Do you have an idea how to remove it ? – Misha Moroshko Sep 30 ’10 at 3:56

0,-1$ in it. Still—very nice answer. – Kyle Strand Sep 21 ’16 at 5:04

dp0 may be a relative path. To convert it to a full path, try something like this:

dp0 directly? – jpaugh Mar 28 ’16 at 19:04

dp0 can be relative, which may or may not be a problem depending on use case – Michael Mrozek Feb 6 ’17 at 19:52

dp0 can’t contain a relative path, d stands for drive and p for path, how a drive could be relative? – jeb Mar 31 ’17 at 15:30

dp0 will be an absolute path even when the script was run as a relative path. Thanks to jeb’s comment, I was not fooled by this answer. Why do people just make up stuff and go and start spreading their wild imagination to others. I have this colleague who does this, but I blamed his (young) age. I wish my down-vote would count. – bitoolean May 25 ’18 at 14:25

Command line .cmd/.bat script, how to get directory of running script

How can you get the directory of the script that was run and use it within the .cmd file?

3 Answers 3

Raymond Chen has a few ideas:

Quoted here in full because MSDN archives tend to be somewhat unreliable:

The easy way is to use the %CD% pseudo-variable. It expands to the current working directory.

set OLDDIR=%CD%
.. do stuff ..
chdir /d %OLDDIR% &rem restore current directory

(Of course, directory save/restore could more easily have been done with pushd / popd , but that’s not the point here.)

The %CD% trick is handy even from the command line. For example, I often find myself in a directory where there’s a file that I want to operate on but. oh, I need to chdir to some other directory in order to perform that operation.

set _=%CD%\curfile.txt
cd . some other directory .
somecommand args %_% args

(I like to use %_% as my scratch environment variable.)

Type SET /? to see the other pseudo-variables provided by the command processor.

Also the comments in the article are well worth scanning for example this one (via the WayBack Machine, since comments are gone from older articles):

This covers the use of %

If you want to know where the batch file lives: %

%0 is the name of the batch file.

dp gives you the drive and path of the specified argument.

Оцените статью