Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied on CopyFile in VBS

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
like image 378
Triz Avatar asked Apr 17 '13 22:04

Triz


4 Answers

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.
like image 81
Ansgar Wiechers Avatar answered Nov 19 '22 09:11

Ansgar Wiechers


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

like image 31
Surabhi Sugandha Avatar answered Nov 19 '22 09:11

Surabhi Sugandha


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
like image 25
Dave Avatar answered Nov 19 '22 08:11

Dave


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."

like image 39
CargoPantsMan Avatar answered Nov 19 '22 08:11

CargoPantsMan