Writing a Text File

04-03-2002, 10:19 AM
Hi all, is there a way to write all of the files within a directory onto a text file using vb code? please help :)


04-03-2002, 10:34 AM
Do the filenames have consistant titles, like "File0001.txt", "File0002.txt", etc? Then you could just loop and check if the file exists. If it does, then open the file and write to it.

04-03-2002, 10:51 AM
Use the Dir function to get all files in a directory (see the help for an example). Use the VB file routines (open, line input, print) to read and write the files.

04-03-2002, 11:28 AM
If the object is to write the contents of the files into a single file, then do as per ArnoutV; if the object is to save the file names only, then instead of opening the files, reading & copying them line by line, just write the file name to the text file.

04-03-2002, 12:14 PM
The Dir$() function can be a hard nut to crack. You start by calling it with all the parameters, to basically 'tell' it what you want it to retrieve. On all subsequent calls, you leave out the parameters and it simply repeats whatever action you did last. Your code might end up something like this:

Dim sFile As String 'Filename string variable
Dim sPath As String 'Directory string variable

sPath = "C:\"

sFile = Dir$(sPath, vbNormal) 'Look for the first file
Do Until sFile = ""
'Do stuff with the sFile here, such as:
Open sPath & sFile For Input As #1
'Read stuff from file
Close #1

Debug.Print sPath & sFile 'Print the filename to the debug window

sFile = Dir$() 'Grab next file
Loop 'Repeat until no more files remain

04-03-2002, 03:15 PM
Thank you very much everyone!!!!

04-03-2002, 07:36 PM
You could also cheat and just shell a batch file....

Shell "SaveDir.Bat"

And then write the batch file like:

Dir *.* > dir.txt

04-03-2002, 09:09 PM
first go to references and make sure "microsoft scripting runtime" is selected

then add this to the general setion:

Dim fsys As New FileSystemObject


dim FileNum as integer
dim thisfolder as folder
dim thisfile as file
dim thesefiles as files
set thisfolder = fsys.getfolder(<folderpath>)
set thesefiles = thisfolder.files
FileNum = FreeFile
open <Filename> for ouput as #FileNum
for each thisfile in thesefiles
write #FileNum, thisfile.name
close #FileNum

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum