• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

VB Stuff



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


...will circle this city
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?


...will circle this city
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...

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 & "\"
    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)
        If Right$(Dir1.Path, 1) = "\" Then
            sCurrPath = Left(Dir1.Path, Len(Dir1.Path) - 1)
            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
        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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
What a long strange trip it's been. =)

Forum statistics

Latest member