|
Answer» I am having a hard time figuring out how to split a string (a path) by another string. I ave SEEN a few posts regarding this, however, people are always splitting using a single character like a '\' for example.
I need to SPECIFICALLY split this: C:\abc\def\ghi\jkl\
by this: def
so I get this: C:\abd\ and this \ghi\jkl\
And I need to be able to address both PIECES. Basically, I need to find that foldername in a string, and it is NEVER in the same place in the path, otherwise I would just split by '\' and count up to that folder... cant do that. What I have now seems to search for each letter individually.
Any ideas? below is where i left off, trying to figure out how to put the foldername "project" after the delims part.
Code: [Select]rem assuming %1 is flder dragged onto .bat file
for /f "tokens=1* delims=<foldername???>" %%a in ("%1") do ( set part1=%%a set part2=%%b )
echo part1 = %part1% echo part2 = %part2%
Thanks for any help you can give! Ken I think you can just do
Code: [Select]mkdir "C:\%part1%\%part2%\%part3%and on and on...The issue is not adding the parts... the issue is how to SPLIT the parts... because from what I can find, the portion after the delims= part ONLY looks for single characters... so if i put foldername as the thing to split by, it will split it into- f if it finds one, then o if it finds one, then l if it finds one.. which is not USEFUL for this... splitstring.bat
@echo off REM YOU NEED THIS setlocal enabledelayedexpansion
REM If the passed parameter string has any spaces you must quote it REM we unquote it using ~ set PassedString=%~1
REM May as well do the same with string to split on set DelimString=%~2
REM The delim string must appear exactly ONCE in the passed string
REM Replace the delim string with a symbol which will not be in the REM path to be split, in this example I use @ set [emailprotected] call set tempstring=!PassedString:%DelimString%=%splitsub%!
REM Use FOR to split the modified string for /f "tokens=1* delims=%splitsub%" %%A in ("%tempstring%") do set part1=%%A & set part2=%%B
REM Show that the 2 variables contain the desired substrings echo Passed = %PassedString% echo Part1 = %part1% echo Part2 = %part2%
Example output:
C:\>splitstring.bat "D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF" "project" Passed = D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF Part1 = D:\Backup\mystuff\programming\Visual Basic\ Part2 = \ABCDE\DEF
C:\>splitstring.bat "D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF" "mystuff" Passed = D:\Backup\mystuff\programming\Visual Basic\project\ABCDE\DEF Part1 = D:\Backup\ Part2 = \programming\Visual Basic\project\ABCDE\DEF
wow, awesome! I think this is exactly what I am looking for. Thanks very much for your explanation of the quotes and the expand option.
Cheers! Ken May want to use the back slashes to make sure it matches on a whole directory name if that is what your original intention was.
Code: [Select]call set tempstring=!PassedString:\%DelimString%\=\%splitsub%\!Two other notes. If you are using the CALL you do not need to use delayed expansion. You can double up your percents and it will still work fine. Code: [Select]call set tempstring=%%PassedString:\%DelimString%\=\%splitsub%\%% If you remove then CALL then you need to use delayed expansion. Code: [Select]set tempstring=!PassedString:\%DelimString%\=\%splitsub%\!Quote from: Squashman on November 22, 2012, 07:27:43 PM If you are using the CALL you do not need to use delayed expansion. You can double up your percents and it will still work fine. I spent about 30 frustrating minutes adding various numbers of percents and then gave up and went with delayed expansion.
|