Get full path and long file name from short file name

前端 未结 10 780
故里飘歌
故里飘歌 2020-12-15 09:13

I have an nice console file manager (eXtreme by Senh Liu), it passes short path/filenames as variables to a menu.bat.

How can I generate a full folder name + long fi

10条回答
  •  旧时难觅i
    2020-12-15 09:30

    this is an ugly batch job and my code is not nice, but brut force :-)

    @echo off &SETLOCAL
    SET "short=P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL"
    SET "shorty=%short:\= %"
    
    FOR %%a IN (%short%) DO SET "shortname=%%~nxa"
    FOR %%a IN (%shorty%) DO (
        IF DEFINED flag (
            CALL :doit "%%~a"
        ) ELSE (
            SET "longpath=%%~a"
            SET flag=true
            SET "first=\"
        )
    )
    ECHO "%longpath%"
    goto:eof
    
    :doit
    SET "last=%~1"
    IF "%last%" neq "%shortname%" (SET "isDir=/ad") ELSE SET "isDir=/a-d"
    FOR /f "delims=" %%b IN ('dir %isdir% %longpath%%first%^|findstr /ri "\<%last%\>"') DO SET "X0=%%b"
    FOR /f "delims=" %%b IN ('dir %isdir% /x %longpath%%first%^|findstr /ri "\<%last%\>"') DO SET "X1=%%b"
    REM for European time format
    IF "%X0: =%"=="%X1: =%" (SET /a token=3) ELSE SET /a token=4
    REM for "AM/PM" time format
    IF "%X0: =%"=="%X1: =%" (SET /a token=4) ELSE SET /a token=5
    FOR /f "tokens=%token%*" %%b IN ('dir %isdir% /x %longpath%%first%^|findstr /ri "\<%last%\>"') DO SET "longname=%%~c"
    SET "longpath=%longpath%\%longname%"
    SET "first="
    goto:eof
    

    Please set your time format in the doit function (delete as applicable format).
    This might maybe fail with special characters in path or file names like !%=&^.

提交回复
热议问题