I have a batch file which takes as an argument a file path
set filePath = %1
Now, lets say the file path is: C:\Temp\Folder
, I want to set the Folder
in a new variable. How can I do that?
I search on the web and all solutions are something like this:
for %%A in (%filePath%) do set last=%%A
but this works only for string with spaces.
You can extract Folder
from C:\Temp\Folder
by applying the ~n
modifier to %1
:
SET "last=%~n1"
If the last item may contain .
, use ~nx
instead:
SET "last=%~nx1"
The ~n
modifier applies to a positional parameter or a loop variable and extracts the last name from the path specified by that parameter or variable. The ~x
modifier extracts the extension of the last name (the part starting from the last .
). Accordingly, ~nx
extracts both the (last) name and the extension.