Discussion:
Delete folder with spaces in path
(too old to reply)
p***@designexpress.eu
2016-05-05 07:01:59 UTC
Permalink
I'm trying to delete a folder in the 'Program Files'. But I don't see why this wont work...
Here's the code I'm trying:

Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub

DeleteAFolder("C:\Program Files\TestFolder")


Result: "Permission denied"

I also tried:

Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub

DeleteAFolder("""C:\Program Files\TestFolder""")


Result: "Path not found"


Any ideas?
Auric__
2016-05-05 07:37:54 UTC
Permalink
Post by p***@designexpress.eu
I'm trying to delete a folder in the 'Program Files'. But I don't see why this wont work...
Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub
DeleteAFolder("C:\Program Files\TestFolder")
Result: "Permission denied"
Your script requires admin privileges.
Post by p***@designexpress.eu
Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub
DeleteAFolder("""C:\Program Files\TestFolder""")
Result: "Path not found"
This version is looking for a folder named "C:\Program Files\TestFolder",
*including the quotation marks*. The first version is correct.
Post by p***@designexpress.eu
Any ideas?
*Why* are you deleting something in Program Files?
--
- Are you questioning my judgment?
- Is your judgment so perfect that it may never be questioned?
p***@designexpress.eu
2016-05-05 12:41:33 UTC
Permalink
Post by p***@designexpress.eu
I'm trying to delete a folder in the 'Program Files'. But I don't see why this wont work...
Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub
DeleteAFolder("C:\Program Files\TestFolder")
Result: "Permission denied"
Sub DeleteAFolder(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
End Sub
DeleteAFolder("""C:\Program Files\TestFolder""")
Result: "Path not found"
Any ideas?
How can I add admin privileges to the script?
Evertjan.
2016-05-05 12:52:04 UTC
Permalink
Post by p***@designexpress.eu
How can I add admin privileges to the script?
You cannot, a script is just a piece of text.

You can have 'admin privileges' for the execution engine by being an
adminstrator, or imposing to be one, who executes such engine, imho.

But that "how" seems to be OT here, Peter.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
JJ
2016-05-05 23:09:28 UTC
Permalink
Post by p***@designexpress.eu
How can I add admin privileges to the script?
You can't, as already mentioned.
But you can add some code to elevate (and trigger UAC prompt) the current
script by using ShellExecute as explained in below MSDN page. (long text
warning)

<https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx>

i.e. instead of executing "notepad.exe", execute "wscript.exe" instead. Use
the current script's full file path+name as the program's argument. And use
"runas" verb instead of "open".

Perform ShellExecute only if the current script is not yet elevated.
VBScript doesn't have built-in capability to check elevation status, so
you'll have to implement it yourself. You can do this by simply creating a
dummy test folder in the "C:\Progrm Files" folder. If it succeeds, then the
current script execution is already elevated (don't forget to delete the
dummy test folder), so don't perform ShellExecute. If it doesn't succeed,
then the current script execution is not yet elevated. Also add some error
handling in case the dummy test folder is already exist before the elevation
check.

Loading...