VBScript - Custom BackUp

Hi I am a vbscript beginner but need a script that will do the following.
Backup files to a network share from 4 different locations
1) My Documents
2) Favourites
3) Desktop
4) C:\Email
5  c:|BackUp
to a network  drive - lets say \\servername\foldername (and then My Document/Favourites/Desktop etc etc)
I need it to show progress (which can be minimised) and report any failures or success at the end of back-up.
I also need it to prompt the user to exit outlook at the start .
This is probobly a relatively easy one but scripting is my achilles heal.

thanks to anyone who helps.
William_ConwayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Personally, I think you'd be better off using a product like robocopy.
It has the capability to mirror the source and target.  To do so with vbscript would require a fair amount of code...
0
William_ConwayAuthor Commented:
Providing it does what it says on the tin (as above) I'm not too bothered.
0
sirbountyCommented:
Robocopy is located here...http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en

Kind of a combination of vbscript and the robocopy tool, set up your script like this:

'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=.ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\"
  Desktop=.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\"
  Favorites=.ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\"
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget="\\Server\Backups\" & username
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

For Each Item in arrFolders
  objShell.Run "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code


The arguments do this:
 /tee = display onscreen status (as well as log to file)
/mir = mirror the source to the directory (if a source file is deleted it will be deleted from the backup - if you don't want this turn it off and use /e instead
 /r:3 = retry open files 3 times, then give up and move on...
 /zb = restartable backup mode - will retry a file if a network hiccup occurs.  Will attempt a copy in 'backup' mode - if the file is in use
 /log+" & strTarget & ".log" = records the log file at the root of the target in the format of username.log
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

William_ConwayAuthor Commented:
Its giving the outlook prompt - creating the backup folder but then it doesn't copy anything - no error
A dos screen pops up momentarily after the outlook prompt but nothing else.
any ideas?
0
sirbountyCommented:
Did it create the log file?
0
William_ConwayAuthor Commented:
No log file
modified script as below

'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=.ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\"
  Desktop=.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\"
  Favorites=.ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\"
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget="\\Server\Share\BackUp\" & username
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

For Each Item in arrFolders
  objShell.Run "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code

0
sirbountyCommented:
use this for/next and let me know what the output is...

For Each Item in arrFolders
  wscript.echo "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
  objShell.Run "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
Next
0
William_ConwayAuthor Commented:
robocopy c:\Documents and Settings\(My Username)\My Documents\ \\server\share\backup\(My Username) /tee /mir /r:3 /zb /log+\\server\share\backup\(My Username).log

followed by a head scratch and a d'oh
0
William_ConwayAuthor Commented:
Then the same message for favourites and desktop with keyword replaced i.e my documents = favorites - but not for c:\Backup or c:\email
0
sirbountyCommented:
Bingo - spaces in the path... :^)

Replace with this:

For Each Item in arrFolders
  objShell.Run "robocopy " & chr(34) & Item & chr(34) & " " & chr(34) & strTarget & chr(34) & " /tee /mir /r:3 /zb /log+" & chr(34) & strTarget & ".log" & chr(34)
Next
0
William_ConwayAuthor Commented:
Apologies c:\backup and c:\wemail does actually show up also
0
William_ConwayAuthor Commented:
Still no joy - no log no copies.
Will
0
sirbountyCommented:
replace this block again...post the output (just one instance is fine)

For Each Item in arrFolders
  wscript.echo "robocopy " & chr(34) & Item & chr(34) & " " & chr(34) & strTarget & chr(34) & " /tee /mir /r:3 /zb /log+" & chr(34) & strTarget & ".log" & chr(34)
'  objShell.Run "robocopy " & chr(34) & Item & chr(34) & " " & chr(34) & strTarget & chr(34) & " /tee /mir /r:3 /zb /log+" & chr(34) & strTarget & ".log" & chr(34)
Next

0
sirbountyCommented:

It should read:

robocopy "c:\Documents and Settings\(My Username)\My Documents\" "\\server\share\backup\(My Username)" /tee /mir /r:3 /zb /log+"\\server\share\backup\(My Username).log"
0
William_ConwayAuthor Commented:
It reads as it should  (As Above)- so the next thing i tried was manually typing it in
I get error : No Destination Directory Specified
0
sirbountyCommented:
Typing it manually, try this...it may just be that it needs that last trailing \

robocopy "c:\Documents and Settings\(My Username)\My Documents\" "\\server\share\backup\(My Username)\" /tee /mir /r:3 /zb /log+"\\server\share\backup\(My Username).log"

^ added the \ just before the /tee
0
William_ConwayAuthor Commented:
From the below it looks to me like its not setting the source and destination correctly - not sure how to correct this though.
Rgds,
Will

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

P:\>c:

C:\>robocopy "c:\Documents and Settings\kw3199\My Documents\" "\\server\share\backup\username\" /tee /mir /r:3 /zb /log+"\\server\share\backup\username.log"

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows     ::     Version XP010
-------------------------------------------------------------------------------

  Started : Thu Mar 22 16:38:41 2007

   Source : c:\Documents and Settings\username\My Documents" \server\sharebackup\username"\
     Dest : C:\log+\server\share\backup\username.log\

    Files : *.*

  Options : *.* /TEE /S /E /COPY:DAT /PURGE /MIR /ZB /R:3 /W:30

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

0
sirbountyCommented:
Try this one...

For Each Item in arrFolders
  wscript.echo  "robocopy """ & Item & """ """ & strTarget & """ /tee /mir /r:3 /zb /log+"""" & strTarget & ".log"""
  objShell.Run "robocopy """ & Item & """ """ & strTarget & """ /tee /mir /r:3 /zb /log+"""" & strTarget & ".log"""
Next
0
William_ConwayAuthor Commented:
line 22 char 117 unterminated string constant
0
sirbountyCommented:
Thought Id checked that...try this..

wscript.echo "robocopy """ & Item & """ """ & strTarget & """ /tee /mir /r:3 /zb /log+""" & strTarget & ".log"""
0
William_ConwayAuthor Commented:
No destination provided and the source seems to be the source plus destination.
0
sirbountyCommented:
It really shouldn't be this difficult.
I just did something similar a couple of weeks ago at work... : (


'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\" & chr(34)
  Desktop=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\" & chr(34)
  Favorites=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\" & chr(34)
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget=chr(34) & "\\Server\Share\BackUp\" & username & chr(34)
strLog=chr(34) & "\\Server\Share\BackUp\" & username & ".log" & chr(34)
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

For Each Item in arrFolders
  objShell.Run "robocopy " & Item & strTarget & " /tee /mir /r:3 /zb /log+" & strLog
'If this fails to work, please comment the above line (or remove it) and uncomment the next line: and let me know the results
'  objShell.Run "robocopy " & Item & strTarget & " /mir /r:3 /zb"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code
0
William_ConwayAuthor Commented:
Bad File Name or Number i echodied the strtarget variable and it is right the problem seems to be in the below line
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)
0
sirbountyCommented:
Alter it to this...

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  objFSO.CreateFolder(strTarget)
End If

see if it errors at the same point (should see the echo line above)
0
William_ConwayAuthor Commented:
Error remains pointing at the below line - I do see the echo and that appears correct.

  objFSO.CreateFolder(strTarget)
0
sirbountyCommented:
What is strTarget set to?
Could it be that the 'parent' isn't created yet?

strTarget is set to \\server\share\backup\username

If backup isn't there, it'll fail...
0
William_ConwayAuthor Commented:
have double checked everything the paths are definetly right and the parent folde rexist - even if i create the username folder i still get that error.
0
sirbountyCommented:
<Smack> - sorry...I think I see the error...sorry for the trouble...remove the paren:

objFSO.CreateFolder strTarget

If that doesn't do it, then I won't feel so bad...but I think it will.
If not, try
set objFolder=objFSO.CreateFolder (strTarget)
0
William_ConwayAuthor Commented:
Still bad filename or number
'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\" & chr(34)
  Desktop=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\" & chr(34)
  Favorites=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\" & chr(34)
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)

wscript.echo strtarget

strLog=chr(34) & "\\server\share\BackUp\" & username & ".log" & chr(34)

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
set objFolder=objFSO.CreateFolder (strTarget)
End If


For Each Item in arrFolders
  objShell.Run "robocopy " & Item & strTarget & " /tee /mir /r:3 /zb /log+" & strLog
'If this fails to work, please comment the above line (or remove it) and uncomment the next line: and let me know the results
'  objShell.Run "robocopy " & Item & strTarget & " /mir /r:3 /zb"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code
0
sirbountyCommented:
This line is being changed for your environment, correct?
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)
0
William_ConwayAuthor Commented:
Yeah definetly even when you echo it it appears as it should be. I think its going wrong where is differentiates between the source and the destination. Because the destination is always blank or only a few letters and the source is always more than it should be
i.e source c:\backup \\server\share and then destination could be username or something similar. It doesnt seem to see where one ends and the other begins.
0
sirbountyCommented:
I don't mind working at this until we get it, if you don't....
I know it's possible cause I just did this for a data migration last month.

That said, we'll start with a slimmed down version at first to see if we can get this working...

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")
Username="Testing"
Email="C:\Email\"

strTarget=" " & chr(34) & "\\server\share\BackUp\" & username & chr(34)

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  set objFolder=objFSO.CreateFolder (strTarget)
End If

objShell.Run "robocopy " & Email & strTarget & " /tee /mir /r:3 /zb /log+C:\Testing.log"

Set objFSO=Nothing
Set objShell=Nothing
'End code
0
William_ConwayAuthor Commented:
bad file name or number line 10 char 3
Befor that i get an echo of "\\Server\Share\Backup\testing"

0
sirbountyCommented:
With that version, the only echo you should get would be
wscript.echo strTarget & " doesn't exist...creating it..."
0
sirbountyCommented:
Do these 3 lines work? (change \\server share below)

Dim objShell:Set objShell=CreateObject("Wscript.Shell")
objShell.Run "robocopy C:\Email \\server\share\backup\testing"
Set objShell=Nothing
0
William_ConwayAuthor Commented:
That worked on folder but not subfolder - so it created the testing folder copied all files to it - but didn't copy any subfolders.
0
sirbountyCommented:
So, this should copy all...

Dim objShell:Set objShell=CreateObject("Wscript.Shell")
objShell.Run "robocopy C:\Email \\server\share\backup\testing /mir"
Set objShell=Nothing

I think the problem has been in the log file...let me look at that bit again..
0
William_ConwayAuthor Commented:
That worked for folder and sub-folders
getting closer!!!
0
sirbountyCommented:
Let's go back to the last full version and just exclude the log file for now...I think that may be the problem...

'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\" & chr(34)
  Desktop=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\" & chr(34)
  Favorites=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\" & chr(34)
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)

wscript.echo strtarget

'strLog=" " & chr(34) & "\\server\share\BackUp\" & username & ".log" & chr(34)

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
set objFolder=objFSO.CreateFolder (strTarget)
End If


For Each Item in arrFolders
  objShell.Run "robocopy " & Item & strTarget & " /tee /mir /r:3 /zb"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code
Admin Edit
0
William_ConwayAuthor Commented:
back to bad filename or number on the below line
set objFolder=objFSO.CreateFolder (strTarget)
0
sirbountyCommented:
So, our culprit is here somewhere... : (

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)
If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  wscript.echo "Creating " & strTarget
set objFolder=objFSO.CreateFolder (strTarget)
End If

Can you post a screen shot of this?
www.ee-stuff.com is a sister-site upload location..
0
William_ConwayAuthor Commented:
New Code
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
strTarget=chr(34) & "\\amstw23file1\william_conway\BackUp\Username"
If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  wscript.echo "Creating " & strTarget
set objFolder=objFSO.CreateFolder (strTarget)
End If

0
sirbountyCommented:
Looks like there's a missing quote...
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
strTarget=chr(34) & "\\amstw23file1\william_conway\BackUp\Username"  & chr(34) '<<<added this last chr(34)
If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  wscript.echo "Creating " & strTarget
set objFolder=objFSO.CreateFolder (strTarget)
End If
0
William_ConwayAuthor Commented:
Same except line 6 char 1 now instead.
0
sirbountyCommented:
Change strTarget to:

strTarget=chr(34) & "\\amstw23file1\william_conway\BackUp\Username\" & chr(34)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
William_ConwayAuthor Commented:
Same error Im afraid
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.