Solved

VBScript cannot delete folder - permission denied

Posted on 2011-03-13
7
1,275 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

823 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