Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

VB Script: Correction backup files and folders v3

Hello experts,

I have use the following script which allows me to backup (copy files and folders from and transfer them to a datestamp folder)
reported at: https://www.experts-exchange.com/questions/28936561/VB-Script-copy-backup-files-and-folders-v2.html

The script works properly when I report just one folder in the arrfolders. However when I report two folders, the second folder is not properly transferred in the datestamp folder I don't know if the byVal delcaration should be performed for the variables


' File I/O constants
Const ForAppending = 8

'Create the file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strLogFile = strScriptDir & "\log-copy.txt"
strStamp = TimeStamp(Now)
arrFolders = Array("C:\Folder1,C:\Folder2")
strFromFolder = ""
strToFolder = strScriptDir
blnFolders = True
arrSkipFolders = Array("Backups-Files")

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Loop through all folders, backup files
For Each strFolder in arrFolders
   BackupFilesFolders strFolder, blnFolders
Next

' Wrap up
objLog.Close

Wscript.Echo Now & " :" & vbCrLf & "Program has completed all the actions successfully"


' Process folder
Sub BackupFilesFolders(strPath, blnFolders)

   ' If the input folder does not exist then skip this folder
   strFromPath = strPath & "\" & strFromFolder
   If Not objFSO.FolderExists(strFromPath) Then
      objLog.WriteLine Now & " WARNING: Input folder """ & strFromPath & """ does not exist, skipped."
      Exit Sub
   End If

   ' If the csv backup folder does not exist create it
   strToPath = strToFolder
   If Not objFSO.FolderExists(strToPath) Then
      objLog.WriteLine Now & " INFO: Created folder """ & strToPath & """."
      objFSO.CreateFolder(strToPath)
   End If

   ' Create a folder for todays backups
   strToPath = strToPath & "\" & strStamp
   If Not objFSO.FolderExists(strToPath) Then
      objLog.WriteLine Now & " INFO: Created folder """ & strToPath & """."
      objFSO.CreateFolder(strToPath)
   End If

   ' Process all files in folder
   For Each objFromFile In objFSO.GetFolder(strFromPath).Files
      strToFile = objFSO.BuildPath(strToPath, objFromFile.Name)
      ' Indicate if we are overwriting or not, and copy the file
      If objFSO.FileExists(strToFile) Then
         objLog.WriteLine Now & " INFO: Overwriting file from : """ & objFromFile.Path & """ to """ & strToFile & """."
         objFromFile.Copy strToFile, True
      Else
         objLog.WriteLine Now & " INFO: Copying file from : """ & objFromFile.Path & """ to """ & strToFile & """."
         objFromFile.Copy strToFile, False
      End If

   Next
   
   ' Process all Folders
   If blnFolders = True Then
      For Each objFromFolder In objFSO.GetFolder(strFromPath).SubFolders
         blnSkipFolder = False
         For Each strSkipFolder In arrSkipFolders
            If LCase(strSkipFolder) = LCase(objFromFolder.Name) Then
               blnSkipFolder = True
            End If
         Next

         If blnSkipFolder = False Then
            strToFolder = objFSO.BuildPath(strToPath, objFromFolder.Name)
            ' Indicate if we are overwriting or not, and copy the file
            If objFSO.FolderExists(strToFolder) Then
               objLog.WriteLine Now & " INFO: Overwriting file from : """ & objFromFolder.Path & """ to """ & strToFile & """."
               objFromFolder.Copy strToFolder, True
            Else
               objLog.WriteLine Now & " INFO: Copying file from : """ & objFromFolder.Path & """ to """ & strToFile & """."
               objFromFolder.Copy strToFolder, False
            End If
         End If

      Next
   End If

End Sub

' Build string of current date time (DDMMYYYY_HHMMSS)
Function TimeStamp(dtmDateTime)
   TimeStamp = Year(dtmDateTime) & Right("0" & Month(dtmDateTime), 2) & Right("0" & Day(dtmDateTime), 2) & "_" & Right("0" & Hour(dtmDateTime), 2) & Right("0" & Minute(dtmDateTime), 2) & Right("0" & Second(dtmDateTime), 2)
End Function

Open in new window


if you have questions, please contact me.

Thank you again for your help.
Avatar of Bill Prew
Bill Prew

Try this change:

arrFolders = Array("C:\Folder1", "C:\Folder2")

Open in new window

~bp
Avatar of Luis Diaz

ASKER

Thank you for your proposal. I tested but I have the same problem.
Please double check that change, it should have fixed the problem.  Perhaps post the updated line here.

Also can you post the log file here for the failed test.

~bp
Exactly the log will allows us to now how the program proceed.

Please find attached the log file I reformat in excel in order to highlight the lines with error.

The log has been generated with the following vbscript configuration :

' File I/O constants
Const ForAppending = 8

'Create the file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strLogFile = strScriptDir & "\log-copy.txt"
strStamp = TimeStamp(Now)
arrFolders = Array("F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\29000596_send-notification-based-list-attached-folder", "F:\2-Files-Storage-Reference\5.R-Files\3-Procedures\Z_old")
strFromFolder = ""
strToFolder = strScriptDir & "\"
blnFolders = True
arrSkipFolders = Array("")

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Loop through all folders, backup files
For Each strFolder in arrFolders
   BackupFilesFolders strFolder, blnFolders
Next

' Wrap up
objLog.Close

Wscript.Echo Now & " :" & vbCrLf & "Program has completed all the actions successfully"


' Process folder
Sub BackupFilesFolders(strPath, blnFolders)

   ' If the input folder does not exist then skip this folder
   strFromPath = strPath & "\" & strFromFolder
   If Not objFSO.FolderExists(strFromPath) Then
      objLog.WriteLine Now & " WARNING: Input folder """ & strFromPath & """ does not exist, skipped."
      Exit Sub
   End If

   ' If the csv backup folder does not exist create it
   strToPath = strToFolder
   If Not objFSO.FolderExists(strToPath) Then
      objLog.WriteLine Now & " INFO: Created folder """ & strToPath & """."
      objFSO.CreateFolder(strToPath)
   End If

   ' Create a folder for todays backups
   strToPath = strToPath & "\" & strStamp
   If Not objFSO.FolderExists(strToPath) Then
      objLog.WriteLine Now & " INFO: Created folder """ & strToPath & """."
      objFSO.CreateFolder(strToPath)
   End If

   ' Process all files in folder
   For Each objFromFile In objFSO.GetFolder(strFromPath).Files
      strToFile = objFSO.BuildPath(strToPath, objFromFile.Name)
      ' Indicate if we are overwriting or not, and copy the file
      If objFSO.FileExists(strToFile) Then
         objLog.WriteLine Now & " INFO: Overwriting file from : """ & objFromFile.Path & """ to """ & strToFile & """."
         objFromFile.Copy strToFile, True
      Else
         objLog.WriteLine Now & " INFO: Copying file from : """ & objFromFile.Path & """ to """ & strToFile & """."
         objFromFile.Copy strToFile, False
      End If

   Next
   
   ' Process all Folders
   If blnFolders = True Then
      For Each objFromFolder In objFSO.GetFolder(strFromPath).SubFolders
         blnSkipFolder = False
         For Each strSkipFolder In arrSkipFolders
            If LCase(strSkipFolder) = LCase(objFromFolder.Name) Then
               blnSkipFolder = True
            End If
         Next

         If blnSkipFolder = False Then
            strToFolder = objFSO.BuildPath(strToPath, objFromFolder.Name)
            ' Indicate if we are overwriting or not, and copy the file
            If objFSO.FolderExists(strToFolder) Then
               objLog.WriteLine Now & " INFO: Overwriting file from : """ & objFromFolder.Path & """ to """ & strToFile & """."
               objFromFolder.Copy strToFolder, True
            Else
               objLog.WriteLine Now & " INFO: Copying file from : """ & objFromFolder.Path & """ to """ & strToFile & """."
               objFromFolder.Copy strToFolder, False
            End If
         End If

      Next
   End If

End Sub

' Build string of current date time (DDMMYYYY_HHMMSS)
Function TimeStamp(dtmDateTime)
   TimeStamp = Year(dtmDateTime) & Right("0" & Month(dtmDateTime), 2) & Right("0" & Day(dtmDateTime), 2) & "_" & Right("0" & Hour(dtmDateTime), 2) & Right("0" & Minute(dtmDateTime), 2) & Right("0" & Second(dtmDateTime), 2)
End Function

Open in new window

log-copy.xlsx
That is processing both folders in your array, I see the second folder starting in this line:

2/9/2017 4:38:19 PM INFO: Copying file from : "F:\2-Files-Storage-Reference\5.R-Files\3-Procedures\Z_old\Add-CCWT-user.docx" to "F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\_backup-files-folders\\\20170209_163818\ref-folder\20170209_163818\Add-CCWT-user.docx".

Open in new window

~bp
Yes and the problem is that a new datestamp folder is created and place unproperly and place the Folder reported in the second array. I supposed this is due to the fact the datestamp is created twice.

In my example the folder "F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\_backup-files-folders\20170209_163818" should receive the folders reported in the Array.
Why we got double date stamp creation?

\20170209_163818\ref-folder\20170209_163818\

Thank you again for your help.
Corrected a couple of things, give this a try and if still have problems post up the log from it please.

' File I/O constants
Const ForAppending = 8

'Create the file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetParentFolderName(objFSO.GetFile(Wscript.ScriptFullName))
strLogFile = strScriptDir & "\log-copy.txt"
strStamp = TimeStamp(Now)
arrFolders = Array("F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\29000596_send-notification-based-list-attached-folder", "F:\2-Files-Storage-Reference\5.R-Files\3-Procedures\Z_old")
strFromFolder = ""          ' Do not add trailing "\" to this variable
strToFolder = strScriptDir  ' Do not add trailing "\" to this variable
blnFolders = True
arrSkipFolders = Array("")

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Loop through all folders, backup files
For Each strFolder in arrFolders
   BackupFilesFolders strFolder, blnFolders
Next

' Wrap up
objLog.Close

Wscript.Echo Now & " :" & vbCrLf & "Program has completed all the actions successfully"


' Process folder
Sub BackupFilesFolders(strPath, blnFolders)

   ' If the input folder does not exist then skip this folder
   If strFromFolder = "" Then 
      strFromPath = strPath
   Else
      strFromPath = strPath & "\" & strFromFolder
   End If
   If Not objFSO.FolderExists(strFromPath) Then
      objLog.WriteLine Now & " WARNING: Input folder """ & strFromPath & """ does not exist, skipped."
      Exit Sub
   End If

   ' If the csv backup folder does not exist create it
   strToPath = strToFolder
   If Not objFSO.FolderExists(strToPath) Then
      objLog.WriteLine Now & " INFO: Created folder """ & strToPath & """."
      objFSO.CreateFolder(strToPath)
   End If

   ' Create a folder for todays backups
   strToPath = strToPath & "\" & strStamp
   If Not objFSO.FolderExists(strToPath) Then
      objLog.WriteLine Now & " INFO: Created folder """ & strToPath & """."
      objFSO.CreateFolder(strToPath)
   End If

   ' Process all files in folder
   For Each objFromFile In objFSO.GetFolder(strFromPath).Files
      strToFile = strToPath & "\" & objFromFile.Name
      ' Indicate if we are overwriting or not, and copy the file
      If objFSO.FileExists(strToFile) Then
         objLog.WriteLine Now & " INFO: Overwriting file from : """ & objFromFile.Path & """ to """ & strToFile & """."
         objFromFile.Copy strToFile, True
      Else
         objLog.WriteLine Now & " INFO: Copying file from : """ & objFromFile.Path & """ to """ & strToFile & """."
         objFromFile.Copy strToFile, False
      End If

   Next
   
   ' Process all Folders
   If blnFolders = True Then
      For Each objFromFolder In objFSO.GetFolder(strFromPath).SubFolders
         blnSkipFolder = False
         For Each strSkipFolder In arrSkipFolders
            If LCase(strSkipFolder) = LCase(objFromFolder.Name) Then
               blnSkipFolder = True
            End If
         Next

         If blnSkipFolder = False Then
            strToFolder = strToPath & "\" & objFromFolder.Name
            ' Indicate if we are overwriting or not, and copy the file
            If objFSO.FolderExists(strToFolder) Then
               objLog.WriteLine Now & " INFO: Overwriting folder from : """ & objFromFolder.Path & """ to """ & strToFolder & """."
               objFromFolder.Copy strToFolder, True
            Else
               objLog.WriteLine Now & " INFO: Copying folder from : """ & objFromFolder.Path & """ to """ & strToFolder & """."
               objFromFolder.Copy strToFolder, False
            End If
         End If

      Next
   End If

End Sub

' Build string of current date time (DDMMYYYY_HHMMSS)
Function TimeStamp(dtmDateTime)
   TimeStamp = Year(dtmDateTime) & Right("0" & Month(dtmDateTime), 2) & Right("0" & Day(dtmDateTime), 2) & "_" & Right("0" & Hour(dtmDateTime), 2) & Right("0" & Minute(dtmDateTime), 2) & Right("0" & Second(dtmDateTime), 2)
End Function

Open in new window

~bp
Thank you very much for this proposal!
Unable to test it right now. I will test it tomorrow.
Hello Bill,

I tested the last version.

I understand the problem if the first reported folder in array contains a sub folders the he second reported folder in array (files and folders) are transferred to one of the subfolder of the first reported array in  a datestamp folder, instead of transferred the files and folders directly in the datestamp as it is done for the first array:

User generated imageExample:
2/10/2017 11:10:17 AM INFO: Created folder "F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\_backup-files-folders\20170210_111017\ref-folder\20170210_111017".

This line is incorrect as the reference datestamp folder which should receive the various folders reported in the array has been already created
"F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\_backup-files-folders\20170210_111017"


Files and folders from the second array should be transferred to
"F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\_backup-files-folders\20170210_111017"


Instead of:
"F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\_backup-files-folders\20170210_111017\ref-folder\20170210_111017".


Do you know what should be modified to correct this issue?

Thank you very much for your help.
log-copy.txt
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Bill for this update. I tested and it works!