How to get a variable substring in a for loop

Discussion in 'Windows XP General' started by ShadowTek, Jun 7, 2008.

  1. ShadowTek

    ShadowTek Guest

    I know how to pull a substring out of a variable, but I don't know how
    to get a substring out of a variable in a for loop.

    When I try the folluwing command...

    for %G in (Input_Files\*) do echo %G:~12%

    ....I get this:

    Input_Files\New Text Document.txt:~12%

    But what I am trying to get is this:

    New Text Document.txt

    What is wrong with this?
     
    ShadowTek, Jun 7, 2008
    #1
    1. Advertisements

  2. "ShadowTek" <> wrote in message
    news:...
    >I know how to pull a substring out of a variable, but I don't know how
    > to get a substring out of a variable in a for loop.
    >
    > When I try the folluwing command...
    >
    > for %G in (Input_Files\*) do echo %G:~12%
    >
    > ...I get this:
    >
    > Input_Files\New Text Document.txt:~12%
    >
    > But what I am trying to get is this:
    >
    > New Text Document.txt
    >
    > What is wrong with this?


    Substring manipulation works with environmental variables.
    %G is not an environmental variable, hence your problem.
    There two ways to resolve this issue. Either like so:
    @echo off
    setlocal EnableDelayedExpansion
    for %%G in (InputFiles\*.*) do (
    set Name=%%G
    echo !Name:~12!
    )

    or like so:
    @echo off
    for %%G in (InputFiles\*.*) do call :Sub %%G
    goto :eof
    :Sub
    set Name=%*
    echo %Name:~12%
    Note that your choice of coding is fragile because
    you do not specify an absolute path for the InputFiles
    Folder. To make the batch file robust you must
    replace this by "c:\InputFiles" (for example).
     
    Pegasus \(MVP\), Jun 7, 2008
    #2
    1. Advertisements

  3. ShadowTek

    ShadowTek Guest

    Thanks
     
    ShadowTek, Jun 7, 2008
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Search Question

    Text Search: how to search a substring using search companion

    Search Question, Mar 22, 2004, in forum: Windows XP General
    Replies:
    2
    Views:
    547
    Alex Nichol
    Mar 23, 2004
  2. Guest

    Stuck in a loop t loop

    Guest, Apr 24, 2006, in forum: Windows XP General
    Replies:
    0
    Views:
    269
    Guest
    Apr 24, 2006
  3. Guest
    Replies:
    4
    Views:
    140
    Pegasus \(MVP\)
    Jul 5, 2007
  4. emrefan

    for loop variable insanity

    emrefan, Sep 12, 2008, in forum: Windows XP General
    Replies:
    8
    Views:
    177
    Pegasus \(MVP\)
    Sep 22, 2008
  5. The Seabat

    Re: Get ubuntu ! Get ubuntu ! Get ubuntu ! Get ubuntu !

    The Seabat, Jun 2, 2009, in forum: Windows XP General
    Replies:
    0
    Views:
    340
    The Seabat
    Jun 2, 2009
Loading...

Share This Page