Link to home
Start Free TrialLog in
Avatar of jaiswalpragya
jaiswalpragya

asked on

VBScript FileSystemObject for copyfile.

Hello experts,

I am using the following VBScript code in Visual Builder to find a file recursively in a folder and then copy it to a different location. I am unable to understand how I can check if a File is not found in thie piece of code.

Any ideas or suggestions will be appreciated.


'Search the main folder for a given file and copy from source to destination
Sub ScanFolders(objFolder,VarName)
      Set colFiles = objFolder.Files
      For Each objFile in colFiles
            if objFile.Name = VarName then
                FSO.CopyFile objFolder.Path & "\" & VarName, objDestFolder & "\" , true
                builder.LogMessage VarName & " copied from" & objFolder.Path & " to" & " - %DestinationDir%",true
          end if
      Next
      ShowSubfolders objFSO.GetFolder(objStartFolder),VarName
End Sub

'Search the subfloders recursively and copy the file from source to destination
Sub ShowSubFolders(Folder,VarName)
      'MsgBox "Folder being searched: " & Folder.Path & " File is: " & Varname
      For Each Subfolder in Folder.SubFolders
        Set objectFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objectFolder.Files
        For Each objFile in colFiles              
            if objFile.Name = VarName then
                     'MsgBox "File found:" & VarName
                     'MsgBox "Source is: " & Subfolder.Path & "\" & VarName
                'MsgBox "Dest is: " & objDestFolder & "\"
                  FSO.CopyFile Subfolder.Path & "\" & VarName, objDestFolder & "\", true       
                  builder.LogMessage VarName & " copied from - " & SubFolder.Path & " to" & " - %DestinationDir%",true
            end if
        Next
        ShowSubFolders Subfolder,VarName
      Next
      
End Sub

Thanks.

Avatar of JesterToo
JesterToo
Flag of United States of America image

Three questions and a couple of observations:

Q1.  Can the filename exist in more than one of these folders and what do you want to do if it does? (your code would continue thru all folders and perform the copy each time it finds the file leaving only the last one in the destination folder)

Q2.  If the filename is not found in any folder do you just want a simple msgbox stating that?

Q3.  Both of these subroutines can be combined into a single sub to simplify the script... do you want that?

Lynn
Avatar of jaiswalpragya
jaiswalpragya

ASKER

Thanks for the response.

1. The filenames exist in only one of the subfolders.
2.If the filename is not found i  will be logging message to the Visual builder, which you could say is like a MsgBox stating that.
3. Yes, a single subroutine would be a better option.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the script. But my script has to copy multiple files by looping. The files to be copied are retrieved from a database and then searched and copied. Here is my whole code:

builder.LogMessage "Total rows selected from PLTSBuilds.mdb : " & vbld_TempMacroObj("ADO_RS").Recordcount

'Set File system Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Define the Source and detsination directory
objStartFolder = "%SourceDir%"
objDestFolder = "%DestinationDir%"
Set objFolder = objFSO.GetFolder(objStartFolder)

'Move to the first record in the Temporary Recordset
vbld_TempMacroObj("ADO_RS").movefirst

'Loop through all the records in record set
Do until vbld_TempMacroObj("ADO_RS").eof
      
          ' Get the file name from
            fname = vbld_TempMacroObj("ADO_RS").Fields("FileName")
            n = Len(fname)
            
            'Get file name without any extension
            CropStr = Left(fname,n-4)
            
            'Process File name
             ScanFolders objFolder,fname      
            
            'Process PDB
            if vbld_TempMacroObj("ADO_RS").Fields("PDB") = True then
                  PdbStr = CropStr & ".pdb"
                  MsgBox PdbStr
                  ScanFolders objFolder,PdbStr
            end if
            
            'Process RegFile            
            if vbld_TempMacroObj("ADO_RS").Fields("RegFile") = True then
                  RegStr = CropStr & ".reg"
                  MsgBox RegStr
                  ScanFolders objFolder,RegStr
            end if
            
            'Process ResourceDLL
             if vbld_TempMacroObj("ADO_RS").Fields("ResourceDLL") = True then
                  ResStr = CropStr & ".RES"
                  MsgBox ResStr
                  ScanFolders objFolder,ResStr
            end if
            
             'Process OtherFiles
             if vbld_TempMacroObj("ADO_RS").Fields("OtherFiles") <> "" then
                    OFStr = vbld_TempMacroObj("ADO_RS").Fields("OtherFiles")
                   Dim i,a
                   'Define the delimiter
                   Seps = ","
                   'Call function to tokenize
                  a = Tokenize(OFStr,Seps)                   
                   For i=1 to UBound(a)
                         ScanFolders objFolder,a(i-1)
                   Next
            end if
                  
            vbld_TempMacroObj("ADO_RS").movenext
Loop

'Function to tokenize "OtherFiles"
Function Tokenize(byVal TokenString, byVal TokenSeparator)

      Dim NumWords, a()
      NumWords = 0
      
            
      Do
            Dim SepIndex, SepPosition
            SepPosition = 0
            SepIndex    = -1
            
                  ' Find location of separator in the string
                  Dim pos
                  pos = InStr(TokenString, TokenSeparator)
                  
                  ' Is the separator present, and is it closest to the beginning of the string?
                  If pos > 0 and ( (SepPosition = 0) or (pos < SepPosition) ) Then
                        SepPosition = pos
                        SepIndex    = i
                  End If
                  
            
            ' Did we find any separators?      
            If SepIndex < 0 Then

                  ' None found - so the token is the remaining string
                  redim preserve a(NumWords+1)
                  a(NumWords) = TokenString
                  
            Else

                  ' Found a token - pull out the substring            
                  Dim substr
                  substr = Trim(Left(TokenString, SepPosition-1))
      
                  ' Add the token to the list
                  redim preserve a(NumWords+1)
                  a(NumWords) = substr
            
                  ' Cutoff the token we just found
                  Dim TrimPosition
                  TrimPosition = SepPosition+Len(TokenSeparator)
                  TokenString = Trim(Mid(TokenString, TrimPosition))
                                    
            End If      
            
            NumWords = NumWords + 1
      loop while (SepIndex >= 0)
      
      Tokenize = a
      
End Function
                  
'Search the main folder for a given file and copy from source to destination
Sub ScanFolders(objFolder,VarName)
      Set colFiles = objFolder.Files
      For Each objFile in colFiles
            if objFile.Name = VarName then
                FSO.CopyFile objFolder.Path & "\" & VarName, objDestFolder & "\" , true
                builder.LogMessage VarName & " copied from" & objFolder.Path & " to" & " - %DestinationDir%",true
          end if
      Next
      ShowSubfolders objFSO.GetFolder(objStartFolder),VarName
End Sub

'Search the subfloders recursively and copy the file from source to destination
Sub ShowSubFolders(Folder,VarName)
      'MsgBox "Folder being searched: " & Folder.Path & " File is: " & Varname
      For Each Subfolder in Folder.SubFolders
        Set objectFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objectFolder.Files
        For Each objFile in colFiles              
            if objFile.Name = VarName then
                     'MsgBox "File found:" & VarName
                     'MsgBox "Source is: " & Subfolder.Path & "\" & VarName
                'MsgBox "Dest is: " & objDestFolder & "\"
                  FSO.CopyFile Subfolder.Path & "\" & VarName, objDestFolder & "\", true       
                  builder.LogMessage VarName & " copied from - " & SubFolder.Path & " to" & " - %DestinationDir%",true
            end if
        Next
        ShowSubFolders Subfolder,VarName
      Next
      
End Sub

In the script you gave me, it copies one file and then exits. Also, the FoundFile and StopNow conditions are set once a file is found.

Thanks.
Hey thanks, I changed it accordingly. Its working fine.

I'm glad you were able to use it.  

Had you stated in your original question that which you explained in your recent comment I would have presented a solution you didn't need to modify :)  Only 3 or 4 minor changes would be necessary to accomodate that.

Thanks for the grade!

Regards,
Lynn
Thanks!