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

Author Topic: How to split any line(path) throught command line (for batch file)?  (Read 44616 times)

0 Members and 1 Guest are viewing this topic.

roohi

    Topic Starter


    Beginner

    I want to split a line (its actually a path), with the help of specific string of that line.

    For example I get Path of current work dir by the command cd let it return following path :
    c:/dir/xyz/abc/test1

    now i only want c:/dir from this path how can i get this path ?
    here one more thing is there which i want to asked in above path c:/dir may change for each user , Test 1 change for diff test but /xyz/abc are constant or fixed for each time .

    so any one tell me how i get that dir path ?

    i want the solution for batch files of MSDOS.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: How to split any line(path) throught command line (for batch file)?
    « Reply #1 on: September 11, 2008, 05:03:58 AM »
    This should work:

    Code: [Select]
    for /f "tokens=1-2 delims=/ " %%i in ('cd') do (
    echo %%i/%%j
    )

    Quote
    i want the solution for batch files of MSDOS.

    Are you really running MS-DOS or the shell program running under Windows?
     8)
    « Last Edit: September 11, 2008, 05:14:31 AM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    roohi

      Topic Starter


      Beginner

      Re: How to split any line(path) throught command line (for batch file)?
      « Reply #2 on: September 11, 2008, 05:15:16 AM »
      it does not works

      it shows full path like

      c:\dir\xyz\test1/

      and i want only c:\dir

      roohi

        Topic Starter


        Beginner

        Re: How to split any line(path) throught command line (for batch file)?
        « Reply #3 on: September 11, 2008, 05:25:55 AM »
        now i get that thanks alot

        now if i want to break from abc then how can i get first part only .

        means like c:\dir\xyz\abc\test1

        now if delims=abc then how can i get first part because i m not sure that each user have 2 or 3 token or one token before that .

        by the way thanks for help

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: How to split any line(path) throught command line (for batch file)?
        « Reply #4 on: September 11, 2008, 05:27:24 AM »
        You need to be consistent:

        c:/dir/xyz/abc/test1

        c:\dir\xyz\test1/

        OR write more generic code:

        Code: [Select]
        for /f "tokens=1-2 delims=\/" %%i in ('cd') do (
        echo %%i/%%j
        )
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        roohi

          Topic Starter


          Beginner

          Re: How to split any line(path) throught command line (for batch file)?
          « Reply #5 on: September 11, 2008, 05:29:37 AM »
          thanks alot for help  :)

          now my problem is sloved .

          roohi

            Topic Starter


            Beginner

            Re: How to split any line(path) throught command line (for batch file)?
            « Reply #6 on: September 11, 2008, 07:36:34 AM »
            Hi Sidewinder

            I want to ask one more thing in delims can i specify only one character ?

            because i tried to give test so it matches t , e , s  speratally and deletes all .

            I mean if I want to match whole word test then?

            Dias de verano

            • Guest
            Re: How to split any line(path) throught command line (for batch file)?
            « Reply #7 on: September 11, 2008, 02:17:09 PM »
            This should work:

            Code: [Select]
            for /f "tokens=1-2 delims=/ " %%i in ('cd') do (
            echo %%i/%%j
            )


            Quote
            i want the solution for batch files of MSDOS.

            Are you really running MS-DOS or the shell program running under Windows?
             8)


            Let's have a little OS quiz -

            1. Which OS family uses a forward slash ("/") as a path delimiter? Is it:

            (a) Unix/Linux
            (b) Microsoft MS-DOS/Windows?

            2. Which OS family uses a backward slash ("\") as a path delimiter? Is it:

            (a) Unix/Linux
            (b) Microsoft MS-DOS/Windows?

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: How to split any line(path) throught command line (for batch file)?
            « Reply #8 on: September 11, 2008, 03:56:25 PM »
            Hi Sidewinder

            I want to ask one more thing in delims can i specify only one character ?

            because i tried to give test so it matches t , e , s  speratally and deletes all .

            I mean if I want to match whole word test then?


            You can have a multiple delims. Each character acts as a delimiter.

            In your case I would recommend dumping the %CD% value in a text file and search for the value test:

            Sample code:
            Code: [Select]
            @echo off
            echo %cd% > test.txt
            find /i "test" test.txt > nul
            if errorlevel 1 echo not found && goto :eof
            if errorlevel 0 echo found && goto :eof

             8)

            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            roohi

              Topic Starter


              Beginner

              Re: How to split any line(path) throught command line (for batch file)?
              « Reply #9 on: September 11, 2008, 11:37:31 PM »
              I used the following code :

              for /f "tokens=1-10 delims=\" %%a in ('cd') do (
                 if %%b==test echo %%a
                 if %%c==test echo %%a/%%b
                 if %%d==test echo %%a/%%b/%%c
                 if %%e==test echo %%a/%%b/%%c/%%d
                 if %%f==test echo %%a/%%b/%%c/%%d/%%e
                 )

              i know its not a good option but for a time begin i used that .


              roohi

                Topic Starter


                Beginner

                Re: How to split any line(path) throught command line (for batch file)?
                « Reply #10 on: September 12, 2008, 05:49:46 AM »
                hello

                finally i used following code can some one plz tell me how i can append the value of g in p variable.

                for /f "tokens=1-10 delims=\" %%1 in ('cd') do (
                      set p=%%1
                   for %%G in (%%1, %%2, %%3, %%4, %%5, %%6, %%7, %%8, %%9, %%10) do (
                      if %%G==test echo %p%
                      set p=.................
                      echo %%G     
                   )
                   
                )



                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                Re: How to split any line(path) throught command line (for batch file)?
                « Reply #11 on: September 12, 2008, 07:20:30 AM »
                I'm still not certain what you are trying to do. At first I thought you  wanted to see if test was in the current directory. Now, based on your code, I think you want to build a path using the parents of the test directory.

                Try not to use the numeric variables within a for loop. While perfectly legal, it only adds confusion with the command line arguments.

                Going with the theory that you want to build a path with the parents of test, this may help you out:

                Code: [Select]
                @echo off
                setlocal enabledelayedexpansion
                for /f "tokens=* delims=\" %%a in ('cd') do (
                set parse=%%a
                set new=!parse:\= !
                )

                for %%G in (%new%) do (
                if .%%G==. goto getout
                  if %%G==test goto getout
                  call set p=%%p%%\%%G
                )
                :getout
                set p=%p:~1%
                echo %p% 

                We're here 24/7, so if I guessed wrong, be sure to let us know. 8)
                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein

                roohi

                  Topic Starter


                  Beginner

                  Re: How to split any line(path) throught command line (for batch file)?
                  « Reply #12 on: September 12, 2008, 08:05:34 AM »
                  Thanks Alot

                  its a currect solution and i got path currectally but i got it twice, thats not a issue .

                  As i m bigginer in batch file programming one thing what i does not understude in this code is

                  if .%%G==. goto getout

                  can u plz tell me what this line is done .

                  Thank u again

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: How to split any line(path) throught command line (for batch file)?
                  « Reply #13 on: September 12, 2008, 08:16:46 AM »
                  Code: [Select]
                  if .%%G==. goto getout

                  The line checks to see if %%G is blank (meaning end of path) and exits the for loop. Writing the code without some character (in this case a dot) will produce an error.  The interpreter uses spaces as a delimiter.

                  Any character can be used in place of the dot.  Personally I use the dot out of tradition and because it's lowercase on the keyboard.

                   8)
                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein

                  roohi

                    Topic Starter


                    Beginner

                    Re: How to split any line(path) throught command line (for batch file)?
                    « Reply #14 on: September 12, 2008, 08:25:56 AM »
                    Thanks for explaning :)