View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Shailesh Shah[_2_] Shailesh Shah[_2_] is offline
external usenet poster
 
Posts: 114
Default sub directories again

Hi Martyn,

Try this,

Sub ListFileName()

Dim filearray()
Dim MyPath, MyName, MyCond
Dim j As Long, a As Long

MyPath = "c:\windows\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
MyCond = ".txt"
Do While MyName < "" ' Start the loop.
If (GetAttr(MyPath & MyName) And vbDirectory) < vbDirectory Then
If UCase(Right(MyName), 4) = UCase(MyCond) Then
a = a + 1
ReDim Preserve filearray(a)
filearray(a) = MyName
End If
End If
MyName = Dir ' Get next entry.
Loop

Open "c:\filearray.txt" For Output As #1
For j = 1 To a
Write #1, filearray(j)
Next
Close #1
End Sub


You can also run in dos mode, DOS Internal command DIR :
e.g.

Dir c:\windows\*.txt c:\filelist.txt

then you can view filelist.txt as under (in dos mode):
Type c:\filelist.txt


But with VBA you can adopt two methods below:

Sub BatchFile()
'To create batchfile that will run from Shell as we cann't use Shell
"C:\windows\*.txt C:\filelist.txt"

Dim BatchFilename, OutputFilename, MyCond

BatchFilename = "c:\dirlist.bat" 'Batch Filename
OutputFilename = "c:\filelist.txt"

' Find the xls files starting with "s"
MyCond = "dir c:\s*.xls /s " ' /S = search in all sub dir also

Open BatchFilename For Output As #1 ' Open file.
Print #1, MyCond & OutputFilename ' Write string to file.
Close #1 'close

Shell BatchFilename, vbNormalFocus 'Run batch File

End Sub

Sub DosCommand()

Dim OutputFilename, MyCond, Mac

OutputFilename = "C:\filelist.txt" ' output filename
' Find the xls files starting with "s"
MyCond = "dir C:\s*.xls /S" ' /S = search in all sub dir also

On Error Resume Next
Kill FileName ' if exist Kill. To append data remove or comment.
On Error GoTo 0

Mac = Shell(Environ$("comspec") & " /c " & MyCond & " " &
OutputFilename, 1)

End Sub



Regards,
Shah Shailesh
http://members.lycos.co.uk/shahweb/


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!