Copy file from script directory to All Users' desktop

3.1k views Asked by At

I've written the script below but when I run it (using PrimalScript for troubleshooting) I get the error 'Permission denied'. I'm admin on this device and I get the same error when I run the script elevated.

Here is the script:

Dim WshShell, strCurDir, File, strDesktop

Set WshShell = CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("AllUsersDesktop")
Set ofso = CreateObject("Scripting.FileSystemObject")
strPath = ofso.GetParentFolderName(WScript.ScriptFullName)
File = "pwsafe.psafe3"
strCurDir = ofso.BuildPath(strPath, File)

ofso.CopyFile strCurDir , strDesktop , OverwriteExisting

What am I doing wrong?

2

There are 2 answers

1
totalyscrewedup On BEST ANSWER

while my last comment showed the script that doesn't work, the script below is combination of Sergio's and Lankymart's work. Wanted to mention them both since they deserve the credit. Here is the working script and I hope someone else makes use of it.

Dim ofso, oshell, oproc, strDesktop, overwrite
Set ofso = CreateObject("Scripting.FileSystemObject")
Set oshell = CreateObject("WScript.Shell")
Set oproc  = oshell.Environment("Process")
strDesktop = oproc.Item("ALLUSERSPROFILE") & "\Desktop\"
overwrite = True
ofso.CopyFile "pwsafe.psafe3" , strDesktop , overwrite

Thank you both.

6
Sergio Internicola On

You must set a process:

Dim ofso, oshell, oproc, strDesktop, overwrite
Set ofso = CreateObject("Scripting.FileSystemObject")
Set oshell = CreateObject("WScript.Shell")
Set oproc  = oshell.Environment("Process")
strDesktop = oproc.Item("UserProfile") & "\Desktop\"
overwrite = True
ofso.CopyFile "pwsafe.psafe3" , strDesktop , overwrite