Windows batch file script directory

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.

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?

Читайте также:  Check which ports are being used windows

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

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.

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.)

Читайте также:  Что такое редактор блогов windows live

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.

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.

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

How to test if a file is a directory in a batch script?

Is there any way to find out if a file is a directory?

I have the file name in a variable. In Perl I can do this:

22 Answers 22

You can do it like so:

However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:

si converts %%i to an 8.3 filename. To see all the other tricks you can perform with FOR variables enter HELP FOR at a command prompt.

(Note — the example given above is in the format to work in a batch file. To get it work on the command line, replace the %% with % in both places.)

Читайте также:  Windows route add метрика

1\» echo It’s a directory – MarioVilas Jun 6 ’13 at 14:08

si* ECHO IS FOLDER – Jakob Sternberg Jan 3 ’14 at 4:16

Works with directory names that contains spaces:

Note that the quotes are necessary if the directory contains spaces:

Can also be expressed as:

This is safe to try at home, kids!

1\» echo It’s a directory – MarioVilas Jun 6 ’13 at 14:10

Recently failed with different approaches from the above. Quite sure they worked in the past, maybe related to dfs here. Now using the files attributes and cut first char

f1 . It expands paths like . and .. to real path. – WesternGun Feb 8 at 22:59

Further to my previous offering, I find this also works:

No quotes around %1 are needed because the caller will supply them. This saves one entire keystroke over my answer of a year ago 😉

Here’s a script that uses FOR to build a fully qualified path, and then pushd to test whether the path is a directory. Notice how it works for paths with spaces, as well as network paths.

Sample output with the above saved as «isdir.bat»:

This works perfectly

1 to remove quotes from %1, and add a backslash at end. Then put thw whole into qutes again.

A variation of @batchman61’s approach (checking the Directory attribute).

This time I use an external ‘find’ command.

(Oh, and note the && trick. This is to avoid the long boring IF ERRORLEVEL syntax.)

  • Directories.
  • Directory symbolic links or junctions.
  • Broken directory symbolic links or junctions. (Doesn’t try to resolve links.)
  • Directories which you have no read permission on (e.g. «C:\System Volume Information»)

The NUL technique seems to only work on 8.3 compliant file names.

(In other words, `D:\Documents and Settings` is «bad» and `D:\DOCUME

I think there is some difficulty using the «NUL» tecnique when there are SPACES in the directory name, such as «Documents and Settings.»

I am using Windows XP service pack 2 and launching the cmd prompt from %SystemRoot%\system32\cmd.exe

Here are some examples of what DID NOT work and what DOES WORK for me:

(These are all demonstrations done «live» at an interactive prompt. I figure that you should get things to work there before trying to debug them in a script.)

This DID NOT work:

D:\Documents and Settings>if exist «D:\Documents and Settings\NUL» echo yes

This DID NOT work:

D:\Documents and Settings>if exist D:\Documents and Settings\NUL echo yes

This DOES work (for me):

D:\Documents and Settings>cd ..

D:\>REM get the short 8.3 name for the file

Volume in drive D has no label. Volume Serial Number is 34BE-F9C9

Directory of D:\
09/25/2008 05:09 PM 2008
09/25/2008 05:14 PM 200809

1.25 2008.09.25
09/23/2008 03:44 PM BOOST_

3 boost_repo_working_copy
09/02/2008 02:13 PM 486,128 CHROME

1.EXE ChromeSetup.exe
02/14/2008 12:32 PM cygwin

[[Look right here . ]]
09/25/2008 08:34 AM DOCUME

1 Documents and Settings

09/11/2008 01:57 PM 0 EMPTY_

1.TXT empty_testcopy_file.txt
01/21/2008 06:58 PM NATION

1 National Instruments Downloads
10/12/2007 11:25 AM NVIDIA
05/13/2008 09:42 AM Office10
09/19/2008 11:08 AM PROGRA

1 Program Files
12/02/1999 02:54 PM 24,576 setx.exe
09/15/2008 11:19 AM TEMP
02/14/2008 12:26 PM tmp
01/21/2008 07:05 PM VXIPNP
09/23/2008 12:15 PM WINDOWS
02/21/2008 03:49 PM wx28
02/29/2008 01:47 PM WXWIDG

2 wxWidgets
3 File(s) 510,704 bytes
20 Dir(s) 238,250,901,504 bytes free

D:\>REM now use the \NUL test with the 8.3 name

D:\>if exist d:\docume

This works, but it’s sort of silly, because the dot already implies i am in a directory:

D:\Documents and Settings>if exist .\NUL echo yes

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