TCrow
09-01-2000, 02:35 PM
I would like to design a program in VB that would confirm a couple of files on a cd. The extensions of these files are more important then the file names. There could be many folders as well. I need to go into each folder and confirm that there is a least one file with the extension of qiz. and at least one file with the extension of vol. Is there a way? I am not sure what the code would look like for this question since I am fairly new. Thanks in advance for your help.
Kwalude
09-01-2000, 03:54 PM
You are going to have to use some combination of the DirListBox and the FileListBox. Use the DirListBox to navigate to the drive or directory, and to move between any and all sub folders. And use the Filter property on the FileListBox.
BillSoo
09-01-2000, 05:23 PM
Here's an example of a program that searches for mp3s
Private Sub Command1_Click()
GetFilesFromDir "c:""
End Sub
Sub GetFilesFromDir(ByVal pth$)
Dim s$
Dim dirarray() As String
Dim n%, i%
s$ = Dir$(pth$ & "*.mp3")
While Len(s$)
Text1 = Text1 & vbCrLf & pth$ & s$
s$ = Dir$
Wend
s$ = Dir$(pth$ & "*.", vbDirectory)
n% = 0
While Len(s$)
If (s$ <> ".") And (s$ <> "..") Then
If Right$(s$, 1) <> """ Then s$ = s$ & """
ReDim Preserve dirarray(n%) As String
Debug.Print pth$ & s$
dirarray(n%) = pth$ & s$
n% = n% + 1
End If
s$ = Dir$
Wend
For i% = 0 To n% - 1
GetFilesFromDir dirarray(i%)
Next i%
End Sub
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
TCrow
09-02-2000, 06:15 PM
I appreciate the response but I can't seem to get the piece of code figured out. Could someone be so kind to comment this piece of code or provide further help to solve my original question. Thanks!
BillSoo
09-05-2000, 01:52 PM
Basically, this program uses the concept of *recursion* to perform the task.
The function lists all MP3 files in the directory and prepares a list of all subdirectories below the current one. If subdirectories exist, then it calls itself and passes the name of each subdirectory.
The subdirectories are stored in an array, rather than being searched immediately, because the DIR$ command is non reentrant (ie. it does not support multiple copies of itself simultaneously). Storing the subdirectories lets us finish using the DIR$ command in one function call before using it in another.
So anyways:
Sub GetFilesFromDir(ByVal pth$)
'gets all MP3 files from pth$ or in subdirectories of pth$
'pth$ is a full subdirectory name ending in """
'assumes that there is a textbox control called Text1 on
'the form somewhere
Dim s$
Dim dirarray() As String
Dim n%, i%
s$ = Dir$(pth$ & "*.mp3")
'the DIR$ command returns the first file that matches
'the criteria. If no criteria is given, then it matches
'the NEXT file based on the PREVIOUS criteria. If it
'runs out of files, then it returns a Null string
While Len(s$) 'as long as files are being found...
Text1 = Text1 & vbCrLf & pth$ & s$ 'add to textbox
s$ = Dir$ 'find the next file
Wend
'now that all MP3 files from the current directory have
'been found, it's time to search all subdirectories
s$ = Dir$(pth$ & "*.", vbDirectory)
'again we use the DIR command but this time we specify that
'it's to look for subdirectories only.
n% = 0
While Len(s$)
If (s$ <> ".") And (s$ <> "..") Then
'we don't want the . or .. subdirectory
If Right$(s$, 1) <> """ Then s$ = s$ & """
ReDim Preserve dirarray(n%) As String
'the preserve keyword allows us to increase the size of
'the array while keeping all previous entries
dirarray(n%) = pth$ & s$
'the subdirectory name is stored in the array
n% = n% + 1
End If
s$ = Dir$ 'get next directory name
Wend
'n% is number of subdirectories found
For i% = 0 To n% - 1
GetFilesFromDir dirarray(i%)
'call function again to search each subdirectory
Next i%
End Sub
You will have to modify this code to do the task you want. Specifically, you will search for *.vol and *.qiz files instead of *.mp3 files. After you find one (if you find one), you'd set some kind of state flag. Probably a global flag.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
NoahBody
09-05-2000, 04:32 PM
An alternative would be to go into a DOS Shell and execute:
(Say the CD is on Drive D:)
Dir D:*.qiz,*.vol /S >FileList.txt
Then all you'd have to do is parse the file FileList.txt.
Hope This Helps,
->Noah
BillSoo
09-05-2000, 05:30 PM
Noah: Your solution would find all instances of the specified files but if I understand the original post correctly, he is more interested in finding if directories exist that *do not* have those files. I could be wrong of course...
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
NoahBody
09-06-2000, 10:32 AM
In that case all he'd have to do is:
Dir D:*.,D:*.qiz,D:*.vol /S >File.lst
This would include all the directory names as well.
->Noah
Eagle
09-06-2000, 11:30 AM
I'm not sure this is what your asking for, but did you know there is a FileExist, and FolderExist fuction to test the existance of a file. Look it up in the MSDN library. It involves working with FileSystemObjects though