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

How to backup certain file types from an entire multiple drive or folder using vbscript?

Status
Not open for further replies.

Oracle911

Systems Engineer
Jan 31, 2020
2
0
0
US
Greetings Viewers,

I'd Preciate if somone can provide a new working vbscript that can copy many file types such as:
"mp3", "dat", "txt"
and so on recrisively from more than one location or folder and subfolders such as:
"c:\", "%AppData%
and so on into a designated folder for example:
"%AppData%\backup"

Below is the code I am currntly working on but its not working and doesn't seem to be solving my problem.

Require variables to be defined
Option Explicit

' Global variables
Dim strBaseFolder
Dim strDestFolder
Dim objFSO
Dim objFolder
Dim objFile

Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim homePath
homePath = objWShell.expandEnvironmentStrings("%HOMEPATH%")

' Define folders to work with
strBaseFolder = "C:\"
strBaseFolder = "D:\"
strBaseFolder = "F:\"
strBaseFolder = homePath + "\AppData
strDestFolder = homePath + "\AppData\Roaming\backup"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Exit if base folder does not exist
If Not objFSO.FolderExists(strBaseFolder) Then
Wscript.Echo "Missing base folder : """ & strBaseFolder & """"
Wscript.Quit
End If

' Exit if dest folder does not exist
If Not objFSO.FolderExists(strDestFolder) Then
Wscript.Echo "Missing dest folder : """ & strDestFolder & """"
Wscript.Quit
End If

' Look at each subfolder of base folder
For Each objFolder In objFSO.GetFolder(strBaseFolder).SubFolders
' Continue if we want this folder
If IncludeFolder(objFolder) Then
' Check each file in this folder
For Each objFile In objFolder.Files
' Continue if we want this file
If IncludeFile(objFile) Then
' Copy the file
'Wscript.Echo "Copying File :""" & objFile.Path & """"
objFile.Copy strDestFolder & "\" & objFile.Name
End If
Next
End If
Next

' Logic to determine if we process a folder
Function IncludeFolder(objFolder)
' Exclude certain folder names
Select Case LCase(objFolder.Name)
Case "exchange", "hr_daily_terminations", "pay", "terminations", "work folder"
IncludeFolder = False
Case Else
IncludeFolder = True
End Select
End Function

' Logic to determine if we process a file
Function IncludeFile(objFile)
IncludeFile = False
Select Case LCase(objFSO.GetExtensionName(objFile.Path))
' Include only these extensions
Case "txt", "dat", "tmp"
' Include only files dated today
If DateDiff("d", objFile.DateLastModified, Now) = 0 Then
IncludeFile = True
End If
End Select
End Function

Thank for your helping in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top