Author Topic: import file  (Read 8296 times)

pennym

  • Newbie
  • *
  • Posts: 26
import file
« on: September 16, 2013, 11:02:39 am »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #1 on: September 16, 2013, 11:34:25 am »
This can probably be accomplished using COM. Check out the 'Automating Access Tutorial' in the tech database: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP/OLE~with~Access+Automating~Access~Tutorial.txt
Deana F.
Technical Support
Wilson WindowWare Inc.

pennym

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #2 on: September 16, 2013, 11:42:28 am »
I need some help to import the records ado thats no problem.

a csv sample wil be great,

pm

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #3 on: September 16, 2013, 11:56:18 am »
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

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #4 on: September 16, 2013, 01:06:26 pm »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #5 on: September 16, 2013, 02:17:32 pm »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #6 on: September 16, 2013, 02:51:35 pm »
Here is a UNDEBUGGED example based on the limited information provided:

Code: Winbatch
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

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #7 on: September 17, 2013, 08:13:58 am »

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

pm

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #8 on: September 17, 2013, 08:46:48 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

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #9 on: September 17, 2013, 10:50:44 am »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #10 on: September 17, 2013, 01:09:08 pm »
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

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #11 on: September 17, 2013, 01:24:05 pm »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #12 on: September 17, 2013, 01:28:02 pm »
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

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #13 on: September 17, 2013, 01:39:21 pm »
Here is another undebugged attempt to get only the command fields

Code: Winbatch
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

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #14 on: September 18, 2013, 01:18:54 am »
the file in short form

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: import file
« Reply #15 on: September 18, 2013, 07:53:34 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

  • Newbie
  • *
  • Posts: 26
Re: import file
« Reply #16 on: September 21, 2013, 06:57:18 am »
I no that way it is so hard for me.

but thx for your time.

Pm

IJRobson

  • Jr. Member
  • **
  • Posts: 94
Re: import file
« Reply #17 on: September 23, 2013, 08:38:12 am »
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?