Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Splitting a path using a string in DOS batch.  (Read 24818 times)

0 Members and 1 Guest are viewing this topic.

kproudfoot

    Topic Starter


    Starter

    • Experience: Familiar
    • OS: Windows 7
    Splitting a path using a string in DOS batch.
    « on: November 22, 2012, 10:25:11 AM »
    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

    1998golfer



      Greenhorn

      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 7
    Re: Splitting a path using a string in DOS batch.
    « Reply #1 on: November 22, 2012, 10:27:45 AM »
    I think you can just do

    Code: [Select]
    mkdir "C:\%part1%\%part2%\%part3%and on and on...

    kproudfoot

      Topic Starter


      Starter

      • Experience: Familiar
      • OS: Windows 7
      Re: Splitting a path using a string in DOS batch.
      « Reply #2 on: November 22, 2012, 11:23:14 AM »
      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... 

      Salmon Trout

      • Guest
      Re: Splitting a path using a string in DOS batch.
      « Reply #3 on: November 22, 2012, 12:35:17 PM »
      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 splitsub=@
      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





      kproudfoot

        Topic Starter


        Starter

        • Experience: Familiar
        • OS: Windows 7
        Re: Splitting a path using a string in DOS batch.
        « Reply #4 on: November 22, 2012, 05:19:45 PM »
        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

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Splitting a path using a string in DOS batch.
        « Reply #5 on: November 22, 2012, 07:19:42 PM »
        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%\!

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Splitting a path using a string in DOS batch.
        « Reply #6 on: November 22, 2012, 07:27:43 PM »
        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%\!

        Salmon Trout

        • Guest
        Re: Splitting a path using a string in DOS batch.
        « Reply #7 on: November 23, 2012, 02:17:46 AM »
        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.