Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More help opening TEXT files.. 1

Status
Not open for further replies.

camidon

Programmer
May 9, 2000
268
US
Okay, I have some some text files that I'm trying to edit and then string together. I've altered my code a little and was trying to get some additional Ideas. Here is what my code looks like now:
Code:
Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim FileObject
Dim SourceFolder
Dim FileCollection
Dim File
Dim Readfile
Dim ThisTxt
Dim FinalTxt
Dim WriteFile
Dim Filename

Set FileObject = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FileObject.GetFolder("C:\files\") 
Set FileCollection = SourceFolder.Files

for each File in FileCollection
	'FileName = "C:\files\" & File.Name
	Set Readfile = File.OpenAsTextStream(ForReading)
	ThisTxt = ReadFile.Readall
	Readfile.close
    	
	ThisTxt = Replace(ThisTxt, vbCRLF, "")
	ThisTxt = replace(ThisTxt, "~", "~" & vbCRLF)
	FinalTxt = FinalTxt & vbCRLF & ThisTxt
Next

FileObject.CreateTextFile("c:\pmsp0000.txt")
Set FileName = FileObject.GetFile("c:\pmsp0000.txt")
Set WriteFile = FileName.OpenAsTextStream(ForWriting)
WriteFile.Write(FinalTxt)
WriteFile.Close

Msgbox "Done adding carriage returns and combining files!"

The data that I'm supposed to be getting back should look like this:

ST TRANSACTION SET HEADER
ST*837*0021~
BHT BEGINNING OF HIERARCHICAL TRANSACTION
BHT*0019*00*0123*19981015*1023*RP~
REF TRANSMISSION TYPE IDENTIFICATION
REF*87*004010X098~

What I'm actually getting back looks like this:

=¥»¡–ï;ä`á—»ÒÝléζ ªûˆ§<ÿùÜ*ÞcHrÎí™î‹>챕ê§þ –ßsðž~
JÌ PK  K™p0l¾2 ¯
 ¶ pmsp5440.clmUT ~

It seems to be the right AMOUNT of data but as you can see it's no good. And it doesn't matter what you put in for the FORMAT argument for the OpenAsTextStream Method. I've tried all the options.

Thanks for any help,

Chris
 
Try this instead:

Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim FileObject
Dim SourceFolder
Dim FileCollection
Dim File
Dim Readfile
Dim ThisTxt
Dim WriteFile
Dim Filename
Dim Report

Set FileObject = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FileObject.GetFolder("C:\Test\")
Set FileCollection = SourceFolder.Files

Report = ""
for each File in FileCollection
'FileName = "C:\files\" & File.Name
Set Readfile = File.OpenAsTextStream(ForReading)
ThisTxt = ReadFile.Readall
Readfile.close
Report = Report & ThisTxt & vbCRLF
Next

FileObject.CreateTextFile("c:\Test\pmsp0000.txt")
Set FileName = FileObject.GetFile("c:\Test\pmsp0000.txt")
Set WriteFile = FileName.OpenAsTextStream(ForWriting)
WriteFile.Write(Report)
WriteFile.Close

Msgbox "Done adding carriage returns and combining files!"

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
I still get garbage.. Slightly different garbage, but still garbage. It now comes out looking like this:

PK =¥»¡–ï;ä`á—»ÒÝléζ ªûˆ§<ÿùÜ*ÞcHrÎí™î‹>챕ê§þ –ßsðž~JÌ PK  K™p0l¾2 ¯
 ¶ pmsp5440.clmUT ~¥W@Ux PK   G O PK "ÿØS‹y×7tæð?§µÀË1sîÜP¿¿–RÀ *Ìÿnç´Q
,ÞØæó&ˆ…2×B(€—î`Àêï¡.zŸ¡Ü»

I've omitted some space to make it fit, but it still comes out with garbage. When I open the original text files I actually have good data in them.

Thanks,

Chris
 
Ah, now I see what is happening. I had some problems with this with some log files too. Has to do with the encoding of the file.

Specifically it is the TRISTATE value that is messing you up. The following code will fix you up.


Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim FileObject
Dim SourceFolder
Dim FileCollection
Dim File
Dim Readfile
Dim ThisTxt
Dim WriteFile
Dim Filename
Dim Report
Dim Line

Set FileObject = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FileObject.GetFolder("C:\Test\")
Set FileCollection = SourceFolder.Files

Report = ""
for each File in FileCollection
FileName = "C:\Test\" & File.Name
'MsgBox FileName
Set Readfile = FileObject.OpenTextFile(FileName,ForReading,,-2)
ThisTxt = Split(ReadFile.ReadAll,vbNewLine)
For Each Line In ThisTxt
Report = Report & Line & vbCRLF
Next
Readfile.Close

Next

FileObject.CreateTextFile("c:\Test\pmsp0000.txt")
Set FileName = FileObject.GetFile("c:\Test\pmsp0000.txt")
Set WriteFile = FileName.OpenAsTextStream(ForWriting,True)
WriteFile.WriteLine(Report)
WriteFile.Close

Msgbox "Done adding carriage returns and combining files!"


I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Well, I agree with you on the encoding of the file, but I'm still having the same exact problem with the exact same output. I don't know what could possibly be my issue now.
 
Sounds like you need to check your source files, or whatever is making them.

The code I posted above was able to work on my system with some text files I created, and also by grabbing my secedit.log file.

The secedit.log gave me the same output as you report until I worked in the code for the Tristate.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Where I have -2 above you should try -1 and 0 to see if either fixes the problem for you.

Valid tristate values are -2, -1, 0.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hello camidon,

Have you tried my solution in my posting to your previous same thread?

regards - tsuji

ps Keep practically the same issue on the same thread.
 
Okay, both of you guys were correct all along. And I'm officially the biggest idiot in the world. I was pointing to the wrong source files and as a result was concantenating together a bunch of ZIP files!!!!

Thanks,

Chris
 
LOL!!!! :) Well I am glad you got it working and that the scripts provided could do the job for you.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top