JSI Tip 8755. How can I extract the last sub-folder from a fully qualified fold path?


In tip 0494, I described how to parse a batch parameter.

When you use the %~n syntax, the CMD.EXE processor actually enumerates the string after the last \.

If variable VAR contains a folder path, you can extract the last sub-folder using:

for %%s in ("%VAR%") do set Last=%%~ns

Example

If variable VAR contains C:\Folder 1\Folder 2\Folder 3, then using:

for %%s in ("%VAR%") do set Last=%%~ns
@echo %Last%

in a batch script would echo:

Folder 3



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish