Windows batch script path

How to test if a path is a file or directory in Windows batch file?

I searched here, found someone using this

but didn’t work, when %1==c:\this is a file with spaces.csproj , the test still success, which means it will still be treated as a folder.

anyone knows the answer, i guess this is a very common problem and Windows has existed for many many years, it should have a very simple solution.

6 Answers 6

I know the if exist path\nul test for a folder used to work on MS-DOS. I don’t know if it was broken with the introduction of long file names.

I knew that if exist «long path\nul» does not work on Windows batch. I did not realize until today that if exist path\nul works on Vista and beyond as long as path is in the short 8.3 form.

The original code appears to work on Vista. It seems like it should work on XP as well, but I believe the following XP bug is getting in the way: Batch parameter %

The original code does not need the FOR loop, it could simply use %

Here is a variation that fully classifies a path as INVALID, FILE or FOLDER. It works on Vista, but does NOT work on XP because of the %

s1 bug. I’m not sure how it performs on MS-DOS.
EDIT 2015-12-08: There are a number of Windows situations where this fails

I believe this variation will work with nearly all versions of Microsoft batch, including MS-DOS and XP. (it obviously won’t work on early versions of DOS that don’t support PUSHD)

UPDATE 2014-12-26

I’m pretty sure the following will work on all versions of Windows from XP onward, but I have only tested on Win 7.
Edit 2015-12-08: This can fail on network drives because the folder test can falsely report a file as a folder

UPDATE 2015-12-08

Finally — a test that truly should work on any Windows version from XP onward, including with network drives and UNC paths

Note — This technique is intended to be used for a path without any wildcards (a single specific file or folder). If the provided path includes one or more wildcards, then it provides the result for the first file or folder that the file system encounters. Identical directory structures may give different sort order results depending on the underlying file system (FAT32, NTFS, etc.)

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.

Читайте также:  Как можно убрать надпись активация windows

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.

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 %

Читайте также:  Bootmgr не найден windows 10

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

Extract list of path in a file using batch script

Here is my text file:

I need to extract the paths from this file, so I have something like this:

It seems I should use findstr TargetWord TargetFile.txt command. and Also it seems I can use regex like this: findstr /r «^[a-z][a-z]$ ^[a-z][a-z][a-z]$»

But I do not know how to loop through found targets or get the list of output. any help is really appreciated.

4 Answers 4

Based on your comment, you want to use the result to perform an xcopy task, it seems you really want something like this. Note I used example.txt as input file, and DESTINATION where you should add your destination, including the relevant xcopy switches you require:

Alternatively we can use the findstr directly on Folder

You can do like this :

For Windows. you can use powershell.

In my opinion, For /F is all you need for the task. Although using Type may be useful in some situations, there’s no need to use find.exe or findstr.exe for this task as you don’t need to match a particular glob/pattern:

Please note that it may be wise, if there’s a chance that one or more of theses Folder s do not exist, that you prepend using If Exist «%%B\» . Importantly, if each of the lines containing the Folder paths, is space padded up to the end of its line, this solution will not work for you.

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?

Читайте также:  Mount webdav on linux

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.

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