Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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 Jul 6 '10 at 22:16
2  
i need to avoid GNU tools so that the batch file will work anywhere for anyone. My question is for "pure DOS" anyways. –  djangofan Jul 6 '10 at 23:17
    
Alright. A quick google showed a SO result for implementing regex search and replace in VBScript (stackoverflow.com/questions/127318/…) 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 Jul 7 '10 at 9:18
2  
FYI, neither for /f nor TomWij's %~n* are supported in MS-DOS. (Windows' cmd.exe is not DOS, it's a native Windows program.) –  grawity Jul 7 '10 at 12:05

7 Answers 7

up vote 20 down vote accepted

Shortest way I have found:

for %* in (.) do echo %~n*

or within a .bat script:

for %%* in (.) do echo %%~n*

or in .bat with Get value in variable.

for %%* in (.) do set CurrDirName=%%~n*
echo %CurrDirName%

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

share|improve this answer
    
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 Jul 6 '10 at 23:17
3  
The 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 Jul 7 '10 at 0:56
2  
Also, the example does not correctly handle folder names containing a period (.), such as my %USERPROFILE% folder. –  Mike Fitzpatrick Jul 7 '10 at 1:00
    
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 Oct 6 '11 at 22:17
1  
Yes, works if you replace '%' with '%%'. Nice answer. Would have been the accepted answer if that was noted earlier. –  djangofan Mar 15 '12 at 20:52

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
%~dpnx - 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.

share|improve this answer
1  
wow, interestingly enough your right. the command "echo %~dp0" solves my problem also and its more elegant. –  djangofan Aug 10 '10 at 21:32
4  
Ok then -- care to award me a '+1' then? –  Kurt Pfeifle Aug 11 '10 at 11:09
    
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 Jun 26 '12 at 0:28
2  
@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 Jun 26 '12 at 11:50
    
Well, at the time I didn't fully test the answer and it turned out to be problematic. –  djangofan Aug 16 '12 at 15:15

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
share|improve this answer
    
Yep, that actually works. Nicely done. :-) –  djangofan Dec 30 '11 at 17:39

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=%%"
share|improve this answer
    
+1 for demonstrating a neat trick. –  djangofan Apr 27 '12 at 4:35
    
So how is this different from %~dp0 –  Amit Naidu Jun 16 '13 at 11:12

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
share|improve this answer

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

set a=%cd%

Check with

echo %a%
share|improve this answer
2  
He wants the current directory name, not the whole path, and as a==cd, you may as well use %cd%. –  paradroid Apr 24 '12 at 16:06

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.