is there any way to know what is the name of upper folder(directory) via cmd in windows? for example you are in C:\A\B\C I need a command which tells B
Asked
Active
Viewed 545 times
0
-
3This link may help you ... [Get parent folder name using batch file.](http://stackoverflow.com/questions/280969/windows-batch-loop-over-folder-string-and-parse-out-last-folder-name) – Opv Dec 23 '16 at 12:00
2 Answers
2
Yes, there is -- use for loops:
set "FOLDER=C:\A\B\C"
for %%J in ("%FOLDER%") do for %%I in ("%%~dpJ.") do echo(%%~nxI
The outer loop is needed to go up one level as %%~dpJ expands to C:\A\B\; let us append a . like %%~dpJ. to get C:\A\B\., which is equivalent to C:\A\B; finally, the inner loop is needed to retrieve the pure name of the referenced directory as %%~nxI returns B.
It is also possible using one for loop:
set "FOLDER=C:\A\B\C"
for %%I in ("%FOLDER%\..") do echo(%%~nxI
The .. means one level up, and C:\A\B\C\.. is therefore equivapent to C:\A\B; finally, %%~nxI returns B again.
aschipfl
- 33,626
- 12
- 54
- 99
2
Alternative, using the built in %CD% variable.
From the command prompt:
For %A In ("%CD%\..\.") Do @Echo(%~nxA
From a batch file:
@For %%A In ("%CD%\..\.") Do @(Echo(%%~nxA&Timeout 5 1>Nul)
Compo
- 36,585
- 5
- 27
- 39