40

I'm trying to automate pushing a file into my users' home directories, but am stuck on a "Permission Denied" error — is thrown on line 6 here, with the CopyFile call.

There are other parts of the script (not shown) that create and copy folder contents using the same source and destination directories, and they work perfectly. It's only when I use CopyFile that it fails.

Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists("H:\Minecraft\.minecraft\options.txt") Then
    fso.CopyFile "C:\Minecraft\options.txt", "H:\Minecraft\.minecraft\"
End If

Set fso = Nothing

H: is a network home directory, to which the current user has full read/write privs.

I've tried adding/removing trailing slashes from the paths, adding "options.txt" to the destination path, removing the false argument... Not sure what else to try. Any thoughts?

FYI, this chunk of code, which comes immediately before the error-prone bit above, executes perfectly every time:

If Not fso.FolderExists("H:\Minecraft\.minecraft\bin\") Then
    If Not fso.FolderExists("H:\Minecraft\.minecraft\") Then
        fso.CreateFolder("H:\Minecraft\.minecraft\")
    End If
    fso.GetFolder("C:\Minecraft\bin\").Copy "H:\Minecraft\.minecraft\"
End If
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Triz
  • 757
  • 2
  • 10
  • 19

8 Answers8

120

I've only ever seen CopyFile fail with a "permission denied" error in one of these 3 scenarios:

  • An actual permission problem with either source or destination.
  • Destination path is a folder, but does not have a trailing backslash.
  • Source file is locked by an application.
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    So it's definitely not #2, it's probably not #3... Permissions on destination are fine, since I can copy other things there. How would I check/fix permissions on the source? (That is, what do they need to be in order to allow a file copy? Just read, right? It has that already...) – Triz Apr 18 '13 at 16:50
  • 1
    Nuked the text file, recreated it, and now the script works. So there must have been some funky permissions on it that I couldn't see. Thanks for helping narrow it down! – Triz Apr 18 '13 at 17:01
  • 32
    "Destination path is a folder, but does not have a trailing backslash." Thanks! – Matt Hanson Jul 23 '13 at 13:55
  • 4
    It was item #2 for me as well. – Bek Dec 28 '13 at 23:17
  • @user2366975 That would have been Erik Lippert. The behavior is actually [documented](https://msdn.microsoft.com/en-us/library/e1wf9e7w.aspx), though. – Ansgar Wiechers Apr 12 '18 at 08:04
  • @Ansgar Wiechers Haha, Thank you, did not want to offend anyone. I personally just find it very frustrating writing code in vbs, which is mostly due to bad tools I have to use + no internet on that specific computer. Easiest things just don't feel intuitive for me and I need to look it up every time, which is not the case for other languages I use (python,even c++ (Qt)). The documentation talks about the backslash thing indeed. – user2366975 Apr 12 '18 at 09:11
  • No offense taken, and I do agree that writing VBScript can be pretty frustrating at times (for instance when it comes to the [different meanings of parentheses](https://blogs.msdn.microsoft.com/ericlippert/2003/09/15/what-do-you-mean-cannot-use-parentheses/)). – Ansgar Wiechers Apr 12 '18 at 09:55
  • @Triz your 2nd comment worked for me, just deleted the destination file, it started working. – Jason Owens Jan 05 '21 at 01:26
6

for me adding / worked at the end of location of folder. Hence, if you are copying into folder, don't forget to put /

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
1

Based upon your source variable (sourcePath = "C:\Minecraft\bin\") I suspect your hard code is pointing at the wrong place

fso.CopyFile "C:\Minecraft\options.txt", destinationPath, false

should be

fso.CopyFile "C:\Minecraft\bin\options.txt", destinationPath

or

fso.CopyFile sourcePath & "options.txt", destinationPath
Dave
  • 8,163
  • 11
  • 67
  • 103
  • Ahh, sorry, that's a good catch, but it's not actually incorrect. I'm using that \bin path for a different part of the script; the options.txt file is indeed located at C:\Minecraft\options.txt. – Triz Apr 18 '13 at 16:28
  • (Have edited the question to reflect the relevant parts more clearly.) – Triz Apr 18 '13 at 16:30
  • An erroneous source path would produce a `File not found` error. – Ansgar Wiechers Apr 18 '13 at 16:32
1

Another thing to check is if any applications still have a hold on the file.

Had some issues with MoveFile. Part of my permissions problem was that my script opens the file (in this case in Excel), makes a modification, closes it, then moves it to a "processed" folder.

In debugging a couple things, the script crashed a few times. Digging into the permission denied error I found that I had 4 instances of Excel running in the background because the script was never able to properly terminate the application due to said crashes. Apparently one of them still had a hold on the file and, thusly, "permission denied."

1

You can do this:

fso.CopyFile "C:\Minecraft\options.txt", "H:\Minecraft\.minecraft\options.txt"

Include the filename in the folder that you copy to.

Tanner Clark
  • 631
  • 1
  • 8
  • 19
0

I have read your problem, And i had the same problem. But af ter i changed some, my problem "Permission Denied" is solved.

Private Sub Addi_Click()
'On Error Resume Next
'call ds
browsers ("false")
Call makeAdir
ffgg = "C:\Users\Backups\user\" & User & "1\data\"
Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.Getfolder("c:\users\Backups\user\" & User & "1\data")
    f.Attributes = 0
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.Copyfile(filetarget, ffgg, True)

Look at ffgg = "C:\Users\Backups\user\" & User & "1\data\", Before I changed it was ffgg = "C:\Users\Backups\user\" & User & "1\data" When i add backslash after "\data\", my problem is solved. Try to add back slash. Maybe solved your problem. Good luck.

Dirk Jan
  • 2,355
  • 3
  • 20
  • 38
0

It's worth checking task manager for any stray wscript.exe tasks that are stuck. It could be one of those that's blocking access to the file.

Dyonn
  • 11
  • 1
0

It is possible that your Antivirus software may be preventing the activity of the script. I encountered this with AVG Antivirus running in silent mode (which means it does not alert you to every protection step it takes, so you get a permission error without realising that AVG is preventing the action). In my case, I invoked verbose mode (switched off silent mode), executed the script and AVG came up with an interception warning, allowing me to train AVG to permit this script to run.

IsNull
  • 11
  • 1