1
votes

Ok Issues I'm having before posting my code... I'm getting this error

Error: Object Required; 'objDIR' Code: 800A01A8 On line 19

When I skip over it using On Error Resume Next and On Error GoTo 0 I get Error Object required: 'strFile' Line 22 When I skip over that It tells me that my last NEXT is not a collection... Here's the thing the program worked beautifully Until I added in the steps for getting .tif and .tiff files. Now even If I remove what I added it doesn't work...

The function of this program is to search a "source directory" for files that are archivable it then moves those files to "Destination Directory" and then checks them as unarchivable so that they aren't picked up a second time around. It also make a log of what files were copied with date stamps etc. Like I said it worked great but after tinkering for hours cant figure out what went wrong...

Option Explicit

Dim objLogFile, objFS, strFolder, strDestination, objFolder, eFolder, strFileName,  
strExtension, objDIR, strFile

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "S:\Source"
strDestination = "Z:\Destination"
Set objFolder = objFS.GetFolder(strFolder)
Set objLogFile = objFS.OpenTextFile ("S:\Log.txt",2,true)

Go(objFolder)

Sub Go(objDIR)
    objLogFile.Writeline "Script started at " & Now
    If objDIR <> "\System Volume Information" Then
        For Each eFolder In objDIR.SubFolders       
            Go eFolder
        Next
        On Error Resume Next
        For Each strFile In objDIR.Files
            On Error GoTo 0
            strFileName = strFile.Name
            strExtension = objFS.GetExtensionName(strFile)
            If strExtension = "pdf" Then
                If objFS.GetFile(strFile).Attributes And 32 Then
                    objFS.CopyFile strFile , strDestination & strFileName, True
                    objLogFile.Writeline "Copied " & strFileName & Now
                Else
                    objFS.GetFile(strFile).Attributes =          objFS.GetFile(strFile).Attributes XOR 32
                End If
            End If              
            If strExtension = "tif" Then
                If objFS.GetFile(strFile).Attributes And 32 Then
                    objFS.CopyFile strFile , strDestination & strFileName, True
                    objLogFile.Writeline "Copied " & strFileName & Now
                Else
                    objFS.GetFile(strFile).Attributes = objFS.GetFile(strFile).Attributes XOR 32
                End If      
            End If
            If strExtension = "tiff" Then
                If objFS.GetFile(strFile).Attributes And 32 Then
                    objFS.CopyFile strFile , strDestination & strFileName, True
                    objLogFile.Writeline "Copied " & strFileName & Now
                Else
                    objFS.GetFile(strFile).Attributes = objFS.GetFile(strFile).Attributes XOR 32
                End If                  
            End If
        Next  
        objFS.GetFile(strFile).Attributes = objFS.GetFile(strFile).Attributes XOR 32
    End If
    objLogFile.Writeline "Script ended at " & Now
    objLogFile.close

    WScript.Quit()
End Sub
2
I should add this is my first VBS script. - ELewis

2 Answers

1
votes

To solve your real world problem, you should consider:

  1. using tools like xcopy or robycopy
  2. avoiding traversal of (sub)folders that shouldn't contain user files; not scanning c:\windows or C:\System Volume Information would reduce risks and increase efficiency
  3. a few dir /s /b c:\can\di\date\*.tiff >> process-later.txt could give a 'clean' list of files to process with your .vbs

There is one obvious error: You try to avoid C:\System Volume Information, but you compare \System Volume Information (no drive).

Not so obvious is: You can't use

On Error Resume Next
 For Each strFile In objDIR.Files
On Error GoTo 0

to 'skip the loop for nasty elements'. OERN just doesn't work that way.

0
votes

I fixed the code... Not sure what exactly was wrong but I cleaned up the script and it works again. Here is the code incase anyone else may need it.

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "S:\Source"
strDestination = "Z:\Destination\"
Set objFolder = objFS.GetFolder(strFolder)
Set objLogFile = objFS.OpenTextFile ("S:Logs.txt",2,true)

Go(objFolder)

Sub Go(objDIR)
    objLogFile.Writeline "Script started at " & Now
    If objDIR <> "\System Volume Information" Then
        For Each eFolder In objDIR.SubFolders       
        Go eFolder
    Next
    For Each strFile in objDIR.files
        strFileName = strFile.Name
        strExtension = objFS.GetExtensionName(strFile)
        If strExtension = "pdf" Then
            If objFS.GetFile(strFile).Attributes And 32 Then
                objFS.CopyFile strFile , strDestination & strFileName, True
                objLogFile.Writeline "Copied " & strFileName & Now
                objFS.GetFile(strFile).Attributes = objFS.GetFile(strFile).Attributes XOR 32
            End If
        End If              
        If strExtension = "tif" Then
            If objFS.GetFile(strFile).Attributes And 32 Then
                objFS.CopyFile strFile , strDestination &     strFileName, True
                objLogFile.Writeline "Copied " & strFileName & Now
                objFS.GetFile(strFile).Attributes =     objFS.GetFile(strFile).Attributes XOR 32
            End If      
        End If
        If strExtension = "tiff" Then
            If objFS.GetFile(strFile).Attributes And 32 Then
                objFS.CopyFile strFile , strDestination &     strFileName, True
                objLogFile.Writeline "Copied " & strFileName & Now
                objFS.GetFile(strFile).Attributes =     objFS.GetFile(strFile).Attributes XOR 32
            End If                  
        End If
    Next  
End If
objLogFile.Writeline "Script ended at " & Now
objLogFile.close

WScript.Quit()
End Sub