How do I get the last part of directory from a command line

453

Solution 1

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%

Solution 2

Give this a try:

for %f in (A\B\C\D) do set var=%~nxf
Share:
453

Related videos on Youtube

user3826141
Author by

user3826141

Updated on September 17, 2022

Comments

  • user3826141
    user3826141 over 1 year

    I'm trying to use Selenium and Java to download .csv files and then re-upload them. The program would visit a google doc with a list of urls. It would then visit each of these urls and click a hyperlink on each site to download a .csv file. It would then go to Google Drive and re-upload the .csv files as google spreadsheets.

    Is this possible to accomplish with Selenium? If not, what is a better solution?

    Thanks

    • Alex
      Alex about 13 years
      You might have more joy with this over on SuperUser (superuser.com)
  • coderlearner
    coderlearner about 13 years
    i try it but the following error appear :- SPATH~nxA was unexpected at this time.
  • Dennis Williamson
    Dennis Williamson about 13 years
    @MohammadAL-Rawabdeh: No. for %%f in (A\B\C\D) do set var=%%~nxf
  • vlad_tepesch
    vlad_tepesch almost 8 years
    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".
  • jscott
    jscott almost 8 years
    @vlad_tepesch My bad, updated answer with correction about variable scoping outside FOR.
  • vlad_tepesch
    vlad_tepesch almost 8 years
    @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.
  • Violet Giraffe
    Violet Giraffe about 3 years
    This doesn't work if the path ends with a slash.