Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
See more:
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!
Posted
Updated 5 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

Open a command prompt (Windows console), and type echo %HOMEPATH%; you will find out that this environment variable does not include the drive. Eg., on my system, it returns \Users\Account.
You could try
strBaseFolder = "C:\" + homePath + "\AppData\Local\Temp"
and see whether it solves your problem.
   
Comments
cHl Security 4 days ago
   
Pls I am trying to copy from c:\ folder and subfolder but I am getting permission denied.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You have nothing handling errors in your code at all and your code WILL run into situations where errors will be thrown. One fine example of this is the folder at the root of the drive called "System Volume Information". Even if you run your code elevated, you will have no access to the content of this folder. You'll get "Access Denied".

Rewrite your code so that if it tries to access folders where you don't have permissions, it will gracefully skip that folder and continue on.
   
Comments
cHl Security 4 days ago
   
Can you kindy illustrate your explanations or give me a code example?
Dave Kreskowiak 4 days ago
   
No, because I have my own code to write and short deadlines to go with it.

You can, however, start reading up on On Error[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100