Loop over folder string and parse out last folder name

后端 未结 13 1468
-上瘾入骨i
-上瘾入骨i 2020-12-03 05:39

I need to grab the folder name of a currently executing batch file. I have been trying to loop over the current directory using the following syntax (which is wrong at prese

13条回答
  •  天命终不由人
    2020-12-03 06:12

    I modified answer given by @Jonathan, since it did not work for me in a batch file, but this below does work, and also supports folders with spaces in it.:

    for %%a in ("%CD%") do set LastFolder=%%~nxa
    echo %LastFolder%
    

    This takes the current directory and echoes the last, deepest folder, as in below example, if the folder is this:

    C:\Users\SuperPDX\OneDrive\Desktop Environment\
    

    The batch code echoes this: Desktop Environment

提交回复
热议问题