Article View: free.it.visual-basic
Article #2071text files
From: Mike Cocker
Date: Wed, 05 Nov 2003 16:48
Date: Wed, 05 Nov 2003 16:48
106 lines
3853 bytes
3853 bytes
I'm having some much fun with these text files. I was hoping someone can
help me. I have a text file (http://www.swandust.com/IN.txt) that I want to
input. I would like it to output as a .csv file with the following format:
Cust,Stock,Quant
438,24 INCH TREATED DUST MOP,11
,42 INCH TREATED DUST MOP,10
,FRAME FOR DUST MOP-24",5
,FRAME FOR DUST MOP-42",5
1267,24 INCH TREATED DUST MOP,8
,FRAME FOR DUST MOP-24",5
1274,42 INCH TREATED DUST MOP,14
,FRAME FOR DUST MOP-42",2
etc...
Keep in mind the number of items in the stock count can change depending on
the customer. According to my cound, I made a stock() array from 0-9,
because I didn't think they'd have more than 9 items. If there is a better
way of doing this, I'd love to know. I'm a beginner with VB, so I tend to
get into trouble quite easily. I want to improve my skills in big way. My
code looks like below, but it prints out only the "customer number" and
"stock item name". It also prints out some other spaces and text that I
don't want. If someone can help me get the above output, that would be
awesome. Thanks in advance.
Private Sub Command1_Click()
Dim mystring As String, j As Long
Dim cusnum As String, printtofile As Boolean
Dim stock(10) As String
Dim fileno1 As Integer, fileno2 As Integer
fileno1 = FreeFile
Open "c:\IN.txt" For Input As fileno1
fileno2 = FreeFile
Open "c:\OUT2.csv" For Output As fileno2
Print #fileno2, "Cust"; ","; "Stock"; ","; "Qty"
Do While Not EOF(fileno1)
printtofile = False 'reset
Line Input #fileno1, mystring
If InStr(mystring, "Customer Number") > 0 Then
cusnum = Trim$(Mid$(mystring, 23, 6))
End If
If InStr(mystring, "=== ========
======================================== ===== = ======== ========") > 0
Then
For j = 0 To 9
Line Input #fileno1, mystring
Call Replace_String
stock(j) = Trim$(mystring)
Next j
printtofile = True
End If
'now write to file
If printtofile = True Then
Print #fileno2, cusnum; ","; Trim$(Mid$(stock(0), 13, 46))
For j = 1 To 9
If stock(i) = "" Then Exit For
Print #fileno2, ","; Trim$(Mid$(stock(j), 13, 46))
Next j
Print #fileno2, 'print blank line
End If
Loop
Close fileno1, fileno2
Print "finished!"
End Sub
Private Sub Replace_String()
mystring = REPLACE$(mystring,
"**************************************************************************"
, "")
End Sub
Technical Details
Message-ID:
<1068057491.237716@ns2.worldwithoutwire.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!wn14feed!wn13feed!worldnet.att.net!207.35.177.252!nf3.bellglobal.com!snoopy.risq.qc.ca!news.uunet.ca!nnrp1.uunet.ca.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f94ee,d44d00ea9af58771,start X-Google-Attributes: gidf94ee,public X-Google-ArrivalTime: 2003-11-05 08:48:47 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!wn14feed!wn13feed!worldnet.att.net!207.35.177.252!nf3.bellglobal.com!snoopy.risq.qc.ca!news.uunet.ca!nnrp1.uunet.ca.POSTED!not-for-mail From: "Mike Cocker" <cockermike@yahoo.com> Newsgroups: free.it.visual-basic Subject: text files Lines: 109 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Organization: none Message-ID: <1068057491.237716@ns2.worldwithoutwire.com> Cache-Post-Path: ns2.worldwithoutwire.com!unknown@209.162.237.59 X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/) Date: Wed, 5 Nov 2003 11:48:43 -0500 NNTP-Posting-Host: 216.185.76.4 X-Trace: nnrp1.uunet.ca 1068050925 216.185.76.4 (Wed, 05 Nov 2003 11:48:45 EST) NNTP-Posting-Date: Wed, 05 Nov 2003 11:48:45 EST