Extract last folder name from a string

March 18, 2011 at 05:01:21
Specs: Windows 7
Hi,

I am having difficulties trying to extract the last folder name from registry strings that I retrieve with the REG command.

The current code I have so far is:

for /F "skip=2 tokens=3 delims=, " %%i in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do (
set Personal=%%i
for /F "delims=\" %%i in ("%Personal%") do (
set Personal=%%~ni
)
)
set Personal


The above code returns the full string that I get from the registry and I cant seem to get it working, would appreciate any help.

Thanks in advance


See More: Extract last folder name from a string

Report •


#1
March 18, 2011 at 09:09:39
You need to enable Delayed Expansion to set and refer to variables inside a For loop (the variable must be marked by ! see SET /?)

setlocal EnableDelayedExpansion
for /F "skip=2 tokens=3 delims=, " %%i in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do (
set Personal=%%i
for /F "delims=\" %%i in ("!Personal!") do (
set Personal=%%~ni
)
)
echo. % Personal%


Report •

#2
March 20, 2011 at 16:18:13
Thank You IVO, that helped :-)

setlocal EnableDelayedExpansion
for /F "skip=2 tokens=3 delims=, " %%i in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do (
set Personal=%%i
for /F "delims= " %%i in ("!Personal!") do (
set Personal=%%~ni
)
)
echo. %Personal%


Report •
Related Solutions


Ask Question