none
VBS script Error

    Question

  • The script i am trying to use is for filing multiple files with the same beginning to their names (AF68-) i need it to only copy the files that are over 30 days old so i can accomplish the file. I keep getting runtime errors or expected end of statement. Once i can get this to work i am going to try to make another version that will also let me get these files from sub folders also. Any help would be great and appreciated.

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

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

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


    • Edited by LonePenguin Thursday, December 04, 2014 5:46 PM
    Thursday, December 04, 2014 5:44 PM

Answers

  • Perhaps this is what you intend (but not tested):

    Dim SOURCE, TARGET, fso, SourceObj, TargetObj, FileObj
    
    SOURCE = "C:\Users\reese\Desktop\Current 68's"
    TARGET = "C:\Users\reese\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) > 30) Then
            fileObj.Copy (TARGET)
        End If
    Next
    


    Richard Mueller - MVP Directory Services

    • Marked as answer by LonePenguin Thursday, December 04, 2014 8:55 PM
    Thursday, December 04, 2014 6:41 PM
    Moderator

All replies

  • objSingleFile is not defined. Also, what is being And'd, the If and what. I cannot be sure what you want.


    Richard Mueller - MVP Directory Services

    Thursday, December 04, 2014 6:23 PM
    Moderator
  • Perhaps this is what you intend (but not tested):

    Dim SOURCE, TARGET, fso, SourceObj, TargetObj, FileObj
    
    SOURCE = "C:\Users\reese\Desktop\Current 68's"
    TARGET = "C:\Users\reese\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) > 30) Then
            fileObj.Copy (TARGET)
        End If
    Next
    


    Richard Mueller - MVP Directory Services

    • Marked as answer by LonePenguin Thursday, December 04, 2014 8:55 PM
    Thursday, December 04, 2014 6:41 PM
    Moderator
  • That was Perfect thank you so much. I am going to try and make a version of this now that also includes sub directories.

    Thursday, December 04, 2014 6:48 PM