Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
alleypuppy
- Posts: 82
- Joined: 24 Apr 2011 19:20
#1
Post
by alleypuppy » 21 Dec 2011 13:24
Hello,
I want to know if it's possible to get the last string after the specified delimiter in a FOR /F loop.
For example, say I have the directory
I want to extract only "Wallpaper" from the string using a FOR /F loop. Or if I have
Code: Select all
C:\Users\[username]\Desktop\Example Folder\Example Folder 2
I want to extract only "Example Folder 2" from the string.
The only problem is I don't know how to set the TOKENS parameter equal to an ever-changing number. Any ideas?
Thanks for the help!
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 21 Dec 2011 14:01
Try:
Code: Select all
set "myPath=C:\Users\[username]\Desktop\Example Folder\Example Folder 2"
for /f "delims=" %%i in ("%myPath%") do set "myFolder=%%~nxi"
echo "%myFolder%"
Regards
aGerman
-
!k
- Expert
- Posts: 378
- Joined: 17 Oct 2009 08:30
- Location: Russia
#3
Post
by !k » 21 Dec 2011 14:07
or
Code: Select all
call :name "C:\WINDOWS\Web\Wallpaper"
call :name "C:\Users\[username]\Desktop\Example Folder\Example Folder 2"
goto :eof
:name
echo name = %~nx1
goto :eof
-
orange_batch
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
-
Contact:
#4
Post
by orange_batch » 21 Dec 2011 23:56
The above works for backslash as a delimiter, but the following works for whatever else:
Code: Select all
call :getlasttoken "what#is#the#last#token"
echo %lasttoken%
pause
exit
:getlasttoken
set "check=%~1"
:loop
if defined check (
for /f "delims=# tokens=1*" %%x in ("%check%") do (
set "lasttoken=%%x"
set "check=%%y"
)
goto loop
) else exit/b