VB Stuff

Discussion in 'Web Design & Coding' started by anewhope, Oct 22, 2002.

  1. anewhope

    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
     
  2. Piett

    Piett ...will circle this city

    Messages:
    528
    Location:
    Calgary, Alberta
    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?
     
  3. anewhope

    anewhope Guest

    Specifying the directory and then sub dir's off that...
     
  4. Piett

    Piett ...will circle this city

    Messages:
    528
    Location:
    Calgary, Alberta
    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
     
  5. a_scoundral

    a_scoundral Guest

    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
     
  6. dave holbon

    dave holbon Moderator

    Messages:
    1,014
    Location:
    London England
    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.
     
  7. JZolloXP

    JZolloXP Guest

    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()