Parse or string minipulating after last "\"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
foremore
Posts: 3
Joined: 08 Feb 2011 10:18

Parse or string minipulating after last "\"

#1 Post by foremore » 08 Feb 2011 10:28

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!

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Parse or string minipulating after last "\"

#2 Post by ChickenSoup » 08 Feb 2011 11:07

Very similar item discussed in this thread: http://www.dostips.com/forum/viewtopic.php?f=3&t=1619

Code: Select all

for %%a in ("%temp2:\=" "%") do @set folder2=%%~a
echo.%folder2%

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Parse or string minipulating after last "\"

#3 Post by aGerman » 08 Feb 2011 11:48

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

foremore
Posts: 3
Joined: 08 Feb 2011 10:18

Re: Parse or string minipulating after last "\"

#4 Post by foremore » 08 Feb 2011 12:16

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Parse or string minipulating after last "\"

#5 Post by aGerman » 08 Feb 2011 12:27

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

foremore
Posts: 3
Joined: 08 Feb 2011 10:18

Re: Parse or string minipulating after last "\"

#6 Post by foremore » 08 Feb 2011 12:51

:oops:

thanks. Still learning. But thank you a ton for the help.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Parse or string minipulating after last "\"

#7 Post by aGerman » 08 Feb 2011 16:56

Don't worry :wink:
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

Post Reply