locked
Folder Copy Help RRS feed

  • Question

  • Hey Forum!

    I've built a nifty vbs script to copy the contents of a folder to a folder on a external drive. The script tests if the target drive is ready, creates a date/time stamped folder on the target, writes a line to a logfile, and then uses the fso.copyfolder method to perform the actual copy. All the various events in the script execute perfectly except for the actual copy which fails with error 800A0005, invalid procedure call or argument. Shown below is a listing of the script. The error is occurring in line 30, char 2 which is the line that executes the fso.copyfolder. The platform the script runs on is 64-bit Windows 7 w/SP1. I've also tried it on a 32-bit Windows 7, SP1, as well as a 32-bit Windows 10, all to no avail. It'd simply be awesome if someone could point me in the right direction to get this resolved.

    Thanks!

    Everett

    ============================= The Script ============================

    Dim fso, d, f
    Dim SourceFolder
    Dim TargetFolder
    Dim Drive1
    Dim Drive2
    Dim DateTime
    Dim LogFile

    SourceFolder = "N:\"
    Logfile = "C:\NetworkBackup\NetworkBackupLog.Log"
    Drive1 = "F:"
    Drive2 = "L:"

    DateTime = cStr(Year(now())) & "_" & _
               Pad(cStr(Month(now())),2) & "_" & _
               Pad(cStr(Day(now())),2) & "_" & _
               Pad(cStr(Hour(now())),2) & "_" & _
               Pad(cStr(Minute(now())),2)

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

    set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpentextFile(LogFile,ForAppending,True,TristateFalse)
    set d = fso.GetDrive(Drive1)
    if d.isready then
     TargetFolder = Drive1 & "\Backup_" & DateTime
     fso.CreateFolder(TargetFolder)
     f.WriteLine DateTime & ": Network Backup Started ..."
     fso.CopyFolder SourceFolder, TargetFolder, True                         <====== Fails Here
     f.WriteLine DateTime & ": Backup Complete"
     f.WriteLine
     else
     set d = fso.GetDrive(Drive2)
     if d.isready then
      TargetFolder = Drive2 & "\Backup_" & DateTime
      fso.CreateFolder(TargetFolder)
      f.WriteLine DateTime & ": Network Backup Started ..."
      fso.CopyFolder SourceFolder, TargetFolder, True                   <======= And Here, too
      f.WriteLine DateTime & ": Backup Complete"
      f.WriteLine
      else
      f.Writeline DateTime & ": Backup Failed. Drives Not Ready."
      f.WriteLine
     end if
    end if
    f.Close
    set fso = nothing

    Function Pad(CStr2Pad, ReqStrLen)
    'Source: http://saltwetbytes.wordpress.com/2012/10/16/vbscript-adding-datetime-stamp-to-log-file-name/
        Dim Num2Pad
     
        Pad = CStr2Pad
        If len(CStr2Pad) < ReqStrLen Then
            Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0")
            Pad = Num2Pad & CStr2Pad
        End If
    End Function

    Wednesday, January 6, 2016 1:36 AM

Answers

  • This will be more reliable:

    SourceFolder = "c:\test"
    Logfile = "C:\test\NetworkBackupLog.Log"
    Drive1 = "f:"
    drive2 = "d:"
    
    DateTime =  Year(Now) & "_" _
    		& Right("0" & Month(Now),2) & "_" _
    		& Right("0" & Day(now),2) & "_" _
    		& Right("0" & Hour(Now),2) & "_" _
            & Right("0" & Minute(Now),2)
            
    set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpentextFile(LogFile,8,True)
    
    set d = fso.GetDrive(Drive1)
    if fso.GetDrive(Drive1).isready Then 
    	drive = drive1
    ElseIf fso.GetDrive(Drive2).IsReady Then
    	drive = drive2
    Else
    	WScript.Echo "No drives ready"
    	f.Writeline DateTime & ": Backup Failed. Drives Not Ready."
    	f.WriteLine
    	WScript.Quit 1
    End If
    
    targetFolder = drive & "\Backup_" & DateTime
    fso.CreateFolder(TargetFolder)
    f.WriteLine DateTime & ": Network Backup Started ..."
    fso.CopyFolder SourceFolder, TargetFolder, True
    f.WriteLine DateTime & ": Backup Complete"
    f.WriteLine
    
    
    


    \_(ツ)_/

    Wednesday, January 6, 2016 6:38 AM
  • Try adding a trailing backslash to TargetFolder, indicating that the folder exists. Or, do not add the backslash, but do not create the folder before using CopyFolder. Then the method will create the folder.

    Richard Mueller - MVP Enterprise Mobility (Directory Services)

    Wednesday, January 6, 2016 9:13 AM

All replies

  • This will be more reliable:

    SourceFolder = "c:\test"
    Logfile = "C:\test\NetworkBackupLog.Log"
    Drive1 = "f:"
    drive2 = "d:"
    
    DateTime =  Year(Now) & "_" _
    		& Right("0" & Month(Now),2) & "_" _
    		& Right("0" & Day(now),2) & "_" _
    		& Right("0" & Hour(Now),2) & "_" _
            & Right("0" & Minute(Now),2)
            
    set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpentextFile(LogFile,8,True)
    
    set d = fso.GetDrive(Drive1)
    if fso.GetDrive(Drive1).isready Then 
    	drive = drive1
    ElseIf fso.GetDrive(Drive2).IsReady Then
    	drive = drive2
    Else
    	WScript.Echo "No drives ready"
    	f.Writeline DateTime & ": Backup Failed. Drives Not Ready."
    	f.WriteLine
    	WScript.Quit 1
    End If
    
    targetFolder = drive & "\Backup_" & DateTime
    fso.CreateFolder(TargetFolder)
    f.WriteLine DateTime & ": Network Backup Started ..."
    fso.CopyFolder SourceFolder, TargetFolder, True
    f.WriteLine DateTime & ": Backup Complete"
    f.WriteLine
    
    
    


    \_(ツ)_/

    Wednesday, January 6, 2016 6:38 AM
  • Try adding a trailing backslash to TargetFolder, indicating that the folder exists. Or, do not add the backslash, but do not create the folder before using CopyFolder. Then the method will create the folder.

    Richard Mueller - MVP Enterprise Mobility (Directory Services)

    Wednesday, January 6, 2016 9:13 AM
  • jrv, Richard...... Thank's for your suggestions. I ended up using both of them in the final solution which is now happily working away. The final completed script is shown below. One other change I made was to add a wildcard character to the SourceFolder specification. I think I had to do this because the SourceFolder was a drive. At any rate, the script is working great and thanks to both of you for your help.

    Everett

    ================= The Script =========================

    SourceFolder = "n:\*"
    Logfile
    = "C:\networkbackup\NetworkBackupLog.Log"
    Drive1
    = "f:"
    drive2
    = "l:"

    DateTime
    =  Year(Now) & "_" _
    & Right("0" & Month(Now),2) & "_" _
    & Right("0" & Day(now),2) & "_" _
    & Right("0" & Hour(Now),2) & "_" _
           
    & Right("0" & Minute(Now),2)
           
    set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpentextFile(LogFile,8,True)

    set d = fso.GetDrive(Drive1)
    if fso.GetDrive(Drive1).isready Then
    drive
    = drive1
    ElseIf fso.GetDrive(Drive2).IsReady Then
    drive
    = drive2
    Else
    f.Writeline DateTime & ": Backup Failed. Drives Not Ready."
    f
    .WriteLine
    WScript
    .Quit 1
    End If

    targetFolder
    = drive & "\Backup_" & DateTime & "\"
    fso
    .CreateFolder(TargetFolder)
    f
    .WriteLine DateTime & ": Network Backup Started ..."
    fso
    .CopyFolder SourceFolder, TargetFolder, True
    f
    .WriteLine DateTime & ": Backup Complete"
    f
    .WriteLine

    Thursday, January 7, 2016 12:33 AM