troubleshooting Question

VBScript FileSystemObject for copyfile.

Avatar of jaiswalpragya
jaiswalpragya asked on
ProgrammingVB Script
7 Comments1 Solution1676 ViewsLast Modified:
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.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros