Solved

PARSE

Posted on 2002-05-21
13
149 Views
Last Modified: 2010-05-02
Hello:

The code below parses the first row in the text file and then stops. Please help.
Thanks.

===================================
Private Sub Command1_Click()
Dim File1 As Integer, File2 As Integer
Dim InputFile As String, OutputFile As String
Dim strLine As String
InputFile = "C:\Copy1.txt" 'Change to your desired name
OutputFile = "C:\Copy2.txt" 'Change to your desired name
File1 = FreeFile
Open InputFile For Input As #File1
File2 = FreeFile
Open OutputFile For Output As #File2
Do Until EOF(File1)
   Line Input #File1, strLine 'Read Line by Line
   If Len(Trim(strLine)) > 87 Then
       Print #File2, Left(strLine, 87)
   Else
       Print #File2, strLine
   End If
Loop
Close #File1 'Close both files
Close #File2
End Sub
0
Comment
Question by:muhammasif
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 3

Expert Comment

by:Elmo_
ID: 7024231
muhammasif,

I had a look at your code and it seems fine.  I have tested it as well and it basically made a copy of the first file.

The only thing that it does do is - If the Line is greater than 87 characters it only outputs the first 87 to the new file.

Are you looking for something else here or are you having a different problem?

Cheers,

Ed.
0
 

Author Comment

by:muhammasif
ID: 7024266
When I manually open the first file in Notepad, it shows me a box shaped character at the end of each record. It seems like this character is the culprit.

Although the code looks OK but it outputs only the first record and then stops.

Thanks.
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 7024285
I too ran your code over a "standard" text file (with CR/LF) as record delimiter and it worked perfectly.  I think your record delimiter ("box shaped character") is the problem.  I suspect also that since it is NOT a valid delimiter the entire file is read into strline on the first read...
Find out what the hex value of that character is and then do a replace to substitute a CR or LF for it and I suspect your problem will disappear...  If this file is coming from a third party, ask them about changing this for you.
0
 
LVL 3

Expert Comment

by:Elmo_
ID: 7024375
muhammasif,

I agree with yourself and trkcorp.  This character is the problem and as trkcorp said "since it is NOT a valid delimiter the entire file is read into strline on the first read..."

I have found a way around it that may help, but if this is a third party file, try get them to fix it.

What you can do is open the file As Binary Access.  It should allow you to read past this character.

File1 = FreeFile
Open InputFile For Binary Access Read Lock Read As #File1
File2 = FreeFile
Open OutputFile For Output As #File2
0
 

Author Comment

by:muhammasif
ID: 7024477
Elmo, tried your code. No change. Still reporting first record only.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 7024488
Did this file come from a Unix system?  They typically put only linefeed characters in rather than CR/LF combos.

One solution is this:

File1 = FreeFile
Open InputFile For Input As #File1
strLine = input$(lof(File1),#File1)
close #File1

dim strFileArray() as string
strFileArray = split(strLine, vbLF)

File2 = FreeFile
Open OutputFile For Input As #File2

for iLine = 0 to ubound(strFileArray)
  If Len(Trim(strLine)) > 87 Then
      Print #File2, Left(strFileArray(iLine), 87)
  Else
      Print #File2, strFileArray(iLine)
  End If
next iLine
close #File2
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:KMAN
ID: 7024641
If it is a UNIX file, then an FTP of the file, rather than Samba or NFS file copy, to the windows file system will "fix" the file; that is make the LF into CR/LF.

K
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7024659
Also, if this is a one-time thing, you can drop the file into Wordpad, save it (answer Yes...lose formatting) and the file will be Windows-ready.
0
 

Author Comment

by:muhammasif
ID: 7025512
The file comes from Mainframe (MVS/TSO) to a Linux system on a daily basis.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7025578
Then you should either export it from MVS with CRLF line breaks, or try the code I offered.  Note that some O/Ss use LF as line breaks, and others use CR, so the "split" line in my code should reflect the proper code:

' For Unix:
...
strFileArray = split(strLine, vbLF)
...

' For some other O/Ss:
...
strFileArray = split(strLine, vbCR)
...
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 7025579
Have you been able to determine the value of the delimiter? <<("box shaped character")>>
0
 

Author Comment

by:muhammasif
ID: 7030534
Excellent.

The character was a Line Feed - 0A.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7030709
Ah, yes, Line Feed: the dreaded mainframe line delimiter!

Glad you got it working.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now