|
|
Subject:
Search local hard drive for all the copies of the single file, VB6
Category: Computers > Programming Asked by: rbrian2-ga List Price: $20.00 |
Posted:
16 Dec 2002 08:21 PST
Expires: 15 Jan 2003 08:21 PST Question ID: 125376 |
Hi, I have a code, which is searching through all hard drives for the specific file. It has only two functions. Function SearchFileRecursivw has a place where I specify the path for my file under comment "Main Loop". My problem is: it is going to be two files with the same name on my hard drive. 1. One file (current) always will be huge (up to 1 GB) and will be under this directory: C:\Project 1.1\Backups\db1.mdb (the size is up to 1 GB) 2. Second file (new and empty) will be under a different directory: C:\Project 1.2\db1.mdb (only 200 KB) My code is looking only for a file : db1.mdb ONCE. All I need is : to modify a code a little bit to find both files, after I found them both, I will do the rest. But my problem I can not figure out how to search for the same name of the file twice (the only difference is the size of the files and directory). Notice, folder Project XX version will be changed every time new version gets installed. Here the code: Private Sub Command1_Click() Dim sAllDrives As String Dim sCurDrive As String Dim sSearchFileName As String Dim iPos As Integer 'Set search file name. sSearchFileName = "db1.mdb" 'Enumerate drives. Call SearchDrives(sSearchFileName) If sDBNewPath <> vbNullString Then MsgBox "New location: " & sDBNewPath & vbCrLf & "Backup location: " & sDBBackupPath Else MsgBox "Not found" End If End Sub Private Sub SearchDrives(ByVal sFileName As String) Dim sAllDrives As String Dim sCurDrive As String Dim sSearchFileName As String Dim iPos As Integer 'Get drives string. sAllDrives = GetDriveString() 'Enumerate drives. Do Until sAllDrives = Chr(0) 'Get drive name. iPos = InStr(sAllDrives, Chr(0)) sCurDrive = StripNull(Mid(sAllDrives, 1, iPos)) If iPos <> 0 Then sAllDrives = Mid(sAllDrives, iPos + 1, Len(sAllDrives)) End If 'Get drive type. Select Case GetDriveType(sCurDrive) Case DRIVE_REMOVABLE: 'Do not make a search. Case DRIVE_FIXED: 'Make a search. Call SearchFileRecursive(sCurDrive, sFileName) Case DRIVE_REMOTE: 'Do not make a search. Case DRIVE_CDROM: 'Do not make a search. Case DRIVE_RAMDISK: 'Do not make a search. End Select 'If file found. If sDBNewPath <> vbNullString Then Exit Do End If Loop End Sub Private Function GetDriveString() As String Dim sBuffer As String 'Enumerate 30 drives, three characters each, plus trailing null. sBuffer = Space(NUM_DRIVES * 4) If GetLogicalDriveStrings(Len(sBuffer), sBuffer) Then GetDriveString = Trim(sBuffer) End If End Function Private Function SearchFileRecursive(ByVal sPath As String, ByVal sFileName As String) As Boolean Dim fData As WIN32_FIND_DATA Dim lFind As Long Dim lContinue As Long Dim sCurFile As String Dim sCurPath As String 'Check if file already found. If sDBNewPath <> vbNullString Then SearchFileRecursive = True Exit Function End If 'Add slash if needed to make sub-directory search. If Right(sPath, 1) <> "\" Then sPath = sPath & "\" End If 'Find first instance. lFind = FindFirstFile(sPath & "*.*", fData) If lFind = INVALID_HANDLE_VALUE Then lFind = 0 SearchFileRecursive = False Exit Function End If 'Main loop. Do sCurFile = Left(fData.cFileName, InStr(fData.cFileName, Chr(0))) If fData.dwFileAttributes And vbDirectory And StripNull(sCurFile) <> "." And StripNull(sCurFile) <> ".." Then 'Directory. sCurPath = SearchFileRecursive(Left(sPath & sCurFile, Len(sPath & sCurFile) - 1), sFileName) Else 'File. 'Here it should meet the requirements: 1) look up for the backup folder '2) look for the newly installed folder with empty database If UCase(StripNull(sCurFile)) = UCase(sFileName) Then sDBBackupPath = sPath & StripNull(sCurFile) sDBNewPath = sPath & StripNull(sCurFile) SearchFileRecursive = True Exit Do End If End If lContinue = FindNextFile(lFind, fData) DoEvents Loop Until lContinue = 0 Call FindClose(lFind) End Function Any help with a CODE would be greatly appreciated. Brian |
|
Subject:
Re: Search local hard drive for all the copies of the single file, VB6
Answered By: theta-ga on 16 Dec 2002 12:09 PST Rated: |
Hi rbrian2-ga, Here is what I understand of how your code is working : You have two global variables sDBNewPath and sDBBackupPath which are supposed to store the new path and the backup path that you have found. The solution to your problem is to not stop searching when you find the first file, but instead, to continue searching until you find the second file or you run out of folders/files to scan. The simplest way to implement this is to add another global variable nFilesFound, which will store the number of files you have found. So, you should stop searching when it contains the value 2. The following code stores the path to the two databases in the sDBBackupPath and the sDBNewPath variables. This means that you should replace your search successful checks, which are currently this : If sDBNewPath <> vbNullString Then .... End If with the following code : if nFilesFound <2 then .... end if ======================================== You can declare the variable globally as : Dim nFilesFound as Integer ======================================== Then in your SearchDrives function, initialize nFilesFound to 0. Private Sub SearchDrives(ByVal sFileName As String) ... ... 'Get drives string. ... 'Enumerate drives. ... 'Get drive type. 'ADDED : Initialize nFilesFound to 0 ' and the path variables to empty strings nFilesFound = 0 sDBNewPath = "" sDBBackupPath = "" Select Case GetDriveType(sCurDrive) ... ... End Select 'MODIFIED: If 2 files were found, nFileFound will be 2 if nFilesFound >=2 then Exit Do End If Loop End Sub ============================================================ Now lets modify the SearchFileRecursive function. You seem to be doing a very weird thing in this function. Whenever you find a valid file, you add its path to BOTH the sDBNewPath AND the sDBBackupPath. My guess is that when you find a valid file, you will want to call a function to determine whether it is a backup file or a new file, and then assign its path to the relevant variable. So, in my code, I assume that you have a function called IsBackup()defined, which returns true if the file is a backup DB and false if it is a new DB. If you didnt want this, you can just modify the code. Private Function SearchFileRecursive(ByVal sPath As String, ByVal sFileName As String) As Boolean ..... ..... 'MODIFIED : Check if file already found. if nFilesFound >=2 then SearchFileRecursive = True Exit Function End If 'Add slash if needed to make sub-directory search. ..... 'Find first instance. .... 'Main loop. Do If ... Then 'Directory. ... Else 'File. If UCase(StripNull(sCurFile)) = UCase(sFileName) Then 'ADDED: Check if it is backup file 'function IsBackup takes the file path & name as argument If isBackup(sPath & sFileName) = True then 'it is a backup file, assign path to backup variable sDBBackupPath = sPath & StripNull(sCurFile) Else ' It is a new file, assign to sDBNewPath variable sDBNewPath = sPath & StripNull(sCurFile) End if ' ADDED : Increment file found counter nFilesFound = nFilesFound + 1 SearchFileRecursive = True 'MODIFIED: We will exit only if 2 files have been found 'otherwise, keep on searching until lcontinue=0 If nFilesFound >=2 Then Exit Do End if End If End If .... Loop Until lContinue = 0 Call FindClose(lFind) End Function ================================================ That should do it. :-D Be warned though : Since you have specifically stated that you are searching for just 2 files, thats what the code does. But you should check for conditions such as when two backup files are found, but no new files(if the user has multiple installations of your program) or vice-versa. You can easily change the code to meet this challenge. Also, my modified function calls the IsBackup() function, which takes the full path to a file and returns whether it is a backup database or not. Of course, there is no such function. But we need to determine which variable to assign the found file path to. So you can either implement this function, or change the code above so that it doesn't call this function. ================================= Hope this helped. If you need any clarifications, just ask! I will be glad to help. Regards, Theta-ga :) |
rbrian2-ga
rated this answer:
and gave an additional tip of:
$10.00
Thanks Theta-ga. You've done a great job. Code works perfectly! Thanks again. |
|
Subject:
Re: Search local hard drive for all the copies of the single file, VB6
From: arcadesdude-ga on 16 Dec 2002 09:05 PST |
While I can't help you with the VB6 code I can offer some algorithm advice. Modify your code so that: 1. You set an initial variable flag (booleen) which will be the found state of the first database file. (If zero not found, if one found). 2. After you have found the file check the flag to see if you already found the file with this name. 2.1 If it is zero then you need to save the path where that file is and set the flag to one then reloop one more time to look for the file name again (but do not start searching from the root/begining just continue the search past the current point). (If you get to the end of the files and still it is not found, report as not found for both files.) 2.2 If it is one in this loop that means you have found the file name again so compare the saved path with this one. If the paths are equal continue onward and search some more for the other file. If the paths are different you have found your other (2nd) file. (If you get to the end of the files and still it is not found, report as not found for one file.) |
Subject:
Re: Search local hard drive for all the copies of the single file, VB6
From: rbrian2-ga on 16 Dec 2002 12:32 PST |
Hi Theta-ga, I want to say thank you for all your hard work. I've been steering into this code for a while and could not figure out what is the solution. I will give a try during today for your solution (it is what I want, I mean - condition for finding files) and come back to you today or tomorrow before I will close the question and definitely rate your answer! Brian |
Subject:
Re: Search local hard drive for all the copies of the single file, VB6
From: rbrian2-ga on 17 Dec 2002 10:38 PST |
Hi arcadesdude-ga, Thanks for your advise, but I was looking for some coding examples of the logic. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |