tags:

views:

450

answers:

2

Below is the VBScript code. If the file/s or folder exist I get scripting error, "File already exists".

  • How to fix that?
  • How to create folder only if it does not exist and copy files only that are new or do not exist in source path?
  • How to insert the username (Point 1) after "Welcome" and at (Poin 3) instead of user cancelled?
  • Can the buttons be changed to Copy,Update,Cancel instead of Yes,No,Cancel? (Point 2)

The code:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
Message = "       Welcome to the AVG Update Module" & vbCR '1*
Message = Message & "       *****************************" & vbCR & vbCR
Message = Message & "        Click Yes to  Copy   Definition Files" & vbCR & vbCR
Message = Message & "                            OR  " & vbCR & vbCR
Message = Message & "        Click  No to Update  Definition Files." & vbCR & vbCR
Message = Message & "        Click  Cancel (ESC) to Exit." & vbCR & vbCR
X = MsgBox(Message, vbYesNoCancel, "AVG Update Module") '2*
'Yes Selected Script
If X = 6 then
    objFSO.FolderExists("E:\Updates")
    if TRUE then objFSO.CreateFolder ("E:\Updates")
    objFSO.CopyFile "c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.*",
    "E:\Updates\" ,   OverwriteFiles
    MsgBox "Files Copied Succesfully.", vbInformation, "Copy Success"
End If
'No Selected Script
If X = 7 then
    objFSO.FolderExists("Updates")
    if TRUE then objFSO.CreateFolder("Updates")
    objFSO.CopyFile "E:\Updates\*.*", "Updates", OverwriteFiles
    Message =  "Files Updated Successfully."  & vbCR & vbCR
    Message = Message & "Click  OK to Launch AVG GUI." & vbCR & vbCR
    Message = Message & "Click  Cancel (ESC) to Exit." & vbCR & vbCR
    Y = MsgBox(Message, vbOKCancel, "Update Success")
    If Y = 1 then
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.Run chr(34) & "C:\Progra~1\avg\avg8\avgui.exe" & Chr(34), 0
        Set WshShell = Nothing
    End if
    If Y = 3 then WScript.Quit
End IF
'Cancel Selection Script
If X = 2 then
    MsgBox "No Files have been Copied/Updated.", vbExclamation, "User Cancelled" '3*
End if

+2  A: 

How to create folder only if it does not exist

This your code:

objFSO.FolderExists("E:\Updates")
if TRUE then objFSO.CreateFolder ("E:\Updates")

simply calls the FolderExists and CreateFolder methods in sequence (CreateFolder is always called because the if TRUE condition evaluates to True) and is equal to:

objFSO.FolderExists("E:\Updates")
objFSO.CreateFolder ("E:\Updates")

You want to call CreateFolder depending on the return value of the FolderExists method:

If Not objFSO.FolderExists("E:\Updates") Then
   objFSO.CreateFolder "E:\Updates"

and copy files only that are new or do not exist in source path?

Neither VBScript nor the FileSystemObject object have this functionality. However, it is possible to call an external tool that can do that, such as xcopy, from your script using the WshShell.Run method. I guess you need something like this:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "xcopy c:\Docume~1\alluse~1\applic~1\avg8\update\download\*.* E:\Updates\ /D", , True

How to insert the username (Point 1)

Concatenate the message text with the strUserName variable value:

Message = "       Welcome " & strUserName & " to the AVG Update Module" & vbCR
...
MsgBox "No Files have been Copied/Updated.", vbExclamation, strUserName & " Cancelled"

Can the buttons be changed to Copy,Update,Cancel Instead of Yes,No,Cancel?(Point 2)

No, VBScript's built-in MsgBox function does not support custom buttons. There're workarounds though: you could create your custom message box using an HTA (HTML application) or use the InputBox function to prompt the user for the task they wish to perform. You can find examples here.


I'd also like to note that you can improve your script by using the Select Case statement to check the MsgBox return value instead of multiple If...Then...End If statements. Also, it's a bad practice to use "magic numbers" like 6 or 7 - use the appropriate constants instead. For example:

Select Case X
  Case vbYes
     ...
  Case vbNo
     ...
  Case Else ' vbCancel
     ...
End Select
Helen
A: 

When you say

"copy files only that are new or do not exist in source path?"

do you mean you only want to copy files from the source directory to the destination directory if they do not exist in the destination? If so this will accomplish that

Const SourceFolder = "C:\Test1\"
Const DestinationFolder = "C:\Test2\"

Set fso = CreateObject("Scripting.FileSystemObject")
'Get a collection of al the files in the source directory
Set fileCol = fso.GetFolder(SourceFolder).Files

'Loop through each file and check to see if it exists in the destination directory
For Each objFile in fileCol
    If NOT fso.FileExists(DestinationFolder & objFile.Name) Then
        'If the file does not exist in the destination directory copy it there.
        objFile.Copy DestinationFolder
    Else
     If objFile.DateLastModified > fso.GetFile(DestinationFolder & objFile.Name).DateLastModified Then
      'If the file is newer than the destination file copy it there
      objFile.Copy DestinationFolder, True
     End If
    End If
Next 
Set fileCol = Nothing
Set fso = Nothing

Added the requested date check.

Tester101
What about replacing older file versions ("copy files only that are new")?
Helen
Do you want to compare the Date Created, Date Last Modified, or Date Last Accessed?
Tester101
Thanks Helen and Tester101 for the help.How to automate copying by adding if strUserName = DARIO than goto 'Yes selected Script else 'No selected script