Jump to content

Create folder based on todays date and copy from another folder - log date/time


Recommended Posts

Posted

I would like the below VBS to write to a text file called backuplog_) to a given folder (as a log) the start date/time and the end date/time the process took and show what was copied? Is this possible?

 

In other words, I would like a textfile called backuplog_ and inside that lines showing

 

C: Volume backup Backup Starting at hh:mm:ss

Copying Volume_1 folders

 

c:\mydocs\Data\pic1.png

c:\mydocs\Data\pic2.jpg

 

etc of each directory and file copied across

 

then at the end of the file and to show the script has finished

 

C: Volume backup Backup Starting at hh:mm:ss

 

I just would like to know how long the process took to backup and a start and end date/time is OK for my needs.

 

Also if it's just possible to show hours and minutes and not seconds that is OK.

 

Here is the code:

 

-----------------------------------------------------------

 

Option Explicit

On Error Resume Next

 

' Change the following lines

Dim sSrcFolder : sSrcFolder = "C:\Data" ' Change Me

Dim sDstFolder : sDstFolder = "E:\DataBackup" ' Change Me

 

'=======================================

' Do not change anything after this line

 

Dim DTM : DTM = Now()

 

' Set date variables

Dim Y, M, D, sToday

Y = Year(DTM)

M = Right("0" & Month(DTM),2)

D = Right("0" & Day(DTM),2)

sToday = D & "_" & M & "_" & Y

 

' Set time variables

Dim H, N, S, sTime

H = Hour(DTM)

N = Right("0" & Minute(DTM),2)

S = Right("0" & Second(DTM),2)

sTime = H & "_" & N & "_" & S

 

' Create objects

Dim oProc

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

Dim oShell : Set oShell = WScript.CreateObject("WScript.Shell")

 

Sub Main()

sDstFolder = sDstFolder & sToday ' Updated the destination folder

 

' First check if folders exists and create if they dont exist

If oFSO.FolderExists(sDstFolder) Then

If not oFSO.FolderExists(sTime) Then

'oFSO.CreateFolder(sTime)

End If

Else

 

' Create the destination directory if it doesn't exist

If Not oFSO.FolderExists(sDstFolder) Then BuildFullPath sDstFolder

 

' Copy data

If oFSO.FolderExists(sSrcFolder) Then oFSO.CopyFolder sSrcFolder & "*", sDstFolder, True

If Err.Number <> 0 Then ErrorHandler

 

End If

 

' Clean up

If oFSO.FileExists(sFile) Then oFSO.DeleteFile sFile, True

Set oFSO = Nothing

Set oShell = Nothing

Set oProc = Nothing

End Sub

 

Sub BuildFullPath(ByVal FullPath)

If Not oFSO.FolderExists(FullPath) Then

BuildFullPath oFSO.GetParentFolderName(FullPath)

oFSO.CreateFolder FullPath

If Err.Number <> 0 Then ErrorHandler

End If

End Sub

 

 

Main()

Posted
HPlum78 - Not really it can be anything really. I just found the code and thought can it be changed to fit my needs. If it can be 5 lines of code that is even better. Are you able to share on what the lines will be? New to PowerShell but keen to see it work.
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...