Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to back up PST files from all computers to Server - vol2 2

Status
Not open for further replies.

bence8810

IS-IT--Management
Jul 20, 2005
241
AE
Hi

This thread is a pick-up of a long and old thread I started a few years back, and since then, I re-worked the script, and have a little problem with it.

For reference here is the old thread
thread329-1215138

For a brief note, this is what the script does:

Checks for the username's first letter, and based on that it decides which day the user should do the backup. If its not the given day, it stops.
Then it checks for a Registry key, and checks when the last backup was carried out. If its on the same day, the script exits, as we dont want two backups on the same day.

Once that is done, it has 3 folders to back up from. Since not all users have the same folders, it checks if there is the folder existing in the first place, and if the folder exists, then it checks if there are any pst files inside. If all that passes, then it carries out the backup.

The only problem I have with the script is that If the folder doesnt exist, it will fail somehow. As I am not even close being a pro, actually not even a beginner at scripting, I dont know where I should start looking.

The following works:

There is a folder, but it has no PST file in it.

The following doesnt work:

There is no folder.

It used to work, when I didnt check for the file being there, and by working that out, I somehow broke the other function to check if the folder is there.

My feeling is that in the beginning, it checks for the folder, but once it finds out it not being there, it wont skip automatically to the next folder. It rather goes on, looks for the file, etc etc.

Any suggestions are welcome,

Main contribution in the old thread came from mrmovie and markdmac, thanks both of you. I took a little piece of the code from Skie, again thanks.

This is how the code looks at the moment,

Ben

Code:
Set WshShell = CreateObject("Wscript.Shell")
varUserName = WshShell.ExpandEnvironmentStrings("%username%")

'Set the Dates of the week the backup occures
strDay = 2 'default for unknown configured user

'usernames starting with a given letter will conduct the backup on the given day
'Sunday =1 Saturday =7
Select Case Left(varUserName,1)
Case "a","b","c","d","e"
    	strDay = 2
Case "f","g","h","i","j"
    	strDay = 3
Case "k","l","m","n","o"
    	strDay = 4
Case "p","q","r","s","t"
    	strDay = 5
Case "u","v","w","x","y","z"
    	strDay = 6

End Select

'Check if today is the day we need to run the Backup (checks Registry for recent backup, and username for 1st character)
If CStr(strDay) = CStr(Weekday(Now())) Then
   Set WshShell = CreateObject("Wscript.Shell")
      On Error Resume Next

      'Reads key from registry to see if we have already backed up today
      strTemp = WshShell.RegRead("HKLM\Software\MYCOMPANY\PSTCopyFlag")
      On Error Goto 0
      If CStr(strTemp) <> CStr(Date) Then

	'If we do the backup today, pop up a warning message to the user
	WshShell.Popup "Your emails are being backed up now. You may safely leave the machine, it will proceed automatically", 5
         Set objFSO = CreateObject("Scripting.FileSystemObject")
         strUserName = WshShell.ExpandEnvironmentStrings("%username%")
	 Set fso = createobject("Scripting.FileSystemObject")
         If objFSO.FolderExists("D:\Mail") Then Set oFolder = fso.GetFolder("D:\Mail")
	 pstCount = 0
  	 For Each oFile In oFolder.files
     	     If (Lcase(Right(oFile.Name,3))) = "pst" Then
               pstCount = pstCount + 1    
             End If
         Next
	 If pstCount <> 0 Then objFSO.CopyFile "D:\Mail\*.pst" , "\\backupserver\PST\" & strUserName & "\"
	 If objFSO.FolderExists("C:\Mail") Then Set oFolder = fso.GetFolder("C:\Mail")
	 pstCount = 0
  	 For Each oFile In oFolder.files
     	     If (Lcase(Right(oFile.Name,3))) = "pst" Then
               pstCount = pstCount + 1    
             End If
         Next
	 If pstCount <> 0 Then objFSO.CopyFile "C:\Mail\*.pst" , "\\backupserver\PST\" & strUserName & "\"
	 If objFSO.FolderExists("C:\Documents and Settings\" & strUserName & "\Local Settings\Application Data\Microsoft\Outlook") Then Set oFolder = fso.GetFolder("C:\Documents and Settings\" & strUserName & "\Local Settings\Application Data\Microsoft\Outlook")
	 pstCount = 0
  	 For Each oFile In oFolder.files
     	     If (Lcase(Right(oFile.Name,3))) = "pst" Then
               pstCount = pstCount + 1    
             End If
         Next
	 If pstCount <> 0 Then objFSO.CopyFile "C:\Documents and Settings\" & strUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , "\\backupserver\PST\" & strUserName & "\"
             Set objFSO = Nothing
         'set the FLAG
         WshShell.RegWrite "HKLM\Software\MYCOMPANY\PSTCopyFlag", Date
      End If
End If
 
Why not simply this ?
Code:
...
If CStr(strDay) = CStr(Weekday(Now())) Then
  On Error Resume Next
  'Reads key from registry to see if we have already backed up today
  strTemp = WshShell.RegRead("HKLM\Software\MYCOMPANY\PSTCopyFlag")
  If CStr(strTemp) <> CStr(Date) Then
    'If we do the backup today, pop up a warning message to the user
    WshShell.Popup "Your emails are being backed up now. You may safely leave the machine, it will proceed automatically", 5
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "D:\Mail\*.pst" , "\\backupserver\PST\" & varUserName & "\"
    objFSO.CopyFile "C:\Mail\*.pst" , "\\backupserver\PST\" & varUserName & "\"
    objFSO.CopyFile "C:\Documents and Settings\" & varUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , "\\backupserver\PST\" & varUserName & "\"
    Set objFSO = Nothing
    'set the FLAG
    WshShell.RegWrite "HKLM\Software\MYCOMPANY\PSTCopyFlag", Date
  End If
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Thanks for the reply.

Unfortunately I have PCs which have D drives, some doesnt, some has D:\Mail folder, some doest, some have C:\Mail some doesnt, and worst of all, some has a given folder, but no PST file in it what so ever.

So what I need the script to do is to check if the folder exists, then check if there are any PST files in it, and if both of those are true, then copy them, else go to the next folder.

Thanks for any suggestions,

Ben
 
And what's wrong with my suggestion ?
Did you even try it ?
 
You are right, I havent. Its just that we had that very long thread which I attached above, and there we went around in circles trying to find a way.

Anyways, I am going to give this a shot, and post back tomorrow,

Thanks a lot

Ben
 
Hi

I just tested it, as I was able to get a remote connection to my server, and it does work. I dont know why we had that long long thread about this before :(

One thing is still wrong though, and that is that files are not overwritten when after it has been copied once. How can I tell this script to actually overwrite the files every time?

Thanks a lot, you are a lifesaver,

Ben
 
Add a third parameter to each CopyFile call:
objFSO.CopyFile source, destination[!], True[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, looks good. I will test it for a few days/week, then report back.

I just delegated it through GPO.

Great help, appreciate it,

Ben
 
Hi

I am not sure this is because of how the script works, but I would like to ask your expert opinion in case its related.

The script runs perfect, and its copying PST files from all locations, just great, but its having a problem with all PST files that are named archive.pst. All of the archive.pst files are at the size 256KB, and this is true for all users. I am not sure why this is, as if the name of PST is different, like archive2.pst, it gets copied just perfect.

Any clues would be welcomed,

Thanks

Ben
 
I am following another script covering this… but my file path seems to be wrong. It just wont work and I’m not sure why.

ON Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Documents and Settings\shanet\Local Settings\Application Data\Microsoft\Outlook\*.pst" , \\sql1\Backups\PST\shanet

I’m just going to start simple and add, days of the week to back up, making it user agnostic ect later… lol I still can’t get past the file path part lol

Can anyone see any problem with this… or why it’s not working? Just so you know when I created a test

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "c:\testa" , \\sql1\Backups\PST\shanet

that seemed to work fine.

It’s just the "C:\Documents and Settings\shanet\Local Settings\Application Data\Microsoft\Outlook\*.pst"

That wont work… anyone have any idea why??

Thanks guys.
 
Sorry guys... it's all good... it just seems to work now
 
OK well now if I try to make it user agnostic by introducing the " & varUserName & " variable (is that what’s it called?) That’s it dies and wont copy… am I missing something that will work for a simple scrip but won’t recognize something more complex??

ON Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Documents and Settings\" & varUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , \\sql1\Backups\PST\shanet

Thanks all
 
[0] You should open your own thread rather than posting question to other person's thread.
[1] It is not variable or whatever. Your unc should be quoted.
>objFSO.CopyFile "C:\Documents and Settings\" & varUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , \\sql1\Backups\PST\shanet
[tt]objFSO.CopyFile "C:\Documents and Settings\" & varUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , [red]"[/red]\\sql1\Backups\PST\shanet[red]"[/red][/tt]
 
Thanks for that mate... i was going to open a new thread, but it is all the same thing really... anyway thanks again :)
 
Since there are some more corrections to the line and that risk of leaving a misleading open-end, I supplement this rather than post to your new thread.

[0] The path _must_ be quoted, there should be no doubt about it.

[1] Since you have wildcard in the source, you destination folder should have a trailing backslash to make it clear that it is a folder.

[2] If variable username is put in the source, it seems, from the face of it, you mean variable folder name as well in accordance with the source.

[3] If the destined filename exists, you might need to supply the overwrite parameter true.

Hence, the line could be this.
[tt]
objFSO.CopyFile "C:\Documents and Settings\" & varUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , "\\sql1\Backups\PST\[red]" & varUserName & "\", true[/red]
[/tt]
 
Hey PHV… I tried your script as

Set WshShell = CreateObject("Wscript.Shell")
varUserName = WshShell.ExpandEnvironmentStrings("%username%")

If CStr(strDay) = CStr(Weekday(Now())) Then
'Reads key from registry to see if we have already backed up today
strTemp = WshShell.RegRead("HKLM\Software\MYCOMPANY\PSTCopyFlag")
If CStr(strTemp) <> CStr(Date) Then
'If we do the backup today, pop up a warning message to the user
WshShell.Popup "Your emails are being backed up now. You may safely leave the machine, it will proceed automatically", 5

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Documents and Settings\" & varUserName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst" , "\\sql1\Backups\PST\" & varUserName & "\", True

Set objFSO = Nothing
'set the FLAG
WshShell.RegWrite "HKLM\Software\MYCOMPANY\PSTCopyFlag", Date
End If
End If


And it didn’t work

Any ideas where I’m going wrong??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top