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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple VBS Script to backup laptop files to USB Memory stick or Network file share

Status
Not open for further replies.

ghath

Technical User
Oct 2, 2012
9
0
0
GB
Hi Guys,

I want to put a simple VBS script on laptop users desktops to run a backup to a choice of locations: a) USB memory stick (script to check if connected or b) Network file share.

It would be good to set a silent logon script to check when the last backup was run, and to where and if it's over a month since last backup then prompt to run the above.

I've made a start and added some code to browse to the backup/restore locations but I need some help on actually copying the data? I've heard that X COPY is the way to go, can anybody help me with the code required to copy the selected data across?

Really need somebody to hold my hand as I'm new to VBS...Thanks


Option Explicit

' Flags for the options parameter
Const BIF_returnonlyfsdirs = &H0001
Const BIF_dontgobelowdomain = &H0002
Const BIF_statustext = &H0004
Const BIF_returnfsancestors = &H0008
Const BIF_editbox = &H0010
Const BIF_validate = &H0020
Const BIF_browseforcomputer = &H1000
Const BIF_browseforprinter = &H2000
Const BIF_browseincludefiles = &H4000

Dim file

file = BrowseForFolder( _
"Select a file or folder to copy", _
BIF_returnonlyfsdirs + BIF_browseincludefiles, _
"")
If file = "-5" Then
WScript.Echo "Not possible to select files in root folder"
Else
If file = "-1" Then
WScript.Echo "No object selected; Cancel clicked"
Else
WScript.Echo "Object: ", file
End If
End If

' Using the shell's BrowseForFolder method to
' return the full path to the selected object
' title = Text shown in the dialog box
' flag = One of the values for controlling the
' BrowseForFolder behavior
' dir = Preselected directory (can be "")
Function BrowseForFolder(title, flag, dir)
On Error Resume Next

Dim oShell, oItem, tmp

' Create WshShell object.
Set oShell = WScript.CreateObject("Shell.Application")

' Invoke Browse For Folder dialog box.
Set oItem = oShell.BrowseForFolder(&H0, title, flag, dir)
If Err.Number <> 0 Then
If Err.Number = 5 Then
BrowseForFolder= "-5"
Err.Clear
Set oShell = Nothing
Set oItem = Nothing
Exit Function
End If
End If

' Now we try to retrieve the full path.
BrowseForFolder = oItem.ParentFolder.ParseName(oItem.Title).Path

' Handling: Cancel button and selecting a drive
If Err<> 0 Then
If Err.Number = 424 Then ' Handle Cancel button.
BrowseForFolder = "-1"
Else
Err.Clear
' Handle situation in which user selects a drive.
' Extract drive letter from the title--first search
' for a colon :)).
tmp = InStr(1, oItem.Title, ":")
If tmp > 0 Then ' A : is found; use two
' characters and add \.
BrowseForFolder = _
Mid(oItem.Title, (tmp - 1), 2) & "\"
End If
End If
End If

Set oShell = Nothing
Set oItem = Nothing
On Error GoTo 0
End Function


 
Thanks for your response.

The Folder.CopyHere method looks ideal but I'm unsure how to incorperate it into my existing code? My VBS skills aren't great, any tips? Cheers

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top