Re: Slightly more complex Copy script - a challenge perhaps?

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hi Bart,

Many Thanks for the quick response! I'll start digging through this script
and see what I can do.

If anyne else has suggestions, its much appreciated.


"Bart Perrier" wrote:


"NoSurprises" <NoSurprises@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E68F42A7-8724-4208-8107-0986D6A2D563@xxxxxxxxxxxxxxxx
Hi Everyone :o)

Thanks in advance to anyone who repsonds to this with suggestions :o) I
will try and be as clear as I can.

Aim:- Copy files from one location to another (UNC or Mapped Drive), based
on modified time, and file extension, to specific folders\subfolders, and
to
have a new folder (folder name based on the current date) created within
the
folder\subfolder for these files. Phew!

Example1:-

C:\FolderA\File.D1 to be copied to

D:\FolderB\FolderC\130707\File.D1

Where D:\FolderB is a UNC or mapped Drive
FolderC is a folder for files with the .D1 File Extension
130707 is the new folder created by script

Example2:-

C:\FolderA\File.E1 to be copied to

D:\FolderB\FolderD\130707\File.E1

Where D:\FolderB is a UNC or mapped Drive
FolderD is a folder for files with the .E1 File Extension
130707 is the new folder created by script

Same process but because file extension differs to that in Example1, it is
copied to the folder for .E1 extesnions.

Basically a way of copying the files, and organising based on file
extension
and modified time I guess.

Hope this is clear!


Here is something that I put together for a similar copy job need but does
not organize the TARGET_LOCATION as I understand you would like. However,
you could modify it before the copy process.

My objective here was to check the modified date and only copy the files
where the SOURCE is newer than the TARGET. I am catching instances where the
TARGET is newer than the SOURCE (I think that is during the process that
genearates the file). I then format the results into an HTML email. Again,
it is not exactly what you requested but it will give you a good start. The
script runs as a scheduled task on the TARGET_SERVER.

I am certain this will have a lot of wordwrap issues from pasting the script
in this post.

Bart

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Error Resume Next

Dim objFSO, Folder, ctrFiles, startTime, totalSize, endTime, elapsedTime,
ctrErrors, objSourceFile, objTargetFile, emailMessage, _
colSourceFiles, subject, ctrConcerns

'**********************************************************************'
' only the lines in this section need to change to add a new copy job *'
'**********************************************************************'
' File Constants
Const SOURCE_LOCATION = "\\server\share\etc\" ' included the trailing
backslash
Const TARGET_LOCATION = "\\server\share\etc\" 'included the trailing
backslash

' email variable
subject = "YOUR EMAIL SUBJECT"

' Email Constants
Const SMTP_SERVER = "YOUR SMTP SERVER"
Const FROM_ADDRESS = "YOUR FROM ADDRESS"
Const TO_ADDRESS = "YOUR TO ADDRESS"
Const CC_ADDRESS = ""
Const BCC_ADDRESS = ""
'**********************************************************************'

startTime = Now
ctrFiles = 0
ctrErrors = 0
totalSize = 0
ctrConcerns = 0


' Display information in the CMD window regarding what the window is doing
WScript.Echo
WScript.Echo "***************************************************"
WScript.Echo " Copying " & subject
WScript.Echo " Canceling could damage database log files"
WScript.Echo " Start Time: " & Space(2) & startTime & Space(2)
WScript.Echo "***************************************************"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFSO.GetFolder(SOURCE_LOCATION)

If ConnectionError(SOURCE_LOCATION) = False Then
Set colSourceFiles = objSourceFolder.Files


For Each objSourceFile In colSourceFiles
sourceFileName = objSourceFile.name
sourceFileSize = objSourceFile.size
sourceFileLastModified = objSourceFile.DateLastModified
sourceFilePath = objSourceFile.path

If objFSO.FileExists(TARGET_LOCATION & sourceFileName) Then
set objTargetFile = objFSO.GetFile(TARGET_LOCATION & sourceFileName)
targetFileName = objTargetFile.Name
targetFileSize = objTargetFile.Size
targetFileLastModified = objTargetFile.DateLastModified
targetFilePath = objTargetFile.Path

' DateDiff comments on conditions
' (DateDiff("s", sourceFileLastModified, targetFileLastModified) <
0) ---- Source File Newer
' (DateDiff("s", sourceFileLastModified, targetFileLastModified) >
0) ---- Target File Newer
If (DateDiff("s", sourceFileLastModified, targetFileLastModified) < 0)
Then
' source file newer
Call DisplaySourceFileInformation
Call DisplayTargetFileInformation
objFSO.CopyFile objSourceFile.Path,TARGET_LOCATION, True
Call CheckForCopyError
totalSize = totalSize + sourceFileSize
FormatLineForHTML("")
Elseif(DateDiff("s", sourceFileLastModified, targetFileLastModified) > 0)
Then
ctrConcerns = ctrConcerns + 1
Call DisplaySourceFileInformation
Call DisplayTargetFileInformation
FormatLineForHTML("<b>-- DateDiff exists")
FormatLineForHTML("-- Target File Newer Than Source File</b>")
FormatLineForHTML("")
Else
If (sourceFileSize <> targetFileSize) Then
Call DisplaySourceFileInformation
Call DisplayTargetFileInformation
FormatLineForHTML("<b>--------------------> sourceFileSize <>
targetFileSize</b>")
ctrConcerns = ctrConcerns + 1
FormatLineForHTML("")
End If
End If
Else
Call DisplaySourceFileInformation
objFSO.CopyFile objSourceFile.Path,TARGET_LOCATION, False
Call CheckForCopyError
FormatLineForHTML("")
totalSize = totalSize + sourceFileSize
End If
Call ClearVariables
Next
End If

endTime = Now
elapsedTime = DateDiff("s", startTime, endTime)

' add the results to the top of the email
emailMessage = "Copy Job Notification<br>" & _
"Start Time:" & Space(1) & startTime & "<br>" & _
"SOURCE_LOCATION:" & Space(1) & SOURCE_LOCATION & "<br>" & _
"TARGET_LOCATION:" & Space(1) & TARGET_LOCATION & "<br>" & _
"Errors:" & Space(1) & ctrErrors & "<br>" & _
"Concerns:" & Space(1) & ctrConcerns & "<br>" & _
"End Time:" & Space(1) & endTime & "<br>" & _
"Total Size:" & Space(1) & FormatNumber(totalSize/1024/1024, 3) &
Space(1) & "MBs" & "<br>" & _
"Throughput:" & Space(1) & FormatNumber((totalSize/1024)/elapsedTime, 0)
& Space(1) & "KB/second" & "<br>" & _
"Elapse Time:" & Space(1) & elapsedTime & Space(1) & "Seconds" & "<br>"
& _
"------------------------------------------------------<br><br>" & _
emailMessage

Call SendMessage



''''''''''''''''''''''
'''' Sub Routines ''''
''''''''''''''''''''''

Sub ClearVariables
targetFileName = ""
targetFileSize = ""
targetFileLastModified = ""
targetFilePath = ""
End Sub


Function ConnectionError(path)
Select Case Err.Number
Case 0
ConnectionError = False
Case 76
ConnectionError = True
emailMessage = "<b>" & "Path Not Found" & "<br>" & _
path & "</b>" & "</br>"
ctrErrors = ctrErrors + 1
Err.Clear
Case Else
ConnectionError = True
emailMessage = "<b>" & "Error Number:" & Space(1) & Err.Number & "<br>"
& _
"Error Description:" & Space(1) & err.Description & "<br>" & _
"Path --" & Space(1) & path & "</b>" & "</br>"
ctrErrors = ctrErrors + 1
Err.Clear
End Select
End Function


' Display Source File Information
Sub DisplaySourceFileInformation
FormatLineForHTML("<b>S</b>-Name:" & Space(1) & sourceFileName & "," &
Space(1) & "Size:" & Space(1) & FormatNumber(sourceFileSize, 0) & Space(1) &
"Bytes" & _
"," & Space(1) & "DateLastModified:" & Space(1) &
sourceFileLastModified & "," & Space(1) & "Path:" & Space(1) &
sourceFilePath)
End Sub

' Display Target File Information
Sub DisplayTargetFileInformation
FormatLineForHTML("<b>T</b>-Name:" & Space(1) & targetFileName & "," &
Space(1) & "Size:" & Space(1) & FormatNumber(targetFileSize, 0) & Space(1) &
"Bytes" & _
"," & Space(1) & "DateLastModified:" & Space(1) &
targetFileLastModified & "," & Space(1) & "Path:" & Space(1) &
targetFilePath)
FormatLineForHTML("---------------------------------------")
End Sub


' specific copy job error handling
Sub CheckForCopyError
Select Case Err.number
Case 0
FormatLineForHTML("<b>File Copy Successful</b>")
FormatLineForHTML("---------------------------------------")
Case Else
FormatLineForHTML("<b>Unexpected Error")
FormatLineForHTML("Error Number: " & Space(1) & Err.Number)
FormatLineForHTML("Error Description: " & Space(1) & Err.Description &
"</b>")
FormatLineForHTML("---------------------------------------")
ctrErrors = ctrErrors + 1
End Select
Err.Clear
End Sub

' Sends the message
Sub SendMessage
Dim objEmail

Set objEmail = CreateObject("CDO.Message")

subject = subject & Space(1) & "Errors:" & Space(1) & ctrErrors & Space(1)
& "Concerns:" & Space(1) & ctrConcerns

objEmail.From = FROM_ADDRESS
objEmail.To = TO_ADDRESS
objEmail.CC = CC_ADDRESS
objEmail.BCC = BCC_ADDRESS
objEmail.Subject = subject
objEmail.HTMLBody = emailMessage

If ctrErrors <> 0 Or ctrConcerns <> 0 then
With objEmail.Fields
.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High" ' For Outlook
2003
.Item("urn:schemas:mailheader:X-Priority") = 2 ' For Outlook 2003 also
.Item("urn:schemas:httpmail:importance") = 2 ' For Outlook Express
.Update
End With
End If

objEmail.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/sendusing";) = 2
objEmail.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserver";) = SMTP_SERVER
objEmail.Configuration.Fields.Item
("http://schemas.microsoft.com/cdo/configuration/smtpserverport";) = 25
objEmail.Configuration.Fields.Update

objEmail.Send
Call CheckForError("objEmail.Send")
End Sub
.



Relevant Pages

  • Re: Slightly more complex Copy script - a challenge perhaps?
    ... have a new folder created within ... where the SOURCE is newer than the TARGET. ... ctrErrors, objSourceFile, objTargetFile, emailMessage, _ ... Call DisplaySourceFileInformation ...
    (microsoft.public.scripting.vbscript)
  • Re: need help with a VBScript - copies files behind links to another location
    ... I'd like to figure out how to drag a set of links or an entire folder ... 'The version below supports dragging of a link file to the script icon ... 'the first one is a source folder and the second is the target folder. ... For Each oFile In oFiles ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: need help with a VBScript - copies files behind links to another location
    ... I'd like to figure out how to drag a set of links or an entire folder ... 'The version below supports dragging of a link file to the script icon ... 'the first one is a source folder and the second is the target folder. ... For Each oFile In oFiles ...
    (microsoft.public.scripting.vbscript)
  • Re: need help with a VBScript - copies files behind links to another location
    ... I'd like to figure out how to drag a set of links or an entire folder ... 'The version below supports dragging of a link file to the script icon ... 'the first one is a source folder and the second is the target folder. ... For Each oFile In oFiles ...
    (microsoft.public.windowsxp.general)
  • Re: move my documents automatically
    ... I was hoping if there was some tweak available or a script that would do such ... Docs target as if s/he were the user (by changing registry perhaps? ... security settings for the new Docs folder (Is it just System and the User ...
    (microsoft.public.windowsxp.setup_deployment)