Solved

VBScript cannot delete folder - permission denied

Posted on 2011-03-13
7
1,263 Views
Last Modified: 2012-05-11
I have 3 scripts that I am combining into 1, now when I run it, I get a permission denied error at line #119.  subfolder.Delete

If I run that section standalone its fine.

Here is the script.
http://pastebin.com/tfEs1TVC

I think it has something to do with 2 different sections using FSO.

Here is a folder structure that I am working with as a point of reference (several csv files in these):
 folder structure
0
Comment
Question by:totalimpact
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35127165
First comment out line 131, you do not need to re-create the object on the objFSO variable (unless you previously set it to nothing).

1. Are there any SubFolders in there?  Try amending line 117 to
If subfolder.Files.Count = 0 And subfolder.SubFolders.Count = 0 Then

Open in new window

2. Do you, or anyone on the network, have that folder open in a Windows Explorer window?

3. Have you, or anyone on the network, got a command prompt window open and the current folder in that window is the one attempting to be deleted?

4. Double check the permissions on the folder.  You know that this is not the problem, but you should double-check anyway!

Regards,
Daz.
0
 
LVL 6

Expert Comment

by:dax_bad
ID: 35129071
try to replace line 119 with

objFSO.DeleteFolder subfolder.Path, True

/Cheers
Daniel
0
 

Author Comment

by:totalimpact
ID: 35129725
Something within the script is locking it, something in the code section above that one. Because if I try to delete it manually while the Msg box is open, its locked, but if I run that code section stand alone (lines 33-125) without all the other code, it deletes it fine.

Not sure, but the only code above that is the FTPDownload function call, which is here:
http://pastebin.com/ddcRr8SK

I can see ftp.exe launch in the task manager while the part of the code is running, and then it closes.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:totalimpact
ID: 35132741
Not sure what changed, But I now get the permission denied earlier at line 109:
 
objfso.MoveFolder Subfolder.Path,subfolder_ar

Open in new window


 I think something isnt closing properly.

0
 
LVL 12

Accepted Solution

by:
Daz_1234 earned 500 total points
ID: 35133265
Ok, if lines 33 to 125 work when run stand-alone, then if anything is holding anything else open, it must be in lines 1 to 32.

Nothing in lines 1 to 32 even opens any objects so the only suspect is the following line:
FTPDownload (ftp_adr), (ftp_user), (ftp_pass), strDestDir, strFetchDate, (ftp_files)

So I did a quick test and when the WshShell.CurrentDirectory equals a folder that you want to delete, you will get permission denied.

Your script is fairly complex to follow exactly without an identical setup to test with, but I want to rule this out first.  The FTPDownload function sets the Current Working Directory.

Can you add a line between 108 and 109 (or the line above wherever you get the permission denied) to check if  the Wshshell.Currentdirectory is in the one being deleted.
Set AShell = CreateObject("WScript.Shell") : MsgBox AShell.CurrentDirectory,,"Current Dir:"

Open in new window


If it is, then I suggest adding at line 96 in your function here http://pastebin.com/ddcRr8SK  the following:
oFTPScriptShell.CurrentDirectory = oFTPScriptShell.ExpandEnvironmentStrings("%windir%")

Open in new window

...which  will just ensure that the current working directory is the windows folder, i.e not one that you're going to delete or move.

Regards,
Daz.
0
 

Author Comment

by:totalimpact
ID: 35133983
WOW - that did it, you deserve extra credit.
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35134009
Thanks for the points, glad I could help!

Daz.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now