Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBScript cannot delete folder - permission denied

Posted on 2011-03-13
7
Medium Priority
?
1,363 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Loops Section Overview
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

581 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