VB Stuff

A

anewhope

Guest
What im looking to do in a VB program is search for a set given file that I would declare, then if found copy it to a directory.

How would I go about this?

Say if I wanted to back up winini.bat to a floppy drive or something like that?

Anyhelp greatly appreciated
 
are you saying search and entire HD for it? because that would take some coding, big time. or do you just mean supplying a directory (without sub-dirs) and checking if it is there?
 
Specifying the directory and then sub dir's off that...
 
hmm i think that i could get it to search in the dir, but not the sub dir. that's a maybe though. haven't done any major vb in like 2 yrs
 
The answer you seek is simple.
Click project -> referneces -> then add in Microsoft Scripting Runtime. File name scrrun.dll

Next add some code

dim fso as FileSystem Object

Set fso = New FileSystemObject
if fso.FileExists(QualifiedSrcFileAndPath) = true then
fso.CopyFile QualifiedSrcFileAndPath, QualifiedDestFileAndPath, True
end if
'The True is to force an overwrite if the file already exists in the destination directory.


Set fso = Nothing
 
There are so many ways to do this it might be best if you stated what the operating system/s you wanted to achieve this under was: as you could use: -

Scripting, API calls, VB functions and a host of other methods or add-ins including batch file processing.
 
Here's code that will recusively search a directory...


Code:
Sub RecursiveSearch(ByVal sPattern As String, ByVal CurrDir As String, sFound() As String)
Dim i As Integer
Dim sCurrPath As String
Dim sFile As String
Dim ii As Integer
Dim iFiles As Integer
Dim iLen As Integer

If Right$(CurrDir, 1) <> "\" Then
    Dir1.Path = CurrDir & "\"
Else
    Dir1.Path = CurrDir
End If
'keep on going into the directories until there are no more left
For i = 0 To Dir1.ListCount
    If Dir1.List(i) <> "" Then
        DoEvents 'make sure it doesn't crash
        Call RecursiveSearch(sPattern, Dir1.List(i), sFound)
    Else
        If Right$(Dir1.Path, 1) = "\" Then
            sCurrPath = Left(Dir1.Path, Len(Dir1.Path) - 1)
        Else
            sCurrPath = Dir1.Path
        End If
	'copy path to string
        File1.Path = sCurrPath
	'copy pattern data to string
        File1.Pattern = sPattern
        If File1.ListCount > 0 Then 'matching files found in the directory
            For ii = 0 To File1.ListCount - 1
		'resize array for a new file
                ReDim Preserve sFound(UBound(sFound) + 1)
                sFound(UBound(sFound) - 1) = File1.List(ii)
            Next ii
        End If
        iLen = Len(Dir1.Path)
        Do While Mid(Dir1.Path, iLen, 1) <> "\"
            iLen = iLen - 1
        Loop
        Dir1.Path = Mid(Dir1.Path, 1, iLen)
    End If
Next i
End Sub

You must have two controls, a directory control named Dir1 and a FileList control named File1. Call it in this format...

Call RecursiveSearch("file.ext", "C:/", fileArray())

That will search for file named "file.ext", in the Entire C:/ drive, and output all matches into an array called fileArray()
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back