Loop over folder string and parse out last folder name

后端 未结 13 1404
-上瘾入骨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:22

    You were pretty close to it :) This should work:

    @echo OFF
    set mydir="%~p0"
    SET mydir=%mydir:\=;%
    
    for /F "tokens=* delims=;" %%i IN (%mydir%) DO call :LAST_FOLDER %%i
    goto :EOF
    
    :LAST_FOLDER
    if "%1"=="" (
        @echo %LAST%
        goto :EOF
    )
    
    set LAST=%1
    SHIFT
    
    goto :LAST_FOLDER
    

    For some reason the for command doesn't like '\' as a delimiter, so I converted all '\' to ';' first (SET mydir=%mydir:\=;%)

    0 讨论(0)
提交回复
热议问题