Text Files

How can I read a list of text file names into another text file or into a string variable
[89 byte] By [Melanie] at [2007-11-15 17:36:18]
# 1 Re: Text Files
Dim strFile as string
Dim strFiles as string

strFile = Dir("c:\*.txt")
strFiles = ""
Do Until strFile = ""
strFiles = strFiles & strFile & vbcrlf
strFile = Dir
Loop

Msgbox strFile

Tom Cannaerts
slisse@planetinternet.be
Moderator on http://www.vbcodelibrary.co.uk/board

A bottomless pit, I'm sure it came with the place, who would dig one on purpose?
Cakkie at 2007-11-10 0:44:17 >
# 2 Re: Text Files
option Explicit
'
'How to
'read a list of text file names
'into another text file
'or into a string variable
'here is a way to do it: you need to put a commandbutton on a form,
'and nothing else...
'This example search only in C:\Temp for txt files
'and write the list of name founded in c:\temp\listOfFile.txt
'
private Sub Command1_Click()
'read list of filenames with txt extension
'from a directory of hard disk
Dim strFileName as string
Dim astrListFileName() as string
Dim lngCounter as Long
Dim intFreeFile as Integer
'tell user something is happening
Screen.MousePointer = vbHourglass

'initialize array
ReDim astrListFileName(50)
'initialize counter
lngCounter = -1

strFileName = Dir("c:\temp\*.txt")
Do While strFileName <> ""
lngCounter = lngCounter + 1
'verify you've not exceded the ubound of array
If lngCounter > UBound(astrListFileName) then
ReDim Preserve astrListFileName(lngCounter + 50)
End If
'store first name
astrListFileName(lngCounter) = strFileName
'read next file
strFileName = Dir()
Loop
If lngCounter > -1 then
'delete unused cells of array
ReDim Preserve astrListFileName(lngCounter)
'now you have all filenames in this array!
'write it all in a file:
'recombine it in a string variable
strFileName = Join(astrListFileName, vbCrLf)

intFreeFile = FreeFile
Open "c:\temp\listOfFile.txt" for Output as #intFreeFile
print #intFreeFile, strFileName
Close #intFreeFile
else
'no file found: erase
'array to free memory
Erase astrListFileName
'tell to user
MsgBox "No text file in C:\Temp"
End If
'tell him you've finished
Screen.MousePointer = vbDefault
End Sub
'

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make dev-archive a great place.
Come back soon, you Gurus.

The Rater
Cimperiali at 2007-11-10 0:45:17 >
# 3 Re: Text Files
As usual you 're quicker but this time you loose name of first file...
;-)

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make dev-archive a great place.
Come back soon, you Gurus.

The Rater
Cimperiali at 2007-11-10 0:46:18 >
# 4 Re: Text Files
The last line must be
Msgbox strFiles

Iouri Boutchkine
iouri@hotsheet.com
Iouri at 2007-11-10 0:47:18 >
# 5 Re: Text Files
Am I? Iouri pointed out a mistake (strFiles instead of strFile), but it should take all the files.

Tom Cannaerts
slisse@planetinternet.be
Moderator on http://www.vbcodelibrary.co.uk/board

A bottomless pit, I'm sure it came with the place, who would dig one on purpose?
Cakkie at 2007-11-10 0:48:17 >
# 6 Re: Text Files
;-)
Ok, you're right: did not saw *files and *file...
Thanks for light!
:-)
Have a nice day, you LampedOne!

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make dev-archive a great place.
Come back soon, you Gurus.

The Rater
Cimperiali at 2007-11-10 0:49:27 >