Solved

VBScript cannot delete folder - permission denied

Posted on 2011-03-13
7
1,291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 13

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 13

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 13

Expert Comment

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

Daz.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

615 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