import file

Started by pennym, September 16, 2013, 11:02:39 AM

Previous topic - Next topic

pennym

hi,

I want to read this and put in in records and files.
Befor i use this als basic but it seem not to work good

file=strlower(file)

; Allocate a buffer much larger than required.
bb=BinaryAlloc(FileSize(file)*4)
structure=BinaryTagInit(bb,"<%count%>","<%/count%>")
BinaryEodSet(bb,0)
aa=""
BinaryRead(bb,file)

the input file is

<1.01>
field 2
</1.01>
<1.02>
field 2
</1.02>
<1.04>
<Command>field1
field2
</1.04>
<1.06>
field 1
field 2
</1.06>
<2.0>
field 1
field 2
</2.0>

structure=BinaryTagFind(structure)
   If structure=="" Then Break ; All done
   data=BinaryTagExtr(structure,0)

afther this export the file to access

Is there easier way to import the above file to import it directly to access.

pm



Deana

Deana F.
Technical Support
Wilson WindowWare Inc.

pennym

I need some help to import the records ado thats no problem.

a csv sample wil be great,

pm

Deana

It is not clear what exactly you need help with. Could you please rephrase the question. Be a specific as possible.

If it is working with the template file, please read this tutorial carefully:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+Tutorials+Template~File~Processing.txt
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym

Hi,
I want to import this file to cvs format.

the file format is as follow

<1.01>
<command> start
data
</101>


the output needs to be like this

test.cvs
nr,command,data
101,start,data
102,start.data


i need some litle help
thx
PM



Deana

Can you provide some more information about this input file. How is it generated? Do you have control over its format? What data format is this file using exactly?
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

Here is a UNDEBUGGED example based on the limited information provided:

Code (winbatch) Select
inputfile = 'c:\public\temp.txt'
data = FileGet(inputfile)

; Parse out floating point numbers
arrArray = ArrayFileGet(inputfile)
For strItem = 0 to ArrInfo(arrArray,1)-1
   num = 0
   str =  arrArray[strItem]
   if StrSub( str, 1, 2 ) == "</" then Continue
   if StrSub( str, 1, 1 ) == "<"
      num = StrSub( str, 2, StrLen(str)-2 )
      if IsFloat(num)
         ;Pause(0,num)
         ; Parse out chunk of data based on floating point number
         startstr = '<':num:'>':@CRLF
         startptr = StrIndex( data, startstr, 1, @Fwdscan )+StrLen(startstr)
         endstr = '</':num:'>':@CRLF
         endptr = StrIndex( data, endstr, 1, @Fwdscan )
         record = StrSub( data, startptr, endptr-startptr )
         Pause(num, record)
      endif
   endif   
Next
exit
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym


How do i get <command> as a extra field. i the record .

pm

Deana

Quote from: perrym on September 17, 2013, 08:13:58 AM

How do i get <command> as a extra field. i the record .

pm

Most of the code was written for you...please post the code you have tried so far. Not clear exactly what you are asking. Please be as descriptive as possible.
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym

The thing what I am trying is to export ascii file  to a cvs file but the file format is hard for me.
Normaly I used binary function for this  but the extra <command> fiels is the problem for me.

The binary code with binary fuction see downstairs.
This was the basic code missing some stuff is missing.

inside the ascii file
<1.01>
<Command>data1</Command>
Data2
</1.01>
<1.02>
<Command>data2</Command>
Data2
</1.02>

<1.01>  #is a field
<command>  data1 </command> # I need the data in data1 in command field
Data 2   # is a field with data
</1.01>  #end of record


old binery code
file=ââ,¬â,,¢test.txtââ,¬â,,¢

; Allocate a buffer

bb=BinaryAlloc(FileSize(file)*4)
structure=BinaryTagInit(bb,"<","</")
BinaryEodSet(bb,0)
aa=""
BinaryRead(bb,file)

While 1
structure=BinaryTagFind(structure)
   If structure=="" Then Break ; All done
   data=BinaryTagExtr(structure,0)

pause(data,count)
endwhile

Deana

Did you try the code I posted? You will simply need to add code to get the data between the <command> </command> tags. I recommend using StrIndex and StrSub.  See previously posted code for a code sample.
<P>
If you'd like you can post the input file as an attachment. Also post Exactly how the output file should be formatted.
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym

the start,
thanks for your support


inputfile = 'd:\a.txt'
data = FileGet(inputfile)

; Parse out floating point numbers
arrArray = ArrayFileGet(inputfile)
For strItem = 0 to ArrInfo(arrArray,1)-1

   num = 0
   str =  arrArray[strItem]
   if StrSub( str, 1, 2 ) == "</" then Continue
   if StrSub( str, 1, 1 ) == "<"
      num = StrSub( str, 2, StrLen(str)-2 )
      if IsFloat(num)
         ;Pause(0,num)
         ; Parse out chunk of data based on floating point number
         startstr = '<':num:'>':@LF
         startptr = StrIndex( data, startstr, 1, @Fwdscan )+StrLen(startstr)
         endstr = '</':num:'>':@LF
         endptr = StrIndex( data, endstr, 1, @Fwdscan )
         record = StrSub( data, startptr, endptr-startptr )
         pos = StrIndexWild(record, "<Command>", 1)
         ret = StrSubWild(record, "<Command>", pos)
         pos1 = StrIndexWild(record, "</Command>", 1)
         ret1 = StrSubWild(record, "</Command>", pos1)
         record2=StrSub(record, pos, pos1 - pos)
                       record2=StrReplace(record2,"<Command>","")
         record1=StrReplace(record,"<Command>","")
         record1=StrReplace(record1,"</Command>","")
                       pause(record2,record2)

      pause(num, record1)
      endif
   endif   
Next
exit




Deana

Post "a.txt". Also attach a file that shows exactly how you expect the output to be formatted.
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

Here is another undebugged attempt to get only the command fields

Code (winbatch) Select
inputfile = 'c:\temp\temp.txt'
filedelimiter = @CRLF
data = FileGet(inputfile)

; Parse out floating point numbers
arrArray = ArrayFileGet(inputfile)
For strItem = 0 to ArrInfo(arrArray,1)-1
   recnum = 0
   str =  arrArray[strItem]
   if StrSub( str, 1, 2 ) == "</" then Continue
   if StrSub( str, 1, 1 ) == "<"
      recnum = StrSub( str, 2, StrLen(str)-2 )
      if IsFloat(recnum)
         ;Pause(0,num)
         ; Parse out chunk of data based on floating point number
         startstr = '<':recnum:'>':filedelimiter
         startptr = StrIndex( data, startstr, 1, @Fwdscan )+StrLen(startstr)
         endstr = '</':recnum:'>':filedelimiter
         endptr = StrIndex( data, endstr, 1, @Fwdscan )
         rec_value = StrSub( data, startptr, endptr-startptr )
         ; Check if command exists in record
         startstr = '<command>'
         startptr = StrIndexNC( rec_value, startstr, 1, @Fwdscan )
         if startptr != 0
            startptr =  startptr+StrLen(startstr)
            endstr = '</command>'
            endptr = StrIndexNC( rec_value, endstr, startptr, @Fwdscan )
            cmd = StrSub( rec_value, startptr, endptr-startptr )
            Pause(recnum : " : Command",cmd)
         endif
      endif
   endif   
Next
exit
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym


Deana

Quote from: perrym on September 18, 2013, 01:18:54 AM
the file in short form

That file attachments doesn't seem to contain a formatting standard, which would be required for parsing.
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym

I no that way it is so hard for me.

but thx for your time.

Pm

IJRobson

As the file is an ASCII file why not just use Fileopen() and FileRead() to read one line at a time and then set up a set of rules dependent on the contents of the Line.

For example if the Line starts "<" then you have a delimiter.  </ is an end delimiter and this can be anywhere in the Line.  If you flag up the last <Command> read you may be able to use that to decide what to do with the the remaining test until you reach "</".

For the basic text I often use parsedata() on each Line then look at first param0 to see how many items (space separated) are on the line.  Using a.txt as an example you can then you can look at Param3 and if it is "AM" or "PM" then you know it if a directory listing and Param1 is the Date,  Param4 is the File Size and Param5+ is the File name etc.

If you open a second File for writing then as you parse each line you can decide what information from that Line you need write to the file and in what format.

You can refine the Rules as you parse more data and find formatting problems?