Get current folder name by a DOS command?

68

26

Is it possible to get the current folder name (not current directory path) by using a DOS command? If so, how?

The closest I got was this but it doesn't do it:

for /f "delims=\" %%a in ("%CD%") do set CURR=%%a
echo.DIR: %CURR%

note: the above attempt was me attempting to tokenize the string and get the last token set as the CURR variable.

djangofan

Posted 2010-07-06T21:29:58.987

Reputation: 2 459

If you have any sort of GNU toolset installed, you should be able to go cd | sed "s/.*\\//" (That pipes the output of cd (cwd) into a regular expression search and replace, replacing everything before the final \ with nothing at all) – Phoshi – 2010-07-06T22:16:28.627

2i need to avoid GNU tools so that the batch file will work anywhere for anyone. My question is for "pure DOS" anyways. – djangofan – 2010-07-06T23:17:51.053

Alright. A quick google showed a SO result for implementing regex search and replace in VBScript (http://stackoverflow.com/questions/127318/is-there-any-sed-like-utility-for-cmd-exe) which could use the same syntax and create the same result - I believe VBScript has been built in since windows 98, so should be quite anywhere for everyone! (You could also very easily rejigger it to work on *nix OS', too)

– Phoshi – 2010-07-07T09:18:54.683

3FYI, neither for /f nor TomWij's %~n* are supported in MS-DOS. (Windows' cmd.exe is not DOS, it's a native Windows program.) – user1686 – 2010-07-07T12:05:12.863

Answers

95

Shortest way I have found:

for %I in (.) do echo %~nxI

or within a .bat script:

for %%I in (.) do echo %%~nxI

or in .bat with Get value in variable.

for %%I in (.) do set CurrDirName=%%~nxI
echo %CurrDirName%

Explanation: http://www.robvanderwoude.com/ntfor.php

nx means file name and extension only

Tamara Wijsman

Posted 2010-07-06T21:29:58.987

Reputation: 54 163

I think it needs to be %~nx. N for path, X for extension this then works with paths that have a . – Adam Butler – 2015-06-11T03:29:25.527

I like the link and the suggestion but when i put that in a batch file I get an immediate closing of the shell with no output. there must be a syntax error in there somewhere? – djangofan – 2010-07-06T23:17:11.570

3The example will work on the command line interactively. To use it in a batch file you need to replace all occurrences of % with %%. – Mike Fitzpatrick – 2010-07-07T00:56:20.807

2Also, the example does not correctly handle folder names containing a period (.), such as my %USERPROFILE% folder. – Mike Fitzpatrick – 2010-07-07T01:00:23.803

How to set this variable with for not in batch file? – anatoly techtonik – 2017-07-11T09:24:14.667

What do you mean with "not in batch file"? – Tamara Wijsman – 2017-07-11T19:05:18.707

Could you explain how exactly this works? Namely what %* and %~nx* are doing to get the directory name. I can't find any explanation for them on RvdW's site, that page seems like it contains more or less the same information than for /?. – Hashim – 2018-04-05T22:41:14.163

1Whatever this god-awful evil sorcery is, I'm glad it works. I'm familiar with most of the expansion-syntax, but I've never seen * used with it before o.o – kayleeFrye_onDeck – 2018-04-20T04:51:06.553

Note that if you output this to a file you have to be careful not to add an extra space.

Example: for %* in (.) do @echo %~n*> TmpFile – Will Bickford – 2011-10-06T22:17:39.210

1Yes, works if you replace '%' with '%%'. Nice answer. Would have been the accepted answer if that was noted earlier. – djangofan – 2012-03-15T20:52:42.590

%~nd is not sufficient if the directory contains dot characters! To be safe it hast to be %~nxd !! – t0r0X – 2014-06-02T18:50:31.610

31

If you want to know the current location of the batch file (and if your Windows isn't a very ancient release), type for /? in a 'DOS box' window. Scroll down. Read.

You'll find out, that you can now read (from within the batch file) these variables:

%0      - as the name how this batchfile was called
%~d0    - as the drive letter where this batchfile is located ('\\' in case of share)
%~p0    - as path (without the drive letter) where this batchfile is located
%~n0    - as filename (without suffix) of this batchfile
%~x0    - as filename's suffix (without filename) of this batchfile
%~a0    - as this batchfile's file attributes
%~t0    - as this batchfile's date+time
%~z0    - as this batchfile's filesize
%~dpnx0 - as this batchfile's fully qualified path+filename
[... and then some more ...]

This works for many cases. Assume, the batchfile is called mytest.bat. You may call it in different ways:

  1. ..\..\to\mytest.bat ............................... (relative path)
  2. d:\path\to\mytest.bat ........................... (full path)
  3. \\fileserver\sharename\mytest.bat ... (path on remote share)

...and you'll always get the right value in your variables.

Kurt Pfeifle

Posted 2010-07-06T21:29:58.987

Reputation: 10 024

Is this actually the current directory, or the path of the batch file? – erict – 2014-12-06T19:46:39.227

@erict: path of batch file. – Kurt Pfeifle – 2014-12-24T22:39:14.287

1it seems that the last one should be %~dpnx0, isn't? – luc – 2016-11-04T08:49:01.533

@luc: You are right, thanks for the hint. I'll add a correction. – Kurt Pfeifle – 2016-11-04T10:02:02.710

1wow, interestingly enough your right. the command "echo %~dp0" solves my problem also and its more elegant. – djangofan – 2010-08-10T21:32:35.730

5Ok then -- care to award me a '+1' then? – Kurt Pfeifle – 2010-08-11T11:09:00.587

No, for the reason that 'for /?' was the obvious first place I looked and so this answer is no better than what is expected by anyone. – djangofan – 2012-06-26T00:28:11.017

4@djangofan: So you say 'Wow, interestingly enough you are right...' to something you did know already?! To an answer that didn't contribute to your knowledge? (You know, '+1' is clicking on the ^-arrow -- it's different from making an answer the accepted one....) – Kurt Pfeifle – 2012-06-26T11:50:01.237

Well, at the time I didn't fully test the answer and it turned out to be problematic. – djangofan – 2012-08-16T15:15:48.093

16

I personally liked Toms answer, until it struggled with dots in dir names. This gave me a hint:

for /f "delims=\" %%a in ("%cd%") do echo topmost dir: %%~nxa

Marc Wittke

Posted 2010-07-06T21:29:58.987

Reputation: 356

5

Tom's answer is good, but if you have a directory name with a period in it (i.e. wxwidgets-2.9.4) you'll only get the full name. So this would output wxwidgets-2.9 instead because the .4 has been treated as an extension (Yes, even though it's a directory name!).

To get the full output name you have to add on the extension to the end:

FOR %I IN (.) DO Echo %~nI%~xI

and in batch file mode:

FOR %%I IN (.) DO Echo %%~nI%%~xI

Or of course, set a variable in the batch file instead:

FOR %%I IN (.) DO SET CurrentD=%%~nI%%~xI

Brian Sidebotham

Posted 2010-07-06T21:29:58.987

Reputation: 151

4

An other way is:

set "MyPath=%~dpnx0" & call set "MyPath=%%MyPath:\%~nx0=%%" 
echo MyPath=%MyPath%  

it works with "." and spaces in pathname

What does it do?

  1. put the whole filename (driveletter-path-filename-extension) into MyPath Var

  2. remove filename and extension from MyPath var

It also works with UNC Paths. If you need the Backslash on the end of the Path. Remove the \ after MyPath in the second set command, eg.

set "MyPath=%%MyPath:%~nx0=%%"

lenz

Posted 2010-07-06T21:29:58.987

Reputation: 41

1So how is this different from %~dp0 – Amit Naidu – 2013-06-16T11:12:53.067

4

You can get the current dir into a variable. One-liner:

set a=%cd%

Check with

echo %a%

vera

Posted 2010-07-06T21:29:58.987

Reputation: 41

6He wants the current directory name, not the whole path, and as a==cd, you may as well use %cd%. – paradroid – 2012-04-24T16:06:52.930

2

just simple

for %%d in ("%CD%") do echo %%~nxd

or

set "sPath=."
for %%d in ("%sPath%") do set "sDirName=%%~nxd"

Be careful of the backslash of the end of path, it has not to be backslash of the end.

kygg

Posted 2010-07-06T21:29:58.987

Reputation: 21

1

My answer in this thread does it in 3 simple lines:

@echo off
SET "CDIR=%~dp0"
:: for loop requires removing trailing backslash from %~dp0 output
SET "CDIR=%CDIR:~0,-1%"
FOR %%i IN ("%CDIR%") DO SET "PARENTFOLDERNAME=%%~nxi"
ECHO Parent folder: %PARENTFOLDERNAME%
ECHO Full path: %~dp0
pause>nul

djangofan

Posted 2010-07-06T21:29:58.987

Reputation: 2 459

1

This works for me from a batch file. It returns the current working directory name.

pushd %1 & for %%i in (.) do @echo %%~ni

nem50

Posted 2010-07-06T21:29:58.987

Reputation: 11