Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

for loop delim problem

102 views
Skip to first unread message

Bob Dylan

unread,
Mar 30, 2012, 5:03:21 AM3/30/12
to
Hi all,

Trying to get a pipe delimited file manipulation batch file set up
and
am having a hard time getting the actual for loop to set my variables
(or getting the echo wrong or something else stupid).


There are three .txt files in c:\bob\nic of the format TOY*.txt, pipe
delimited with about 15 fields.


###############code###############
@echo off
c:
cd c:\bob\nlc


for /f "tokens=1-12* delims=|" %%a in ('dir /b /a-d TOY*.txt') do (
@echo %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p
%
%q %%r %%s %%t
)
#############code##################


############output################
TOY478435.txt %n %o %p %q %r %s %t
TOY478436.txt %n %o %p %q %r %s %t
TOY478437.txt %n %o %p %q %r %s %t


c:\Bob\NLC>type toy478435.txt
13|TOY5|dave coogan|Tel: |64 Dover Close|Blisworth|GB||GH99 5FD|D|
Thanks for your order|0.00|N|N|Contents fragile|1
############output###############


So i can see that the single quoted dir command fills the first and
that my twelve tokens are being initialised but for some reason the
file isn’t being read into the variables. If I can get the variables
set I will be able to write them to a new file in a different order.


Any help appreciated!
Thanks,
Bob

Tom Lavedas

unread,
Mar 31, 2012, 1:14:45 PM3/31/12
to
There are several problems. First you are processing a directory listing about the files, not the files. You need to remove the DIR part in order to read the files themselves ...

for /f "tokens=1-12* delims=|" %%a in (TOY*.txt) do ( ...

Second, you have listed 20 variables, but you say in the tokens that you want to work with no more than 13, so the rest appear as unwanted %ltr strings at the end of your echo.

Since you don't really explain what you hope to accomplish, it is hard to say any more than that.
_____________________
Tom Lavedas

Bob Dylan

unread,
Apr 11, 2012, 8:11:11 AM4/11/12
to
Hi Tom,

Thanks for your answer, unfortunately I did try using the wildcarded
string TOY*.txt as the set and couldnt get it to use the wildcard it
just kept coming back with 'could not find TOY*.txt'. This was with
all types of quote and usebackq. I'm sure I was doing something wrong
but couldnt seem to work out what.

The undefined vars coming back as literal echos is not unexpected, I
just wanted to have more echos than tokens so I could see what was
being initialised and what wasnt. My intention (as alluded to) was to
manipulate a pipe delimited file (to change the field order more
specifically)

The solution in the end was to use a second, nested for loop to
process each line of the 'dir' command output which seems to have done
the trick. Please see below

@echo off
:top
c:
cd c:\nl\input
for /f "delims=" %%a in ('dir /b /a-d TOY*.txt') do (
for /f "tokens=1-17 delims=|" %%b in ('type "%%a"') do (
@echo %%b^|Toy Co^|%%f^|^|^|%%g^|%%i^|COUNTY^|%%d^|%%e > c:\nl\output\%
%a
@echo %%b^|Toy Co^|%%f^|^|^|%%g^|%%i^|COUNTY^|%%d^|%%e
del %%a
)
)
c:\nl\sleep 1
GOTO top

Tom Lavedas

unread,
Apr 11, 2012, 1:28:34 PM4/11/12
to
On Apr 11, 8:11 am, Bob Dylan <xeod...@googlemail.com> wrote:
> On Mar 31, 6:14 pm, Tom Lavedas <tglba...@verizon.net> wrote:
>
>
>
>
>
> > On Friday, March 30, 2012 5:03:21 AM UTC-4, Bob Dylan wrote:
> > > Hi all,
>
> > > Trying to get a pipe delimited file manipulation batch file set up
> > > and
> > > am having a hard time getting the actual for loop to set my variables
> > > (or getting the echo wrong or something else stupid).
>
> > > There are three .txt files in c:\bob\nic of the format TOY*.txt, pipe
> > > delimited with about 15 fields.
>
> > > ###############code###############
> > > @echo off
> > > c:
> > > cd c:\bob\nlc
> > There are several problems.  First you are processing a directory listing about the files, not the files.  You need to remove the DIR part in order to read the files themselves ...
>
> > for /f "tokens=1-12* delims=|" %%a in (TOY*.txt) do ( ...
>
> > Second, you have listed 20 variables, but you say in the tokens that you want to work with no more than 13, so the rest appear as unwanted %ltr strings at the end of your echo.
>
> > Since you don't really explain what you hope to accomplish, it is hard to say any more than that.
> > _____________________
> > Tom Lavedas
>
> Hi Tom,
>
> Thanks for your answer, unfortunately I did try using the wildcarded
> string TOY*.txt as the set and couldnt get it to use the wildcard it
> just kept coming back with 'could not find TOY*.txt'.  This was with
> all types of quote and usebackq.  I'm sure I was doing something wrong
> but couldnt seem to work out what.
>
> The undefined vars coming back as literal echos is not unexpected, I
> just wanted to have more echos than tokens so I could see what was
> being initialised and what wasnt.  My intention (as alluded to) was to
> manipulate a pipe delimited file (to change the field order more
> specifically)
>
> The solution in the end was to use a second, nested for loop to
> process each line of the 'dir' command output which seems to have done
> the trick.  Please see below

You are right, of course. I stand corrected. I guess I had never
actually tried to use a wildcard filespec in a FOR with the /F switch
before. Now that I checked it (and gave it some thought), it makes
sense that it wouldn't.

Sorry for misleading you and glad others gave you the right
directions.
________________________
Tom Lavedas
0 new messages