VBScript Question: Outputing results from objFSO.CopyFolder

Edmond 6 Reputation points
2022-02-21T20:08:37.167+00:00

Hi there,

I apologize if I am posting to the wrong forum.

I am trying to modify a VBScript that copy folders and contents from on location to another by using objFSO.CopyFolder. What I want to do is for the script to perform the copy, list all the files/folders that were copied, and email that result. I have got the base code that does the copying. I have figured out how to email say a txt file. But I could not find anything about if I can output and list all the files/folders being copied via objFSO.CopyFolder and have that saved as a log file.

Just wondering if someone could point me to the right direction please? I have listed the code I have for the copying process below.

Thanks,

Edmond.


Dim objFSO,objFolder,objSubfolder
Dim strDestination,strStart,strDestinationFolder

'Define overwrite

Const OverWriteFiles = True

strDestination="c:\temp1"
strStart="c:\temp"

Set objFSO = CreateObject("Scripting.FileSystemObject")
set objSubfolder=objFSO.GetFolder(strStart).SubFolders

For Each objFolder In objSubfolder

strDestinationFolder=strDestination & "\" & objFolder.Name
  

If objFSO.FolderExists(strDestinationFolder) = False And objFolder.Name <> strCurrentDate Then

objFSO.CreateFolder(strDestinationFolder)
objFSO.CopyFolder objFolder.path , strDestinationFolder , OverWriteFiles 

End if

Next

MsgBox "Copy completed"

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,711 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Edmond 6 Reputation points
    2022-02-22T23:09:22.897+00:00

    I think I have managed to find the answer to my own question. This is my final code. New codes are in bold and commented out the part that send email.


    Dim objFSO,objFolder,objSubfolder
    Dim strDestination,strStart,strDestinationFolder
    Dim objLogger

    'Define overwrite
    Const OverWriteFiles = True
    Const ForAppending = 8

    'Sync
    strDestination="c:\temp2"
    strStart="c:\temp1"
    'Define the log file
    strLog="C:\Scripts\Copy.Log"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    set objSubfolder=objFSO.GetFolder(strStart).SubFolders

    'Clear the old log
    if objFSO.FolderExists(strLog) = False Then objFSO.DeleteFile(strLog)
    set objLogFile=objFSO.OpenTextFile(strLog,ForAppending,True)
    objLogFile.WriteLine("New folders backed up as on " & Date() & " " & Time() & vbCrLf)

    For Each objFolder In objSubfolder

    strDestinationFolder=strDestination & "\" & objFolder.Name

    If objFSO.FolderExists(strDestinationFolder) = False Then

    objFSO.CreateFolder(strDestinationFolder)
    objFSO.CopyFolder objFolder.path , strDestinationFolder , OverWriteFiles

    set objDestFolder = objFSO.GetFolder(strDestinationFolder)
    For Each objSubFolder in objDestFolder.SubFolders
    For Each objCopiedFiles in objSubFolder.Files
    objLogFile.WriteLine("File Copied " & objSubFolder & "\" & obCopiedFiles & vbCrLf)
    Next
    Next

    End if

    Next

    objLogFile.Close()
    'Generate Email message and attach log file as attachment
    ' Set objMessage = CreateObject("CDO.Message")
    ' objMessage.Subject = "File Copy Result"
    ' objMessage.Sender = "Your Email"
    ' objMessage.To = "Someone's Email address"
    ' objMessage.TextBody = "File copy completed"
    'objMessage.Configuration.Fields.Item _
    ' ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    'objMessage.Configuration.Fields.Item _
    ' ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
    ' SMTP_server
    'objMessage.Configuration.Fields.Item _
    ' ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    'objMessage.AddAttachment "C:\Scripts\Copy.Log"
    'objMessage.Configuration.Fields.Update
    'objMessage.Send

    MsgBox "Finished Copying!"

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.