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]

# 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
# 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
# 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