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

Author Topic: For Loops and Strings in DOS Batch Files  (Read 21154 times)

0 Members and 2 Guests are viewing this topic.

donstafford

  • Guest
For Loops and Strings in DOS Batch Files
« on: November 04, 2011, 09:11:07 AM »
I am in need of a batch file to do the following:

for %%cust in (abcde fghij klmno pqrst uvwxy) do command %%cust,XX,YY,%%substringofcust

In order words, the 'command' should be:

command abcde,XX,YY,abc
command fghij,XX,YY,fgh
command klmno,XX,YY,klm

etc..........

I can't figure out how to pull only a portion of the customer

Salmon Trout

  • Guest
Re: For Loops and Strings in DOS Batch Files
« Reply #1 on: November 14, 2011, 01:06:53 PM »
I think your description of the the task requirements is just a tad too opaque.

Raven19528



    Hopeful
  • Thanked: 30
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: For Loops and Strings in DOS Batch Files
    « Reply #2 on: November 16, 2011, 06:05:58 PM »
    Set up your list in a file. Ex:

    <<List.txt>>
    abcde
    fghij
    klmno
    pqrst
    uvwxy


    Then use the following batch:

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion

    for /f "delims=" %%A in (List.txt) do (
      set sub=%%A
      set sub=!sub:~0,3!
      command %%A,XX,YY,!sub!
    )

    The way to grab the substring is in the second line. What it is saying is SET the SUB variable to the SUB variable offset by 0 characters for 3 characters. Essentially, take the first 3 characters of the sub variable (which was previously set to be the customer string) and make them the new sub variable. This only works if you know that you are always going to use the characters at specific positions within the string. If what you are looking for varies in it's location, it will take some different coding to get it. Please let us know if this is the case.
    "All things that are
    Are with more spirit chased than enjoy'd" -Shakespeare