none
!Scripting help .VBS RRS feed

  • Question

  • I need help making this script search in the sub folders as well as the source.

    Dim SOURCE, TARGET, fso, SourceObj, TargetObj, FileObj
    SOURCE = "C:\Users\1385741622A\Desktop\Current 68's"
    TARGET = "C:\Users\1385741622A\Desktop\Due 68's\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set SourceObj = fso.GetFolder(SOURCE)
    For Each fileObj in SourceObj.Files
        If (InStr(1, FileObj.Name,"AF68-",vbTextCompare) = 1) And _
                (DateDiff("d", fileObj.DateLastModified, Now) > 10) Then
            fileObj.Copy (TARGET)
        End If
    Next

    Thursday, December 4, 2014 10:45 PM

Answers

  • I need help making this script search in the sub folders as well as the source.

    Here is a generic script to perform a recursive process:

    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

    • Proposed as answer by jrv Friday, December 5, 2014 12:42 AM
    • Marked as answer by LonePenguin Friday, December 5, 2014 11:31 PM
    Thursday, December 4, 2014 11:56 PM

All replies

  • I need help making this script search in the sub folders as well as the source.

    Here is a generic script to perform a recursive process:

    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

    • Proposed as answer by jrv Friday, December 5, 2014 12:42 AM
    • Marked as answer by LonePenguin Friday, December 5, 2014 11:31 PM
    Thursday, December 4, 2014 11:56 PM
  • When I try and add my search parameters I just get errors even though they work in the original script. I need it to search and copy any Files with the naming convention that begins with AF68- and is older than 10days and move it to the due folder. I have tried everything I can think of I just don't know why it won't work.

    Friday, December 5, 2014 10:28 PM
  • When I try and add my search parameters I just get errors even though they work in the original script. I need it to search and copy any Files with the naming convention that begins with AF68- and is older than 10days and move it to the due folder. I have tried everything I can think of I just don't know why it won't work.

    Your original question was: How to make your script search in subfolders. You now appear to have changed direction, mentioning some errors without posting any details. If you need help with these errors then you should start a new thread, post your script and report the errors plus the code that generates them.
    Friday, December 5, 2014 10:37 PM
  • Well here is the code that i am trying to use and i keep getting multiple errors so i will just start with the first one, even if i include the required object (FileObj) I then get a error stating Expected end of statement. I got these error when I tried using the recursive script posted earlier I am still having the same problem of neither script searching and coping the source folder and Subfolders.

    --------------Start--------------------

        

    Dim SOURCE, TARGET, fso, SourceObj, TargetObj, FileObj, oFolder, oSubFolder
    SOURCE = "C:\Users\1385741622A\Desktop\Current 68's"
    TARGET = "C:\Users\1385741622A\Desktop\Due 68's\"
    Set oFSO = CreateObject("Scripting.Filesystemobject")
    SearchFolder oFSO.GetFolder(source)

    Sub SearchFolder(oFolder)
        For Each oFile In oFolder.Files
            FileObj (InStr(1, FileObj.Name,"AF68-",vbTextCompare) = 1) And _
                (DateDiff("d", fileObj.DateLastModified, Now) > 10) Then
            fileObj.Copy (TARGET)

        For Each oSubfolder In oFolder.SubFolders
            SearchFolder oSubfolder
        Next
    End If
    Next
    End Sub

    -------End--------------------------------

    Friday, December 5, 2014 10:54 PM
  • Well here is the code that i am trying to use and i keep getting multiple errors so i will just start with the first one, even if i include the required object (FileObj) I then get a error stating Expected end of statement. I got these error when I tried using the recursive script posted earlier I am still having the same problem of neither script searching and coping the source folder and Subfolders.

    --------------Start--------------------

        

    Dim SOURCE, TARGET, fso, SourceObj, TargetObj, FileObj, oFolder, oSubFolder
    SOURCE = "C:\Users\1385741622A\Desktop\Current 68's"
    TARGET = "C:\Users\1385741622A\Desktop\Due 68's\"
    Set oFSO = CreateObject("Scripting.Filesystemobject")
    SearchFolder oFSO.GetFolder(source)

    Sub SearchFolder(oFolder)
        For Each oFile In oFolder.Files
            FileObj (InStr(1, FileObj.Name,"AF68-",vbTextCompare) = 1) And _
                (DateDiff("d", fileObj.DateLastModified, Now) > 10) Then
            fileObj.Copy (TARGET)

        For Each oSubfolder In oFolder.SubFolders
            SearchFolder oSubfolder
        Next
    End If
    Next
    End Sub

    -------End--------------------------------

    Your script fails because your seem to have distributed various keywords randomly in your code. I have bolded them in the block above. The code below is syntactically correct but I did not go to the trouble of setting up a test bed in order to fully test it.

    Dim SOURCE, TARGET, fso, SourceObj, TargetObj, FileObj, oFolder, oSubFolder
    SOURCE = "C:\Users\1385741622A\Desktop\Current 68's"
    TARGET = "C:\Users\1385741622A\Desktop\Due 68's\"
    Set oFSO = CreateObject("Scripting.Filesystemobject")
    SearchFolder oFSO.GetFolder(source)

    Sub SearchFolder(oFolder)
        For Each oFile In oFolder.Files
            If (InStr(1, FileObj.Name,"AF68-",vbTextCompare) = 1) And _
                (DateDiff("d", fileObj.DateLastModified, Now) > 10) Then _
                fileObj.Copy (TARGET)
        Next

        For Each oSubfolder In oFolder.SubFolders
            SearchFolder oSubfolder
        Next
    End Sub

    Friday, December 5, 2014 11:13 PM
  • The reason I added those keywords was i had been prompted by Windows script host to do so. I cannot run the script without them becasue i keep reciving the runtime errors. I may have to scrub this project and start fresh.
    Friday, December 5, 2014 11:18 PM
  • I fixed the issue here is the finished and working Script.

    Set oFSO = CreateObject("Scripting.Filesystemobject")
    sFolder = "C:\Users\1385741622A\Desktop\Current 68's"
    TARGET = "C:\Users\1385741622A\Desktop\Due 68's\"
    SearchFolder oFSO.GetFolder(sFolder)

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

    Friday, December 5, 2014 11:31 PM
  • The reason I added those keywords was i had been prompted by Windows script host to do so. I cannot run the script without them becasue i keep reciving the runtime errors. I may have to scrub this project and start fresh.

    You need to go back and learn the very basic componenets of scripting.  You are jsut jamming things together with no idea of what you are doimg.  You are asking multiple quesitons and not understanding that you need to put in some effort to understand what is happening.  We cannot do this for you.  This forum is for technicians who know or are interested in learning the techology. It is not really set up to ask as an end user help desk.

    I will post a fix of your attempt to smash to things tohether.  Look at it carefully.  It will teach you a lot about what you are doing wrong.

    If you continue to have issues you will have to start a new question and attemp to describe your issue in an understandable way.  YOu mist post complete error messages.

    Here is the fix:

    Const SOURCE = "C:\Users\1385741622A\Desktop\Current 68's"
    Const TARGET = "C:\Users\1385741622A\Desktop\Due 68's\"
    
    
    Set oFSO = CreateObject("Scripting.Filesystemobject")
    Set SearchFolder = oFSO.GetFolder(source)
    
    Sub SearchFolder(oFolder)
    
        For Each oFile In oFolder.Files
            If InStr(oFile.Name,"AF68-") = 1 And DateDiff("d", oFile.DateLastModified, Now) > 10 Then
                oFile.Copy TARGET
            End If
        Next
    
        For Each oSubfolder In oFolder.SubFolders
            SearchFolder oSubfolder
        Next
        
    End Sub


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, December 5, 2014 11:36 PM
    Friday, December 5, 2014 11:34 PM
  • I fixed the issue here is the finished and working Script.


    Your script will run but I doubt very much that it will produce the results you expect. If you take the trouble to use proper code intendation (like I did) then you will see that your "if" / "end if" surrounds the wrong block.
    Friday, December 5, 2014 11:38 PM
  • Fred.  I think someone is clicking your switch.

    ¯\_(ツ)_/¯

    Friday, December 5, 2014 11:41 PM