The following code is creating shortcuts to Paths listed in List.txt textfile (like C:/folder1/folder2/folder3/folder4), and saves all shortcuts to path specified in set SAVETO.
How to extract each Folder3 (second last folder name of each row in List.txt, to use it then in %SAVETO% paths)?
<...>
SET "SAVETO=%userprofile%\desktop"
for /f "usebackq delims=" %%G IN ("List.txt") DO (
call :createLink "%%~nG" "%%G"
)
goto :eof
:createLink
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%SAVETO%\%~1.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "%~2" >> %SCRIPT%
<...>
After receiving second last folder, the shortcut must be saved to:
D:/custompath/%Folder3-second last from row in List.txt%/
UPDATE: For example, this code is almost what I need, it extracts folder3 name, but I can't apply this to my code. And also it's not necessary for me to check equ "\"
setlocal EnableDelayedExpansion
set "var=C:\folder 1\folder 2\folder 3\folder 4\"
if "%var:~-1%" equ "\" set var=%var:~0,-1%
set var=%var:\=" "%
for %%a in ("%var%") do (
set lastButOne=!lastFolder!
set lastFolder=%%~a
)
echo Last but one: %lastButOne%
And when code returning second last folder name of current line from List.txt I want to use its name in save path C:/path/*Second Last Folder Name*/Shortcut. And I need to do this for each line separately. So if first line of List.txt is path C:/a/bbb/c then shortcut to this path must be saved to c:/custom_path/bbb/shortcut_name, for C:/ab/cd/efff/g it must be saved to c:/custom_path/efff/shortcut_name.