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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output to Text File

Status
Not open for further replies.

arbytech

MIS
Feb 10, 2004
92
US
Hi everyone,

I've inherited some old VB6 code that needs an update (hopefully simple!). Please bear in mind that I don't have a VB background.

I needed the program to output a text file based on some variables the user inputs. Can this be done with certain formating (i.e. inserting certain constant charaters after certain variables)?

Also can this be done with creating a unique name for the text file everytime (i.e. appending a number to the name)?

Thanks for your help!!!
 
To do all of these, you can use the File System Object. To use the FSO, go to Project->References and select "Microsoft Scripting Runtime". Then:

Dim fso As New Scripting.FileSystemObject
Dim fld As Folder
Dim f
Dim FilePath As String
Dim FileOutput As String
Dim FileName As String
Dim ff As Integer
Dim NumFiles As Long

'put in the path to the directory where the
'files are located
FilePath = "C:\path\to\your\files\"

'Retrieve folder
Set fld = fso.GetFolder(FilePath)

NumFiles = 0

'Get the number of files in the folder
For Each f in fld.Files
NumFiles = NumFiles + 1
Next f

'Name new file with number one greater than number of
'files in the folder
FileName = "MyTextFile" & NumFiles + 1 & ".txt"

'Build string to write to text file
'I'm not sure exactly what you need to put here
'So this is very generic
FileOutput = SomeVar1 & SomeChar1 & SomeVar2 & SomeChar2

'Get a free file handle
ff = FreeFile

'Open a new text file for output
Open FilePath & FileName For Output As #ff

'Write your output to the file just opened
Print #ff, FileOutput

'Close the file
Close #ff

Let me know if you need any help building the output string, or have any other questions.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top