Subject: multiple textfiles to one textfile for import
Posted By: Marleen Post Date: 9/28/2006 5:21:00 AM
Hello,

I am searching for days now and still have a problem with some coding.

I build an Access application which makes .txt files of .port files. These .txt files need to be writen in 1 other .txt file, which will be imported in the Access application. The name of the .txt files are always different.

So I made this:

Function ImportPorB()

Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objImportB
Set objImportB = objFSO.CreateTextFile("V:\aa\bb\cc\ImportB.txt")

Dim objFile
Dim Src
Dim NewSrc
Dim Order
Dim ImportB

For Each objFile In objFSO.GetFolder("V:\aa\bb\cc\").Files
Order = Left(objFile.Name, 8 )
Src = "V:\aa\bb\cc\" & Order & ".por "
NewSrc = "V:\aa\bb\cc\" & Order & ".txt "

If Right(objFile.Name, 5) = "B.por" Then

Debug.Print Src
Debug.Print NewSrc

FileCopy Src, NewSrc

End If

Next
Set ImportB = objFSO.OpenTextfile("V:\aa\bb\cc\ImportB.txt")
NewSrc.write ImportB.ReadAll
ImportB.Close
NewSrc.Close

Set objFSO = Nothing

End Function

The first part is working fine! The code is making a ImportB.txt and make of all .por files a .txt file.

But he doesn't fill the ImportB.txt and gives a warning.

So I made this:

Open "V:\aa\bb\cc\ImportB.txt" For Input As #1
Open "V:\aa\bb\cc\" & Order & ".txt " For Output As #2
Write #1,
Close #1
Close #2

But still a warning.

So I tried this:
Dim nExportbestand As Integer
nExportbestand = FreeFile

Open "V:\aa\bb\cc\ImportB.txt" For Output As #nExportbestand

Print #nExportbestand, "bla bla"

Close #nExportbestand

Still a warning.

My last idea was this:
Set FS = CreateObject("Scripting.FileSystemObject")
Set a = FS.CreateTextFile("V:\aa\bb\cc\ImportB.txt")
a.Write (NewSrc)
a.Close

No warnings this time. But now he writes the pathname of the file in ImportB.txt instead of the text in the textfile.

What am I doing wrong?

Reply By: mmcdonal Reply Date: 9/28/2006 7:25:08 AM
Using text1.txt as the source file, and text2.txt as the target file:

Const ForReading = 1
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\text1.txt", ForReading)
strContents = objFile.ReadAll
objFile.Close

Set objFile = objFSO.OpenTextFile("C:\text2.txt", ForAppending)
objFile.Write strContents
objFile.Close

This opens the first file, reads all of its contents into the variable strContents and then closes the first file. It opens the second file, and appends strContents in line after the original contents of the second file.

Does this help?



mmcdonal
Reply By: Marleen Reply Date: 9/28/2006 7:49:29 AM
Well in the mean while I wrote this:

Set a = objFSO.CreateTextFile("V:\aa\bb\cc\ImportB.txt")
Set b = objFSO.OpenTextFile(NewSrc)
a.write b.ReadAll
a.Close

It is working, but he only writes 1 file into the ImportB.txt and not all files.

Reply By: mmcdonal Reply Date: 9/28/2006 8:02:55 AM
Take one file at a time, read it, and then append it, then go to the next file, read it, and then append it, etc.

There are more operations this way, but it will work.

If you must put all the data into one variable before appending, remember to do this:

strContent = strContents & objFile.ReadAll

Otherwise each new read wipes out the old one in the variable, and you only get one file appended. You may want to do this instead:

strContents = strContents & vbCrLf & objFile.ReadAll

This will make sure there is a line break between rows at the beginning and ends of each file.

Does this help?

mmcdonal
Reply By: mmcdonal Reply Date: 9/28/2006 8:09:39 AM
Sorry, a change:

This - strContent = strContents & objFile.ReadAll
Should be - strContents = strContents & objFile.ReadAll


mmcdonal
Reply By: Marleen Reply Date: 9/28/2006 9:03:40 AM
I got it up and running!!!
The createtextfile was in the loop. How could I've not seen it!!!!!



Function ImportPor()
         
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Dim a As Object
    Set a = objFSO.CreateTextFile("V:\aa\bbImportB.txt")
    Dim b
    
    Dim objFile
    Dim Src
    Dim NewSrc
    Dim Order
                                 
    For Each objFile In objFSO.GetFolder("V:\aa\bb\").Files
        Order = Left(objFile.Name, 8)
        Src = "V:\aa\bb\" & Order & ".por "
        NewSrc = "V:\aa\bb\" & Order & ".txt "
        
        If Right(objFile.Name, 5) = "B.por" Then
            
            Debug.Print Src
            Debug.Print NewSrc
            
            FileCopy Src, NewSrc
            
            Set b = objFSO.OpenTextFile(NewSrc)
            a.write b.ReadAll
            b.Close
        
        End If
         
    Next
    
    a.Close
                    
End Function



Go to topic 50308

Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156
Return to index page 155
Return to index page 154
Return to index page 153