Discussion Locked

Simple File Listing in Visual Basic

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am working on a project in my spare time... I have been out of VB for too long, and think someone out there can give me a hand, and help me speed up the re-learning process. :)
I need to be able to take a look at all files/folders within a certain directory on a server, and then compare that list with one taken from a local pc. How can I get a simple directory listing and transfer that data to a file, or a object type? Thanks in advance to anyone who can help me out!

5 replies

by peyison

The following code will list the files and subdirectories in a given directory:

Sub ListFiles(asPath as String)
Dim sFile As String

'- look for normal files and subdirectories
sFile = Dir(asPath, vbNormal + vbDirectory)
Do While Len(sFile) > 0
If sFile <> "." And sFile <> ".." Then
MsgBox sFile
End If
sFile = Dir
Loop

End Sub

e.g. : ListFiles "c:\windows"

To transfer the data to a file, there are several options. You can write the file and directory names to a text file using the Print command, or you can save to a database. If the local PC has access to the directory on the server, the ListFiles routine can be called from the PC on both its directory and on the server directory and the files can be kept in arrays in memory at which point the comparison can be done - thus eliminating the need to write the files at all.

Hope this helps.

by lab rat

Could you explain alittle more about the vbNormal and vbDirectory parts of the code? I still need to be able to identify what is a folder, and what is a file. How can I go about copying these files from the server to the client?

by lab rat

I think I'm on the right track. I've created a routine that checks wether it is a file or folder. Also figured out vbNormal, etc... Thx anyway. I still run into the question of copying a file/folder and preserving the long file names... Will be working on it anyhow. Will rate answers when I figure this out.

This discussion is closed to new replies.