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

Batch file for reading the first line of a file

1,053 views
Skip to first unread message

arul...@yahoo.com

unread,
May 11, 2005, 7:42:13 AM5/11/05
to
Hi,

Following is the batch file code for reading the first line of a file,

@echo off
:: filename.ext is the file to be processed
copy /y filename.ext %temp%.\t1.bat > nul
echo eEA BE 00 01 46 80 3C 0A 75 FA 81 EE> %temp%.\t2.dat
echo eF5 FA 00 89 F1 53 45 54 20 25 31 3D>> %temp%.\t2.dat
echo g=EA F9>> %temp%.\t2.dat
for %%? in (wF9 q) do echo %%?>> %temp%.\t2.dat
DEBUG %temp%.\t1.bat < %temp%.\t2.dat > nul
:: Set below the variable name where the string should be saved
call %temp%.\t1.bat MyString
for %%? in (t1.bat t2.dat) do del %temp%.\%%?

taken from
http://www.geocities.com/leopignataro86/batchfiles/index.htm#25 link.

The logic of this program is,
1) It makes a copy of the file to be processed to .bat file
2) Assigns the first line to SET %1=
3) Detects the end of the first line and makes the file end there
4) Then the batch file is executed.
5) And finally the first line is assigned to MyString

My problem is, I am not able to understand the exact meaning of
following lines code implementing the logic,

echo eEA BE 00 01 46 80 3C 0A 75 FA 81 EE> %temp%.\t2.dat
echo eF5 FA 00 89 F1 53 45 54 20 25 31 3D>> %temp%.\t2.dat
echo g=EA F9>> %temp%.\t2.dat
for %%? in (wF9 q) do echo %%?>> %temp%.\t2.dat
DEBUG %temp%.\t1.bat < %temp%.\t2.dat > nul

Can someone please help me in understanding the above logic.

Thanks
Arul E

William Allen

unread,
May 11, 2005, 10:38:46 AM5/11/05
to
<arul...@yahoo.com> wrote in message
...snip (Read this post in a fixed-width font)

> My problem is, I am not able to understand the exact meaning of
> following lines code implementing the logic,
>
> echo eEA BE 00 01 46 80 3C 0A 75 FA 81 EE> %temp%.\t2.dat
> echo eF5 FA 00 89 F1 53 45 54 20 25 31 3D>> %temp%.\t2.dat

The small debug script in T2.DAT is made up as follows:
The two ECHO lines above are the hex-packed code for this routine:

0EA BE0001 MOV SI,0100 Load 100 in SI (this is
where the file starts)
0ED 46 INC SI Bump SI to look along the file
0EE 803C0A CMP BYTE PTR [SI],0A Is it a Linefeed?
0F1 75FA JNZ 00ED Jump back to INC SI if not
0F3 81EEFA00 SUB SI,00FA Sub offset FA from SI, this
allows for length of data
bytes below and the length of
the first line of the file
0F7 89F1 MOV CX,SI Set length to write in CX
0F9 5345542025313D data byte string:"SET %1="
100 this is where the file starts (the code above works out where
the first line ends and adjusts the length to write in CX so that
the SET %1= is written in front of it.

> echo g=EA F9>> %temp%.\t2.dat

This executes the above routine from 0EA to 0F9 (not counting the byte
at 0F9), in other words it executes the above small program.

> for %%? in (wF9 q) do echo %%?>> %temp%.\t2.dat

the WF9 writes from offset F9 for CX bytes, in other words it writes
the SET %1= plus the first line of the file.

> DEBUG %temp%.\t1.bat < %temp%.\t2.dat > nul

This executes the whole process described above to produce a temporary
Batch file that consists of:

SET %1=(first line of file)

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/


Pegasus (MVP)

unread,
May 11, 2005, 10:46:41 AM5/11/05
to

<arul...@yahoo.com> wrote in message
news:1115811733.6...@f14g2000cwb.googlegroups.com...

To read just the first line of a text file, you could do this:

@echo off
for /F %%a in (test.txt) do (
set line=%%a
goto Next
)
:Next
echo First line=%line%

This works fine as long as the first line does not have any
"poison" characters (", %, ^ etc.).


billious

unread,
May 11, 2005, 11:51:34 AM5/11/05
to

<arul...@yahoo.com> wrote in message
news:1115811733.6...@f14g2000cwb.googlegroups.com...

Do you want to show the first line of a file, set an environment variable to
the contents of the first line of a file, or find out how the above batch
works?

In NT4, 2K or XP, the following will set an environment variable and display
the first NON-BLANK line of a file:

@echo off
set yl1=
for /f "tokens=*" %%i in (poison.txt) do if not defined yl1 set yl1=%%i&echo
%%i


(which sets the environment variable "YL1" to the contents of the first
non-blank line)
* Omit the "&echo %%i" to NOT show the first line - set the environment
variable only
* follow with another
set yl1=
to show without permanently setting the environment variable

Where POISON.TXT is a text file which MAY contain "poison characters"

(My test file was...)

A line of many < and >varied %poison ^ characters | like "," and so on
another line
and another

Now actually USING an environment variable containing poison characters -
that's another story.


As for the batch you posted, it's actually a debug script, implementing an
assembler program...nothing to do with batch per se, and hieroglyphics
except to an assembler programmer. Some people seem to believe that
packaging a program written in a language of their choice and appropriately
wrapping it in batch commands turns it into a "batch program." Sometimes
it's necessary because of the inadequacies of the batch language, other
times it's a lack of sneakitude.

It's difficult to tell at times whether to post a program that solves the
problem or to simply do as best can be done within the debatable boundaries
of the batch language.

HTH

...Bill

Phil Robyn

unread,
May 11, 2005, 12:45:24 PM5/11/05
to
arul...@yahoo.com wrote:
> Hi,
>
> Following is the batch file code for reading the first line of a file,

Here's a much shorter version:

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

--
Phil Robyn
Univ. of California, Berkeley

billious

unread,
May 11, 2005, 7:46:44 PM5/11/05
to

"Phil Robyn" <pro...@berkeley.edu> wrote in message
news:d5tcr5$11eu$1...@agate.berkeley.edu...

> arul...@yahoo.com wrote:
>> Hi,
>>
>> Following is the batch file code for reading the first line of a file,
>
> Here's a much shorter version:
>
> set /p first_line=<x:\yourpath\yourfile.txt
>

Doesn't work on NT, though...

...Bill


nospam...@ualberta.ca

unread,
May 12, 2005, 11:13:03 AM5/12/05
to
arul...@yahoo.com wrote in news:1115811733.686663.207230
@f14g2000cwb.googlegroups.com:

The ECHO commands are creating a file with DEBUG commands in it that are
then fed to the DEBUG program to assemble a small executable program in
memory and run it.

If one is going to go this sort of route, they could just get a port of the
UNIX "head" command, which can be used to read the first n lines of a file.

Rauf Sarwar

unread,
May 12, 2005, 12:16:42 PM5/12/05
to

for /F "tokens=*" %%A in ('findstr /n $ file.txt^|findstr /b 1:') do (
set fline=%%A
)
set fline=%fline:~2%

This grabs the first line... blank or not.

Regards
/Rauf

arul...@yahoo.com

unread,
May 23, 2005, 9:02:26 AM5/23/05
to
Thank you for all of your responses.

I have a file named test.pgp. The first line of this file contains a
date like,

0,"05/22/2005"

I want to make a copy of the file test.pgp with the name dest0522.pgp.

0522 in the file name refers to the date in the first line of test.pgp
file.

This date in the file keeps on changing from time to time.

Could someone correct the following piece of code to meet the
requirement.

@echo off
set MyString=
for /f "tokens=*" %%i in (D:\test.pgp) do if not defined MyString set
MyString=%%i&echo
%%i
set sDD="%MyString:~6,2%"
set sMM="%MyString:~3,2%"
copy D:\test.pgp
D:\"file"%sMM%%sDD%".pgp"

Thanks
Arul E

foxidrive

unread,
May 23, 2005, 10:43:27 AM5/23/05
to
On 23 May 2005 06:02:26 -0700, arul...@yahoo.com wrote:

> I have a file named test.pgp. The first line of this file contains a
> date like,
>
> 0,"05/22/2005"
>
> I want to make a copy of the file test.pgp with the name dest0522.pgp.
>
> 0522 in the file name refers to the date in the first line of test.pgp
> file.
>
> This date in the file keeps on changing from time to time.
>
> Could someone correct the following piece of code to meet the
> requirement.
>

I took a different approach, though your string parsing was fine.

@echo off
set file=D:\test.pgp

for %%a in ("%file%") do (
set name=%%~dpna
set ext=%%~xa
)

for /f "tokens=2 delims=," %%i in (%file%) do set datex=%%i
for /f "tokens=1,2 delims=/" %%i in (%datex%) do (
set sMM=%%i
set sDD=%%j
)
copy /b "%file%" "%name%%sMM%%sDD%%ext%"

billious

unread,
May 23, 2005, 11:06:14 AM5/23/05
to

[prior discussion snipped]

<arul...@yahoo.com> wrote in message
news:1116853346.4...@g44g2000cwa.googlegroups.com...


> Thank you for all of your responses.
>
> I have a file named test.pgp. The first line of this file contains a
> date like,
>
> 0,"05/22/2005"
>
> I want to make a copy of the file test.pgp with the name dest0522.pgp.
>
> 0522 in the file name refers to the date in the first line of test.pgp
> file.
>
> This date in the file keeps on changing from time to time.
>
> Could someone correct the following piece of code to meet the
> requirement.
>
> @echo off
> set MyString=
> for /f "tokens=*" %%i in (D:\test.pgp) do if not defined MyString set
> MyString=%%i&echo
> %%i
> set sDD="%MyString:~6,2%"
> set sMM="%MyString:~3,2%"
> copy D:\test.pgp
> D:\"file"%sMM%%sDD%".pgp"
>
> Thanks
> Arul E


Protocol : please post AFTER the existing discussion, not BEFORE.

Your program worked perfectly for me, both in NT4 and XPHSP2

It could be that you have split lines in the wrong place if it's not working
for you. Batch is extremely sensitive to line-breaks. Your batch file should
be:

[1]@echo off
[2]set MyString=
[3]for /f "tokens=*" %%i in (c:test.pgp) do if not defined MyString set
MyString=%%i&echo %%i
[4]set sDD="%MyString:~6,2%"
[5]set sMM="%MyString:~3,2%"
[6]copy c:test.pgp c:\destdir\"file"%sMM%%sDD%".pgp"


Each line begins [number]. Lines will be wrapped in transmission and need to
be rejoined. The [number] at the beginning of each line needs to be removed.

The double-quote characters in lines [4],[5] and [6] are not required, and
can be removed. They appear to cause no harm however.

I've also changed the source filename and the destination directory to suit
my setup, but that should be of no consequence.

What difficulty are you having? What filename is the program generating? Are
there any error messages? Is the first line format actually '0,"05/22/2005"
' as you describe, or can the "0," part have multiple digits?

...Bill


arul...@yahoo.com

unread,
May 24, 2005, 8:20:36 AM5/24/05
to
The first line of the source file will be always of the format,

0,"MM/DD/YYYY"

MM - stands for month, DD - stands for day and YYYY - stands for year.

for example,

1) if the first line of the source file is,
0,"05/23/2005"
then the destination file name should be "file0523.CASHMTCH.pgp"

2) if the first line of the source file is,
0,"05/12/2005"
then the destination file name should be "file0512.CASHMTCH.pgp"

Now my program,

@echo off
set MyString=
for /f "tokens=*" %%i in (D:\test.pgp) do if not defined MyString set
MyString=%%i&echo %%i
set sDD=%MyString:~6,2%

set sMM=%MyString:~3,2%


copy D:\test.pgp D:\file"%sMM%%sDD%".pgp

generates the destination file with the name "file.CASHMTCH.pgp"
instead of "file0523.CASHMTCH.pgp", if test.pgp contains the first line
as

0,"05/23/2005"

The MM, DD part in the first line of the source file is not getting
included in the file name of the destination file.

This is my problem.

Thanks,
Arul E

foxidrive

unread,
May 24, 2005, 8:37:54 AM5/24/05
to
On 24 May 2005 05:20:36 -0700, arul...@yahoo.com wrote:

> The first line of the source file will be always of the format,
>
> 0,"MM/DD/YYYY"
>
> MM - stands for month, DD - stands for day and YYYY - stands for year.
>
> for example,
>
> 1) if the first line of the source file is,
> 0,"05/23/2005"
> then the destination file name should be "file0523.CASHMTCH.pgp"
>
> 2) if the first line of the source file is,
> 0,"05/12/2005"
> then the destination file name should be "file0512.CASHMTCH.pgp"
>
> Now my program,

Try this - I didn't test it.

@echo off
for /f "tokens=*" %%i in (D:\test.pgp) do (
set MyString=%%i&echo %%i&goto :next)
:next


set sDD=%MyString:~6,2%
set sMM=%MyString:~3,2%

copy D:\test.pgp D:\file%sMM%%sDD%.CASHMTCH.pgp

billious

unread,
May 24, 2005, 7:40:41 PM5/24/05
to

<arul...@yahoo.com> wrote in message
news:1116937236.0...@z14g2000cwz.googlegroups.com...

Hmmm...I believe you're leaving something important out.

You appear to be posting an edited version your batchfile, since "CASHMTCH"
appears from nowhere and the "." between 'file' and '"%sMM%' is missing in
your batch but reported as present in your generated filename.

How do you know that the first line of your sourcefile is '0,"date" ' - is
that what is shown on the screen when you execute the batch (since you ECHO
%%i ??)

It could be that %%i, and hence mystring actually contains
'...CASHMTCH...<backspaces>0,"date"' - but even then, the set ...~x,2 will
set sMM and sDD to a 2-character string each, and ".CASHMTCH" is a
9-character string.

If this "backspaces" condition is the case, change "~6,2" to "~-8,2" and
"~3,2" to "~-11,2" (8 from the end, 11 from the end; each for a length of 2)

OR it could be that the first line of the file contains some invisible
non-blank characters. It would be useful if you revealed what was actually
reported on the screen, since you ECHO %%i after having set mystring....

....Bill

arul...@yahoo.com

unread,
May 25, 2005, 11:19:31 AM5/25/05
to
Sorry for the confusion.

I was working on 2 different files. (One for testing and one for the
original requirement)

The first line of the source file will always be of the format as per
my requirement,

0,"MM/DD/YYYY"

MM - stands for month, DD - stands for day and YYYY - stands for year.


There will not be any blank spaces in the first line.
I was the one who created the source file in the above format.

The output which I got on the screen was just,
%i

I don't know whether the first line is copied into MyString.

After reading the entire first line into MyString, I want to read the
4th and 5th characters for DD and 7th & 8th characters for MM value.

Then I want to copy the contents of test.pgp to a new file with the
following name,

fileMMDD.pgp

MM and DD should be the values read from test.pgp.

The first line of test.pgp can be,

0,"05/22/2005"

The first line will not contain blanks. I am testing my program only
with this scenario.
So the corresponding name of the destination file should be,

file0522.pgp

But what I am getting is file .pgp
2 blank spaces after "file" in the file name.

You can test the below program with test.pgp file, containing the first
line as 0,"05/22/2005"

@echo off
set MyString=
for /f "tokens=*" %%i in (D:\test.pgp) do if not defined MyString set
MyString=%%i&echo %%i
set sDD=%MyString:~6,2%
set sMM=%MyString:~3,2%
copy D:\test.pgp D:\file"%sMM%%sDD%".pgp

Thanks,
Arul E

billious

unread,
May 25, 2005, 10:30:00 PM5/25/05
to

<arul...@yahoo.com> wrote in message
news:1117034371.4...@o13g2000cwo.googlegroups.com...

Hmmm...

The program that I posted worked on both XPHSP2 and NT4 using a file with a
first line as you describe.

I suspect that you may have included spaces before or after the "=" in the
SET statements. Note that

set mystring=something

sets the variable "mystring" to the value "something", whereas

set mystring = something

sets the variable "mystring " to the value " something"

I'm also suspicious of the screen report "%i" This would indicate that
"echo %%i" is on its own line, not on the same line as the "for".

You can examine you environment variables by typing

SET

at the prompt.

If you type

SET m

then all variables starting with M will be listed (this is why I start all
of my variables with Y - they're at the end of the list when I type SET, and
I don't have to scroll back or MORE the list.)


...Bill


0 new messages