Page 1 of 1
Parse or string minipulating after last "\"
Posted: 08 Feb 2011 10:28
by foremore
lets say create a string and name it a folder path. How do I take whats in that string and parse out just whats after the last "\"?
here is what I am trying to figure out:
Code: Select all
set temp=C:\test local\test last parse\test folder
set temp2=C:\test local2\test another parse\another test folder
:: how do I take whats above and set a variable to equal "test folder"?
:: if I ran the for loop with %temp2% it would come out with a variable named
:: "another test folder"
:: this is just and example but I would like to do it for any location length after
:: and before the "\"
whats tricky is there is no set amount of chars before the last "\" and no set amount of chars after. If it where a set amount of chars either way I would just count over and set the variable. I would think it has to do with a for loop with a delmis:"\" but I just can't pin it down to how to get to the last "\"
Thanks for the help!
Re: Parse or string minipulating after last "\"
Posted: 08 Feb 2011 11:07
by ChickenSoup
Very similar item discussed in this thread:
http://www.dostips.com/forum/viewtopic.php?f=3&t=1619Code: Select all
for %%a in ("%temp2:\=" "%") do @set folder2=%%~a
echo.%folder2%
Re: Parse or string minipulating after last "\"
Posted: 08 Feb 2011 11:48
by aGerman
First: Don't overwrite
temp. It's one of the predefined environment variables. You could use
temp1 instead.
@ChickenSoup
Of course, that's a possible way. On the other hand these are simple windows pathes and you could use the FOR-variable options.
Code: Select all
@echo off &setlocal
set "temp1=C:\test local\test last parse\test folder"
set "temp2=C:\test local2\test another parse\another test folder"
for /f "delims=" %%a in ("%temp1%") do set "foldername1=%%~nxa"
for /f "delims=" %%a in ("%temp2%") do set "foldername2=%%~nxa"
echo %foldername1%
echo %foldername2%
pause
BTW I used
%%~nxa because it's allowed to have a dot in a foldername.
Regards
aGerman
Re: Parse or string minipulating after last "\"
Posted: 08 Feb 2011 12:16
by foremore
awesome! thanks for the help.
I thought I would get it figured out from here but I got loused up on another part. Here is my code:
Code: Select all
SetLocal EnableDelayedExpansion
Set InputFile=locations.txt
For /F "tokens=* Delims=" %%I IN ('Type "%InputFile%"') DO (
for /f "delims=" %%a in ("%%I") do set "folder=%%~nxa"
:: folder is still null after this command. not for sure what I am doing wrong
::robocopy command pulling variables from almost everywhere
ROBOCOPY "%%~I" "%dest%%folder%" %what% %options%
::report errorlevel
echo !ERRORLEVEL!>>"%logging%"
EndLocal
I am not for sure why %%I when passed through the for loop will not set the %folder% variable to a value. I have also tried %%~I like shown in the robocopy command.
thanks for the help!
Re: Parse or string minipulating after last "\"
Posted: 08 Feb 2011 12:27
by aGerman
Code: Select all
ROBOCOPY "%%~I" "%dest%!folder!" %what% %options%
Ordinary variables are expanded only once in a command line or block. Thats why you used
SetLocal EnableDelayedExpansion. But why didn't you enclose
folder in exclamation marks then??
Regards
aGerman
Re: Parse or string minipulating after last "\"
Posted: 08 Feb 2011 12:51
by foremore
thanks. Still learning. But thank you a ton for the help.
Re: Parse or string minipulating after last "\"
Posted: 08 Feb 2011 16:56
by aGerman
Don't worry
Always a good idea to find out whether you're inside a block or not is indenting the lines between the parentheses -- especially if you want to work with nested loops.
Regards
aGerman