PDA

View Full Version : [SOLVED] When using open text file how to tell if the file existed, or a file was created?



Zuccj
08-01-2017, 09:54 AM
I'm working on a quick script that will export some data from excel into a text file. I have it all working, but I would like to be able to tell if the file already existed, or if it had to be created. The reason for this is if the file I was created I would like to add a header to the text file, so each column is able to be read quickly. If the file already existed, then there will be a header and no need for the header to be applied.


Dim fs, f
Set fs = CreateObject("scripting.FileSystemObject")
Set f = fs.opentextfile("C:\..." & part & ".txt", 8, True, TristateUseDefault))
f.write ....
f.writeline
f.Close

part is the part number which is taken from a cell, which is also the name of the text file.

mdmackillop
08-01-2017, 10:25 AM
If Len(Dir("C:\..." & part & ".txt")) > 0 Then
'Proceed
Else
'Add headers
End If

Zuccj
08-01-2017, 12:04 PM
Yup figured it out right after posting. Original thought was trying to see if I could get a returned value from opentextfile, but just did what you posted.