6

How do I store the last part of directory in a variable?

For example I have the following path: A\B\C\D, I want to store D in variable like file_name=D.

1
  • You might have more joy with this over on SuperUser (superuser.com) Commented Mar 13, 2011 at 14:38

2 Answers 2

8

Because of your Windows tag, I assume your cmd.exe has extensions built-in. If that is the case, you can use two of FOR's special substitution variable references:

Given a variable %A, containing a path and file:

%~nA will output the file name, %~xA will output the file extension. The following example uses the pipe character | as a delimiter. The pipe is an invalid character for files and paths and should not appear in a path. This will allow for spaces in paths and filenames. See FOR /? for full details.

C:\> SET FSPATH=C:\WINDOWS\Temp\file.txt C:\> echo %FSPATH% C:\WINDOWS\Temp\file.txt C:\> FOR /F "delims=|" %A IN ("%FSPATH%") do echo %~nxA file.txt 

Alternatively, should you not have extensions in your cmd.exe, you can use delims=\, count the directory separators and split your path/file string based on that number.

Edit: Per your comment about the error. Above is an example on the command line. If you want to perform the same within a batch script, you need to double the % on the the variables:

FOR /F "delims=|" %%A IN ("%FSPATH%") do echo %%~nxA 

To use the value outside of the FOR loop, you would need to assign the value to another variable. The variable %%A is limited to the scope of FOR.

:: example.bat SET FSPATH=C:\Windows\bfsvc.exe FOR /F "delims=|" %%A IN ("%FSPATH%") DO ( echo Inside loop %%~nxA SET SOMEFILE=%%~nxA ) ECHO Outside loop %SOMEFILE% 
5
  • i try it but the following error appear :- SPATH~nxA was unexpected at this time. Commented Mar 13, 2011 at 15:05
  • this does not work for me. the FOR line echos the correct part of the path but after this the %%~nxA does not get expanded and is passed just as the string "%%~nxA". Commented Jul 20, 2016 at 12:23
  • @vlad_tepesch My bad, updated answer with correction about variable scoping outside FOR. Commented Jul 20, 2016 at 12:34
  • @jscott thanks - i just got it with the help of the other answer. It is possible to keep this a one liner by omitting the echo and directly do the set. Commented Jul 20, 2016 at 12:43
  • This doesn't work if the path ends with a slash. Commented Apr 14, 2021 at 13:16
2

Give this a try:

for %f in (A\B\C\D) do set var=%~nxf 
4
  • this error appear :- ~nxf was unexpected at this time. C:\Program Files\GNU\>for ~nxf Commented Mar 13, 2011 at 15:07
  • @MohammadAL-Rawabdeh: You said "from a command line". If you're doing it in a batch file, you'll need to use double percent signs. Commented Mar 13, 2011 at 15:29
  • sorry ... i want execute it from batch file :- it will be %~nxf% Commented Mar 13, 2011 at 15:46
  • @MohammadAL-Rawabdeh: No. for %%f in (A\B\C\D) do set var=%%~nxf Commented Mar 13, 2011 at 15:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.