86

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.

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
djangofan
  • 2,851
  • 9
  • 34
  • 35
  • 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 06 '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 06 '10 at 23:17
  • 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 Jul 07 '10 at 09:18
  • 3
    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.) – u1686_grawity Jul 07 '10 at 12:05
  • [Are the Command Prompt and MS-DOS the same thing?](http://superuser.com/q/451432/241386), [The Windows command prompt is *NOT* a DOS prompt!](https://scalibq.wordpress.com/2012/05/23/the-windows-command-prompt-is-not-a-dos-prompt/) – phuclv Nov 30 '16 at 16:44

11 Answers11

124

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

PRMan
  • 151
  • 1
  • 5
Tamara Wijsman
  • 57,083
  • 27
  • 185
  • 256
  • 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 06 '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 07 '10 at 00:56
  • 2
    Also, the example does not correctly handle folder names containing a period (`.`), such as my %USERPROFILE% folder. – Mike Fitzpatrick Jul 07 '10 at 01: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 06 '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
  • %~nd is not sufficient if the directory contains dot characters! To be safe it hast to be %~nxd !! – t0r0X Jun 02 '14 at 18:50
  • I think it needs to be %~nx. N for path, X for extension this then works with paths that have a . – Adam Butler Jun 11 '15 at 03:29
  • How to set this variable with `for` not in batch file? – anatoly techtonik Jul 11 '17 at 09:24
  • What do you mean with "not in batch file"? – Tamara Wijsman Jul 11 '17 at 19:05
  • 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 Aziz Apr 05 '18 at 22:41
  • 1
    Whatever 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 Apr 20 '18 at 04:51
  • most excelent trick... forcing the ./%CD to be treated as a SCRIPT/:PROC parameter – ZEE Oct 04 '21 at 17:13
37

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
  • 12,411
  • 2
  • 54
  • 71
18

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
  • 406
  • 5
  • 10
8

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
6

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

set a=%cd%

Check with

echo %a%
slhck
  • 223,558
  • 70
  • 607
  • 592
vera
  • 61
  • 1
  • 1
5

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=%%"
slhck
  • 223,558
  • 70
  • 607
  • 592
lenz
  • 51
  • 1
  • 2
3

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
  • 31
  • 2
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
  • 2,851
  • 9
  • 34
  • 35
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
  • 11
  • 2
0

Almost all of the above code options will lead to an error if you call the bat file from the outside by other script or program. For example, this code: for %%I in (.) do set CurrDirName=%%~nxI, when called from Notepad++, will output Notepad++, but I don't need a parent Notepad++ folder name surely! It is a bad way. In addition, for the same reason, do not use %cd% and any other options. Use only the input parameters of this, maybe remotely located script. Otherwise, you will get the result for the calling, but not called script and what is written in its command line, but not what must be written in the command line of the called code. Use this code for avoid errors:

    set path=%~p0
    set path=%path:~0,-1%
    For %%A in ("%path%") do (Set CurrDirName=%%~nxA)
Garric
  • 121
  • 3
0

set "_cd=%cd:\=" & set "_cd=%"

It is possible to expand in a set _with=auto-expanded variable command using the variable that stores the current folder/path %cd% and get the name of the current folder at the same time saving that value in a variable without using for | for /f loop and/or additional command line:


echo %cd%
C:\Users\ecker\AppData\Local\Temp

:: set _cd with auto expansion from value of %cd% ::
:: set _cd=C:\Users\ecker\AppData\Local\Temp      ::

set "_cd=%cd:\=" & set "_cd=%"
echo %_cd%
Temp

  • To visualize how autoexpansion operates, use:
echo %cd:\=" & set "_cd=%
C:" & set "_cd=Windows" & set "_cd=System32

enter image description here


  • Inspired by references:

Io-oI
  • 7,588
  • 3
  • 12
  • 41