Archive

Posts Tagged ‘vbscript’

Copy Favorites folder to Home Directory

February 15th, 2010 No comments

This script is designed to copy a user’s favorites folder from their local profile to their home directory. It is useful when you are not implementing roaming profiles and your users migrate to various computers in the office.

Save this script as a .vbs file, you can either incorporate it into an Active Directory Group Policy or execute the script with the cscript command.

As always this script comes with no written or implied warranty. Use at your discretion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'Created by Dustin Berube on 1/17/2009

'This script will copy the users favorites to the users folder on the server.

Option Explicit
 
On Error Resume Next
 
Dim objShell, objFSO, objNet, strHomeShare, strUserProfile
 
'Create the shell and file system objects
Set objShell = WScript.CreateObject("WScript.Shell")
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objNet = WScript.CreateObject("WScript.Network")
 
'Use the Shell Object to retrieve system environment variables
strHomeShare = "\\Server\Share\" & objNet.UserName
strUserProfile = objShell.SpecialFolders("Favorites") 'Local

'Exit if the variables are blank
If strHomeShare = "" Or strUserProfile = "" Then
   objShell.LogEvent 2, "Cannot copy favorites to the server" & vbCRLF & _
   "Error Description: Variables not set"
   Wscript.Quit
End If
 
If objFSO.FolderExists(strHomeShare & "\Favorites") Then
   objFSO.DeleteFolder strHomeShare & "\Favorites"
End If
 
objFSO.CopyFolder strUserProfile,strHomeShare & "\Favorites"
 
If Err Then
   objShell.LogEvent 1, "Cannot copy favorites from " & strUserProfile & _
   " to " & strHomeShare & "\Favorites" & vbCRLF & "Error Description: " & _
   Err.Description
   Err.Clear
Else
   objShell.LogEvent 0, "Favorites successfully copied from " & _
   strUserProfile & " to " & strHomeShare & "\Favorites"   
End If
 
Set objShell = Nothing
Set objFSO = Nothing
Set objNet = Nothing
Categories: Scripting Tags:

VBScript – Delete files Older than X days

February 9th, 2009 No comments

This quick and dirty script will delete all files in a supplied directory older than X number of days. I have used this to clear still images from a surveillance system so it doesn’t fill the entire hard drive.

Warning this script is intended to delete data from a hard drive. Use at your own discretion I am not responsible for any data loss experienced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'Created by Dustin Berube 2/9/2009
'Last modified 2/9/2009

Dim fso, f, f1, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("path\to\folder")
Set fc = f.Files
For Each f1 in fc
If DateDiff("d", f1.DateLastModified, Now) > < # of days> Then
f1.Delete
End If
Next
Set fso = Nothing
Set f = Nothing
Set fc = Nothing

Save this as a .vbs file and execute by running cscript filename.vbs

Categories: Scripting Tags: