@echo off&set FileName=%~n0&shift :: ============================================= :: AutoHelp PlugLab Concept: 2006 by Reatogo :: ============================================= :: Help Info: :: ======================= set Routine_Type=1 :: 1=Getting Data 2=Prompts 3=Getting Files 4=Creating Files 5=General set Routine_Vers=1 set Routine_Date=2007-02-15 set RoutineName=SplitPath set RoutineAuthor=Siegfried set Using_work_from= set Example_call_1=%%SplitPath%% "%%ProgramFiles%%\Symantec\Norton Ghost\New Ghost Tray.exe" "show" set Example_call_2=%%SplitPath%% "%%AppData%%" "show" set Example_call_3=%%SplitPath%% set text_01=Splits up a path into its elements and returns them as various variables. set text_02=. set text_03=For details see the example below or just run example 1 for a test. set text_04=. set text_05=If parameter 2 is empty the results are not displayed. set text_06=. set text_07=The input path may be given with or without a file or with/without trailing backslash. All result paths will be without trailing backslash. if "%~1" == "GetHelp" if "%SplitPathExampleDone%" == "" ( %SplitPath% "%ProgramFiles%\Symantec\Norton Ghost\New Ghost Tray.exe" "s" set SplitPathExampleDone=yes ) set text_11=. set text_12=*** Example 1 returns the following results: *** set text_13=. set text_14= %%PathFull%% is "%ProgramFiles%\Symantec\Norton Ghost" set text_15= %%PathFull2%% is "%ProgramFiles%\Symantec" set text_16= %%PathFull3%% is "%ProgramFiles%" set text_17=. set text_18= %%PathCore%% is "%LastDir3%\Symantec\Norton Ghost" set text_19= %%PathCore2%% is "%LastDir3%\Symantec" set text_20= %%PathCore3%% is "%LastDir3%" set text_21=. set text_23= %%LastDir%% is "Norton Ghost" set text_24= %%LastDir2%% is "Symantec" set text_25= %%LastDir3%% is "%LastDir3%" set text_26=. set text_27= %%FirstDir%% is "%FirstDir%" set text_28= %%FirstDir2%% is "Symantec" set text_29= %%FirstDir3%% is "Norton Ghost" set text_30=. set text_31= %%File%% is "New Ghost Tray.exe" set text_32= %%FileName%% is "New Ghost Tray" set text_33= %%Extension%% is ".exe" set text_34= %%Drive%% is "%Drive%" set text_35=. set text_36= %%LastDirNB%% is "NortonGhost" set text_37= %%FileNB%% is "NewGhostTray.exe" set text_38= %%FileNameNB%% is "NewGhostTray" set text_39=. set text_40= NOTE: the quotation marks are NOT part of the results. if "%~1" == "GetHelp" goto :EOF if not "%~2" == "s" echo.&echo %* :: ============================================= if "%~1" == "" (call :SplitPathMain "%cd%" "%~2") else (call :SplitPathMain "%~1" "%~2") goto :EOF :SplitPathMain if exist "%temp%\TempLab.cmd" del "%temp%\TempLab.cmd" call :Vars_Reset set AllParams="%~1" "%~2" set Full=%~1 if "%~x1" == "" if "%Full:~-1%" == "\" set Param_1_adj=%Full% if "%~x1" == "" if not "%Full:~-1%" == "\" set Param_1_adj=%Full%\ if not "%~x1" == "" set Param_1_adj=%~dp1 set PathFull=%Param_1_adj:~0,-1% set Drive=%~d1 :: Only if the path contains a file: if "%~x1" == "" goto NoFileInPath set File=%~nx1 set Extension=%~x1 set FileName=%~n1 set FileNameNB=%FileName: =% set FileNameNB=%FileNameNB:.=% set FileNB=%FileNameNB%%Extension% :NoFileInPath call :SplitPath_Sub "%Param_1_adj%" if exist "%temp%\TempLab.cmd" ( call "%temp%\TempLab.cmd" del "%temp%\TempLab.cmd" ) set N_folders=%N_folders:~0,-1% set FirstDir=%FirstDir1% set LastDir=%LastDir1% SETLOCAL ENABLEDELAYEDEXPANSION echo set PathCore=!PathFull:%Drive%\=!>> "%temp%\TempLab.cmd" if not "%LastDir2%" == "" ( set PathFull2=!PathFull:\%LastDir%=! echo set PathFull2=!PathFull2!>> "%temp%\TempLab.cmd" echo set PathCore2=!PathFull2:%Drive%\=!>> "%temp%\TempLab.cmd" ) if not "%LastDir3%" == "" ( set PathFull3=!PathFull:\%LastDir2%\%LastDir%=! echo set PathFull3=!PathFull3!>> "%temp%\TempLab.cmd" echo set PathCore3=!PathFull3:%Drive%\=!>> "%temp%\TempLab.cmd" ) if not "%LastDir4%" == "" ( set PathFull4=!PathFull:\%LastDir3%\%LastDir2%\%LastDir%=! echo set PathFull4=!PathFull4!>> "%temp%\TempLab.cmd" echo set PathCore4=!PathFull4:%Drive%\=!>> "%temp%\TempLab.cmd" ) if not "%LastDir5%" == "" ( set PathFull5=!PathFull:\%LastDir4%\%LastDir3%\%LastDir2%\%LastDir%=! echo set PathFull5=!PathFull5!>> "%temp%\TempLab.cmd" echo set PathCore5=!PathFull5:%Drive%\=!>> "%temp%\TempLab.cmd" ) if not "%LastDir6%" == "" ( set PathFull6=!PathFull:\%LastDir5%\%LastDir4%\%LastDir3%\%LastDir2%\%LastDir%=! echo set PathFull6=!PathFull6!>> "%temp%\TempLab.cmd" echo set PathCore6=!PathFull6:%Drive%\=!>> "%temp%\TempLab.cmd" ) ENDLOCAL if exist "%temp%\TempLab.cmd" ( call "%temp%\TempLab.cmd" del "%temp%\TempLab.cmd" ) if "%~2" == "show" call :Show_Results if "%~2" == "print" ( call :Show_Results call :Print_Results ) goto :EOF :: ======================== :SplitPath_Sub :: ======================== set TempSplitPath=%~dp1 if "%TempSplitPath:~-1%" == "\" set TempSplitPath=%TempSplitPath:~0,-1% SETLOCAL ENABLEDELAYEDEXPANSION set cnB=1 :MainLoop set cn=1 set cnF=0 :Foldertokloop FOR /F "tokens=%cn%* delims=\" %%a IN ("%TempSplitPath%") DO ( if "%%a"=="" goto FolderCont set /a cn +=1 set /a cnF +=1 goto Foldertokloop ) :FolderCont set /a cn-=%cnB% set /a cnF-=%cnB% FOR /F "tokens=%cn% delims=\" %%a IN ("%TempSplitPath%") DO ( if "%%a" == "" goto _end set LastDir%cnB%=%%a set FirstDir%cnF%=%%a echo set LastDir%cnB%=%%a>> "%temp%\TempLab.cmd" echo set FirstDir%cnF%=%%a>> "%temp%\TempLab.cmd" if "%cnB%" == "1" ( set LastDirNB=!LastDir1: =! set LastDirNB=!LastDirNB:.=! echo set LastDirNB=!LastDirNB!>> "%temp%\TempLab.cmd" ) echo set N_folders=%cnB%x>> "%temp%\TempLab.cmd" set /a cnB +=1 if "%cn%" == "2" goto _end goto MainLoop ) ENDLOCAL :_end goto :EOF :: ======================== :Vars_Reset :: ======================== set AllParams= set File= set FileNB= set FileName= set Extension= set Drive= set PathFull= set PathCore= set Param_1_adj= set Full= set LastDirNB= set FileNameNB= set TempSplitPath= set LastDir= set FirstDir= for /l %%i in (1,1,10) do set PathFull%%i= for /l %%i in (1,1,10) do set PathCore%%i= for /l %%i in (1,1,10) do set LastDir%%i= for /l %%i in (1,1,10) do set FirstDir%%i= goto :EOF :: ======================== :Show_Results :: ======================== cls echo **************************************************************************** echo %%SplitPath%% %AllParams% echo **************************************************************************** echo. echo %%PathFull%% %PathFull% if not "%PathFull2%" == "" echo %%PathFull2%% %PathFull2% if not "%PathFull3%" == "" echo %%PathFull3%% %PathFull3% if not "%PathFull4%" == "" echo %%PathFull4%% %PathFull4% if not "%PathFull5%" == "" echo %%PathFull5%% %PathFull5% if not "%PathFull6%" == "" echo %%PathFull6%% %PathFull6% echo. echo %%PathCore%% %PathCore% if not "%PathCore2%" == "" echo %%PathCore2%% %PathCore2% if not "%PathCore3%" == "" echo %%PathCore3%% %PathCore3% if not "%PathCore4%" == "" echo %%PathCore4%% %PathCore4% if not "%PathCore5%" == "" echo %%PathCore5%% %PathCore5% if not "%PathCore6%" == "" echo %%PathCore6%% %PathCore6% echo. echo %%N_Folders%% %N_Folders% echo. SETLOCAL ENABLEDELAYEDEXPANSION echo %%LastDir%% %LastDir% for /l %%i in (2,1,10) do if not "!LastDir%%i!" == "" echo %%LastDir%%i%% !LastDir%%i! echo. echo %%FirstDir%% %FirstDir% for /l %%i in (2,1,10) do if not "!FirstDir%%i!" == "" echo %%FirstDir%%i%% !FirstDir%%i! ENDLOCAL echo. if not "%Extension%" == "" ( echo %%File%% %File% echo %%FileName%% %FileName% echo %%Extension%% %Extension% ) echo %%Drive%% %Drive% echo. echo %%LastDirNB%% %LastDirNB% if not "%Extension%" == "" ( echo %%FileNB%% %FileNB% echo %%FileNameNB%% %FileNameNB% ) echo. goto :EOF :: ============================================= :Print_Results :: ======================== cls echo ****************************************************************************>> SplitExamples.txt echo %%SplitPath%% %AllParams%>> SplitExamples.txt echo ****************************************************************************>> SplitExamples.txt echo.>> SplitExamples.txt echo %%PathFull%% %PathFull%>> SplitExamples.txt if not "%PathFull2%" == "" echo %%PathFull2%% %PathFull2%>> SplitExamples.txt if not "%PathFull3%" == "" echo %%PathFull3%% %PathFull3%>> SplitExamples.txt if not "%PathFull4%" == "" echo %%PathFull4%% %PathFull4%>> SplitExamples.txt if not "%PathFull5%" == "" echo %%PathFull5%% %PathFull5%>> SplitExamples.txt if not "%PathFull6%" == "" echo %%PathFull6%% %PathFull6%>> SplitExamples.txt echo.>> SplitExamples.txt echo %%PathCore%% %PathCore%>> SplitExamples.txt if not "%PathCore2%" == "" echo %%PathCore2%% %PathCore2%>> SplitExamples.txt if not "%PathCore3%" == "" echo %%PathCore3%% %PathCore3%>> SplitExamples.txt if not "%PathCore4%" == "" echo %%PathCore4%% %PathCore4%>> SplitExamples.txt if not "%PathCore5%" == "" echo %%PathCore5%% %PathCore5%>> SplitExamples.txt if not "%PathCore6%" == "" echo %%PathCore6%% %PathCore6%>> SplitExamples.txt echo.>> SplitExamples.txt echo %%N_Folders%% %N_Folders%>> SplitExamples.txt echo.>> SplitExamples.txt SETLOCAL ENABLEDELAYEDEXPANSION echo %%LastDir%% %LastDir%>> SplitExamples.txt for /l %%i in (2,1,10) do if not "!LastDir%%i!" == "" echo %%LastDir%%i%% !LastDir%%i!>> SplitExamples.txt echo.>> SplitExamples.txt echo %%FirstDir%% %FirstDir%>> SplitExamples.txt for /l %%i in (2,1,10) do if not "!FirstDir%%i!" == "" echo %%FirstDir%%i%% !FirstDir%%i!>> SplitExamples.txt ENDLOCAL echo.>> SplitExamples.txt if not "%Extension%" == "" ( echo %%File%% %File%>> SplitExamples.txt echo %%FileName%% %FileName%>> SplitExamples.txt echo %%Extension%% %Extension%>> SplitExamples.txt ) echo %%Drive%% %Drive%>> SplitExamples.txt echo.>> SplitExamples.txt echo %%LastDirNB%% %LastDirNB%>> SplitExamples.txt if not "%Extension%" == "" ( echo %%FileNB%% %FileNB%>> SplitExamples.txt echo %%FileNameNB%% %FileNameNB%>> SplitExamples.txt ) echo.>> SplitExamples.txt goto :EOF :: =============================================