Skip to content

Instantly share code, notes, and snippets.

@chlserver
Created February 14, 2020 21:56
Show Gist options
  • Save chlserver/16fe67a9978250e57c6de063fb758b7b to your computer and use it in GitHub Desktop.
Save chlserver/16fe67a9978250e57c6de063fb758b7b to your computer and use it in GitHub Desktop.
Copy Folder and SubFolder Files With Certain File Extension From More Than One Location to %AppData% Variable Path.
Using Vbscript, I am trying to copy files with certain file extensions such as [.tmp, .dat, .txt] from folder and subfolder in more than one location to %AppData% destination user an elevated privillege. Below is the sample of my code that prompts Permission Denined
' 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:\"
strDestFolder = homePath + "\AppData\Roaming\Shell32"
' 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
But, using Base Folder:
strBaseFolder = homePath + "\AppData\Local\Temp"
it doesn't prompt any error but, when I checked the Destination Folder, I discovered that no file has been copied into it.
Also, when I tried merge multiple BaseFolder:
strBaseFolder = "C:\"
strBaseFolder = "D:\"
strBaseFolder = homePath + "\AppData\Local\Temp"
it also doesn't prompt any error but, when I checked the Destination Folder, I discovered that no file has been copied into it as well.
I'd appreciate if someone can help me with a solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment