-
I am using API to move hundreds of text files from 3 folders into a 4th folder. Problem is, some of these files have the same name. If they have the same name, I would like to join (append?) them together and keep the original name. Any help would be greatly appreciated.
-
Wouldn't it be more practical to add a number suffix if there are more than one file with the same name? Eg:
Myfile1.txt
Myfile2.txt
Is there a reason why you couldn't do it this way?
-
The files are generated by a progam. A file goes into this program and is split into 3 files and placed into 3 folders.
The program does it's thing and expires. I would like to archive the files by joining them back together and place these files in a seperate folder. This program will be run weekly, with the files going into a different folder (based on date) each time. It is necessary to keep the same file name.
-
Here We Go
Right. Two merge two files. You need to open the file as a text stream object for appending. Now open the other file for reading.
Now there are a couple of ways to do this depending on how big the files are. If they are quite small you can read the whole file and write it to the other file.
Code:
Dim fs
Dim myReadFile, myAppendFile
Set fs = CreateObject("Scripting.FileSystemObject")
Set myAppendFile = fs.OpenTextFile("c:\test\testfile.txt", ForAppending)
Set myReadFile = fs.OpenTextFile("c:\testfile.txt", ForReading)
strFileRead = myReadFile.ReadAll
myAppendFile.Write(strFileRead)
myAppendFile.Close
myReadFile.Close
Set fs = Nothing
If the files are quite large then you will have to read them a line at a time.
Code:
Dim fs
Dim myReadFile, myAppendFile
Set fs = CreateObject("Scripting.FileSystemObject")
Set myAppendFile = fs.OpenTextFile("c:\test\testfile.txt", ForAppending)
Set myReadFile = fs.OpenTextFile("c:\testfile.txt", ForReading)
While Not myReadFile.AtEndOfStream
strFileRead = myReadFile.ReadLine
myAppendFile.WriteLine (strFileRead)
Wend
myAppendFile.Close
myReadFile.Close
Set fs = Nothing
Now you can kill the read file and do whatever you want with the new file that has been appended to.:)
-
Thanks for the help! The only problem is that I don't know the names of the files. They are different every week. I need a way to check to see if the files are the same name in the 3 folders. If they are, append them and archive it into a 4th folder. Thanks for your help so far, I currently trying to use the code you gave me in a for loop.