none
VBS Problem RRS feed

  • Question

  • My VBS problem is the script I am using functions, except when it encounters a subfolder that contains only another subfolder. It stops the search there and goes back to the source folder and tries the next folder in line. The files I am searching for are sometimes 3 directories into one folder. Is there a way to make my recursive search push though these folders?

    Set oFSO = CreateObject("Scripting.Filesystemobject")
    Source = "X:\XXXXXX"
    TARGET = "W:\XXXX\"
    SearchFolder oFSO.GetFolder(Source)

    Sub SearchFolder(oFolder)
        For Each oFile In oFolder.Files
    If (InStr(1, oFile.Name,"XXX",vbTextCompare) = 1) And _
    DateDiff("d", oFile.DateLastModified, Now) < XXX  Then    
    OFile.Copy (TARGET)
       
        
        For Each oSubfolder In oFolder.SubFolders
            SearchFolder oSubfolder
        Next
    End If
    Next
    End sub

    Monday, December 8, 2014 10:05 PM

Answers

All replies

  • Why script a recursive file copy when you can just use robocopy?


    robocopy X:\source w:\target ...

    See the robocopy documentation for how to use it.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Amber Rees Tuesday, December 9, 2014 3:56 PM
    Monday, December 8, 2014 10:47 PM
    Moderator
  • This same scrip keeps popping up but with different errors:

    Set oFSO = CreateObject("Scripting.Filesystemobject")
    Source = "X:\XXXXXX"
    TARGET = "W:\XXXX\"
    SearchFolder oFSO.GetFolder(Source)
    
    Sub SearchFolder(oFolder)
        For Each oFile In oFolder.Files
              If (InStr(1, oFile.Name,"XXX",vbTextCompare) = 1 And DateDiff("d", oFile.DateLastModified, Now) < 10  Then    
                  oFile.Copy TARGET
              End If
         
              For Each oSubfolder In oFolder.SubFolders
                 SearchFolder oSubfolder
              Next
        Next
    End sub


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, December 8, 2014 11:16 PM
    Monday, December 8, 2014 11:15 PM
  • You have one of your "next" statements in the wrong place, probably due to inconsistent code indentation. The script interpreter does not care about indentation but to humans it is a great visual help! Here is a generic recursive script that will do the trick:

    Set oFSO = CreateObject("Scripting.Filesystemobject")
    sFolder = "d:\Temp"
    SearchFolder oFSO.GetFolder(sFolder)

    Sub SearchFolder(oFolder)
        For Each oFile In oFolder.Files
            WScript.Echo oFile.Path
        Next
        
        For Each oSubfolder In oFolder.SubFolders
            SearchFolder oSubfolder
        Next
    End Sub

    Monday, December 8, 2014 11:15 PM
  •  I suspect a junior highschool class learning how to program from a tescher tha tis clueless.


    ¯\_(ツ)_/¯

    Monday, December 8, 2014 11:17 PM
  • Thank you so much. I really like that you game me a hyperlink with the information i needed so i was able to work this from a different angle. You are the best :)

    Tuesday, December 9, 2014 3:58 PM