Click here to Skip to main content
15,894,240 members

Copy folder and subfolder files with certain file extension from more than one location to %appdata% variable path

cHl Security asked:

Open original thread
Using Vbscript, I am trying to copy files with certain file extensions such as [.tmp, .dat, .txt] from folder and subfolder in more than on location to %AppData% destination user an elevated privillege. Below is the sample of my code that prompts Permission Denined in line 40 of my code

' 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 


What I have tried:

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 mutiple BaseFolder:
strBaseFolder = "C:\"
strBaseFolder = "D:\"
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 as well.

I'd appreciate if someone can provide me a great help my helping me with working code to my problem as a solution.

Thank You!
Tags: VBScript, Windows

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900