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 44617 times)

0 Members and 1 Guest are viewing this topic.

Dias de verano

  • Guest
Re: How to split any line(path) throught command line (for batch file)?
« Reply #15 on: September 12, 2008, 10:11:09 AM »
Code: [Select]
if .%%G==. goto getout

That's a new one on me. I am used to doing it this way

Code: [Select]
if "%%G"=="" goto getout

roohi

    Topic Starter


    Beginner

    Re: How to split any line(path) throught command line (for batch file)?
    « Reply #16 on: September 17, 2008, 06:27:20 AM »
    hi side winder
    i used the code send by you and after that code i have to set some enviorment
    but after that it doesnot set any enviorment varible, can u tell me why this happens?

    i think because of setlocal enabledelayedexpansion

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: How to split any line(path) throught command line (for batch file)?
    « Reply #17 on: September 17, 2008, 06:47:39 AM »
    Quote
    i think because of setlocal enabledelayedexpansion

    Indeed it is. Usually I try not to use setlocal for this very reason. The call  instruction can be put to good use in this context.

    Code: [Select]
    @echo off
    for /f "tokens=* delims=\" %%a in ('cd') do (
    set parse=%%a
    call 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% 

    After the batch file executes, the variable p will contain the directory name.

    Good luck. 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 #18 on: September 17, 2008, 07:06:38 AM »
      i used following code

      setlocal enabledelayedexpansion
      set DIR=
      for /f "tokens=* delims=\" %%P in ('cd') do (
         set mypath=%%P
         set array=!mypath:\= !
         )
         
      for %%E in (%array%) do (
         if .%%E==. goto getout
        if %%E==test goto getout
        call set DIR=%%DIR%%\%%E
      )
      :getout

      set TOP_DIR=%DIR%//test1

      and if i removed setlocal enabledelayedexpansion
      and prints valuse of TOP_DIR = \!mypath:\\!\\test1

      and if print the value of TOP_DIR with enabledelayedexpansion
      it is TOP_DIR = %TOP_DIR%
      that is no value is set

      so now what should done
      « Last Edit: September 17, 2008, 07:39:57 AM by roohi »

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: How to split any line(path) throught command line (for batch file)?
      « Reply #19 on: September 17, 2008, 07:47:47 AM »
      If you remove the setlocal statement, you must also remove any references to delayed expansion:

      Code: [Select]
      set DIR=
      for /f "tokens=* delims=\" %%P in ('cd') do (
         set mypath=%%P
         call set array=%%mypath:\= %%
         )
         
      for %%E in (%array%) do (
         if .%%E==. goto getout
        if %%E==test goto getout
        call set DIR=%%DIR%%\%%E
      )
      :getout

      set TOP_DIR=%DIR%//test1

      Why are you using both forward and backward slashes? They are not interchangeable.

      Why keep changing the variable (both declared and generated) names? Many people read these posts; let's try and keep it simple.

      I thought you were trying to find the parents of the test directory on the path. Now I see you're appending test1 to the generated path. Any more surprises ???
      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 #20 on: September 17, 2008, 07:58:00 AM »
        first of all sorry for changing variables .


        now let me explain you what i was doing
        i find out the the parents of the test directory, which i found with the hlep of ur code.
        After then i want to set some eviorment variables using this DIR(perent directory ) path.
        and that variables value should be fixed till that command prompt is open or setup is runs again.

        so finally The thing is done . :)
        No more surprisess.
        and thanks for that much support.