VBS FileSystemObject.DeleteFolder Path does not exists, yet FileSystemObject.FolderExists is true
Asked Answered
G

2

6

I'm close to going insane. I have a configuration of code in VBS and it throws an error that should be logically impossible.

Check this code: (You need at least 10 reputation to post images. Well that sure is helpful.) https://i.sstatic.net/jjNDm.png

If the folder revFolder exists, then (force) delete it. That also means if the folder does not exist, no DeleteFolder will be executed. I literally have the folder in front of me, it's there, I can see it. It was created by the same code just a few lines up. FolderExists returns true so the folder exists. Yet it throws the error "Path not found".

What is going on? This must be a bug in VBS, right?

Grinnell answered 4/6, 2015 at 12:33 Comment(1)
I also tried having basePath point to C: and to a personal network drive, it makes no difference.Grinnell
H
10

.FolderExists tolerates the spurious "\", .DeleteFolder doesn't:

>> WScript.Echo goFS.FolderExists("C:\Documents and Settings\eh\30643986\")
>> goFS.DeleteFolder "C:\Documents and Settings\eh\30643986\"
>>
-1
Error Number:       76
Error Description:  Path not found
>> goFS.DeleteFolder "C:\Documents and Settings\eh\30643986"
>> WScript.Echo goFS.FolderExists("C:\Documents and Settings\eh\30643986")
>>
0
>>
Haemolysis answered 4/6, 2015 at 12:49 Comment(3)
You gotta be kidding me! The effin backslash was the culprit! Thank you so much, I was trying to solve this issue for DAYS! This is some serious bulls** :D (edit: this not even documented in MSDN) (edit2: can't upvote your post :( sorry about that)Grinnell
@Grinnell due to this behaviour @Haemolysis pointed out, as someone who doesn't like to play with trailing slashes I always try to use GetFolder to avoid. goFS.GetFolder(revFolder).Delete True. GetFolder also has no problem with trailing backslashes.Dulcedulcea
This gets even worse, as .FolderExists returns true for every given path with a trailing backslah no matter if it exists.Munshi
S
1

Try something like this: https://msdn.microsoft.com/en-us/library/fyy7a5kt%28v=vs.110%29.aspx

Sometimes you need to use the Path.Combine function to properly format a file path (it'll use all the appropriate escape characters).

EDIT

Look for buildpath in this article: http://windowsitpro.com/scripting/understanding-vbscript-manipulating-files-filesystemobject

Secessionist answered 4/6, 2015 at 12:43 Comment(1)
Hi, thanks for your suggestion. Unfortunately I was unable to find a VBS (VB Script) equivalent of System.IO.Path.Combine, it doesn't seem to exist in VBS, or did I just not find it?Grinnell

© 2022 - 2024 — McMap. All rights reserved.