Use For /F to process ONLY the first line of a file?

N

NayJo

I am attempting to nest two For statements like so:


for %%f in (*.log) do (
echo Processing %%f
for /F "tokens=3-5 delims=,/" %%m in (%%f) do (
echo Report Date is %%m%%n%%o
updatefile.exe %%m%%n%%o
goto L1
)
:L1
ren %%f %%f.bak
)

Problem is when I reach :L1 the var %%f seems to be uninitialized.
but when I leave the inner loop, the ren command does not seem to be
executed. I *could* process every line of the log file (ie. remove the goto
and the label) and get the desired result but is there any way just to
process only the first line?

Many thanks in advance.

John C.
 
P

Phil Robyn

NayJo said:
I am attempting to nest two For statements like so:


for %%f in (*.log) do (
echo Processing %%f
for /F "tokens=3-5 delims=,/" %%m in (%%f) do (
echo Report Date is %%m%%n%%o
updatefile.exe %%m%%n%%o
goto L1
)
:L1
ren %%f %%f.bak
)

Problem is when I reach :L1 the var %%f seems to be uninitialized.
but when I leave the inner loop, the ren command does not seem to be
executed. I *could* process every line of the log file (ie. remove the goto
and the label) and get the desired result but is there any way just to
process only the first line?

Many thanks in advance.

John C.

set /p first_line=<c:\yourpath\yourfile.txt

The preceding will put the first line of c:\yourpath\yourfile.txt
into the environment variable first_line. So what exactly do you
mean by 'process'?
 
N

NayJo

Phil Robyn said:
set /p first_line=<c:\yourpath\yourfile.txt

The preceding will put the first line of c:\yourpath\yourfile.txt
into the environment variable first_line. So what exactly do you
mean by 'process'?

Thank you. By 'process' in this case I mean to read a line of data and
extract the date from that line. Without a goto my inner for will read the
entire file and in my case for this particular file type, get the same
result on the first record as on the last, but I would rather read only the
first line. In my case it is just more efficient.
 
W

wayne

I'm not 100% clear on what your loop is doing, but the %%f is always
uninitialized once you leave the FOR loop (in my experience at least.) you
need to set a new variable first. such as...

for %%f in (*.log) do (
echo Processing %%f
for /F "tokens=3-5 delims=,/" %%m in (%%f) do (
echo Report Date is %%m%%n%%o
updatefile.exe %%m%%n%%o
set filename=%%f
goto L1
)
:L1
ren %filename% %filename%.bak
)

----- Original Message -----
From: "NayJo" <[email protected]>
Newsgroups: microsoft.public.win2000.cmdprompt.admin
Sent: Thursday, February 02, 2006 2:51 PM
Subject: Use For /F to process ONLY the first line of a file?
 
N

NayJo

wayne said:
I'm not 100% clear on what your loop is doing, but the %%f is always
uninitialized once you leave the FOR loop (in my experience at least.) you
need to set a new variable first. such as...

for %%f in (*.log) do (
echo Processing %%f
for /F "tokens=3-5 delims=,/" %%m in (%%f) do (
echo Report Date is %%m%%n%%o
updatefile.exe %%m%%n%%o
set filename=%%f
goto L1
)
:L1
ren %filename% %filename%.bak
)

Thank you for your help Wayne.

If my inner for did not have the "goto" the "for /F" would read and act on
every line in the file.

In my case this causes no harm because the result is invariant but since the
file can be up to 1MB in size I would rather not keep doing redundant work.

However, If I use the goto and the inner for is executed once, then I branch
to L1 but the batch falls through the outer 'for' and only processes one
file. (This is using your code fragment above but mine behaves the same
way.)

The first file IS renamed but it is the only file that is processed.

All UpdateFile.exe is doing is updating a fixed length fixed field size data
file with the date of the report.

Thank you for your feedback.

John C.
 
N

NayJo

Okay,

Going by both your responses and trying to get the use of single quotes,
double quotes and % signs right, this is what I ended up with and it works:

for %%f in (*.log) do (
echo Processing %%f
set /P first_line=<%%f
for /F "tokens=3-5 delims=,/" %%m in ("%first_line%") do (
echo Report Date is %%m%%n%%o
updatefile.exe %%m%%n%%o
)
ren %%f Processed_%%f
)

Thank you both for your help with this.

John C.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top