Jump to content

Recommended Posts

Posted

I have trying to create a script in VBS to delete oversized folders but both the DeleteFolder and Size methods give a Permission Denied error even though I full access to the folder tree in question.

 

Anybody come across this behaviour before?

Posted
You may want to check that everything under the root folder is inheriting permissions or you have sufficient permissions. Also are you forcing the delete as it is set at false by default.
Posted
Sorry, I meant are you running it as part of a scheduled task or logon script or something?

 

nope interactive while logged as admin. The folders were on a remote share if that makes any difference.

 

The funning thing is though I could quite happily delete folders usins the RD /S batch command in a CMD sub shell.

  • 2 months later...
Posted

I think the problem might be because the size and delete methods of a folder object cannot act on a folder more than 1 level deep.

 

You'd think the Microsoft programers could just make use of few recursive system calls :roll:

Posted
I think the problem might be because the size and delete methods of a folder object cannot act on a folder more than 1 level deep.

 

You'd think the Microsoft programers could just make use of few recursive system calls :roll:

 

Works recursively here. Could be read-only files somewhere in the tree? If so, use filesystemobject.deletefolder "foldername",true - the true forces deletion of read-only files.

Posted

Manage to fix your problem? I've been a bit slow to spot this thread!

 

Dunno about VBS, but I've coded C++ apps in .net that bring up this error. It happens if you try to access things across UNC/mapped drives (.net has a pile of BS 'security' additions that prevent code being executed in this way unless you 'appease' the security -- what nonsense!).

 

If you're running it off a network, try copying it to a local machine, failing that check if its trying to do network file access.

Posted

@srochford

 

The force parameter worked but what I don't understand is why the same error comes up when you're just trying to find the folder size.

 

Read-only file blocking a recurisive delete but why what kind blocks directory sizing when you have admin priviliges.

 

@Friez

Code C++ in .net? You bad boy. You don't believe Microsoft that C# is speedy enough for everybody.

Posted

I've never learned C# as such. I've always coded in C++ mainly due to coding for mac, linux and windows (impossible in C#) but now and then I have to code for Windows specific thing.

 

I code games as a hobby, C# is FAR too clunky for serious development in that area.

Posted
@srochford

 

The force parameter worked but what I don't understand is why the same error comes up when you're just trying to find the folder size.

 

Read-only file blocking a recurisive delete but why what kind blocks directory sizing when you have admin priviliges.

 

 

If there are any sub-folders which you don't have permission to access (because the permissions are wrong) then this will fail.

 

I'm not absolutely sure why this is but I'd guess the logic is this: when you want to know the size of a folder the OS has to open that folder to see what's inside it. If you're not allowed to open that folder then you won't be able to get the size of it and the only sensible message to return is "access denied" - because that's what it is!

 

Are these user home folders? If so, I'd guess a user has created a folder and then changed the permissions so that only they have rights. This might be for badness or just because they're experimenting :-)

 

What I'd guess you need to do is make sure that you have rights to every folder in the tree before you try to find the size of the folders.

Posted

Alright this isn't making any sense. The trouble seems to come if I try to For Each (iterate) through a subfolders collection. I get "Permission Denied" when trying to get folder size of each constituent folder.

 

But if I use a FOR /F to get the folders and then call the VBS script using the folder name as an argument, the Folder.Size call works.

 

This has been the problem all along. My access rights are OK using command line or batch script to do the task but not VBS :(

 

I am the domain admin for pity's sake. Gimme the blasted information you blasted annoying script engine.

  • 2 weeks later...
Posted

objFSO = CreateObject("Scripting.FileSystemObject")
objHomeRoot = objFSO.GetFolder("E:\\Student\\HomeFolders\Year" & WScript.Arguments(0))

For Each objSubFolder In objHomeRoot.SubFolders

     WScript.Echo "Folder " & objSubFolder.Name & " occupies " & objSubFolder.size & " bytes"

Next 

 

I get the permission denied error at the WScript.Echo line unless I either delete or comment out the objSubfolder.size term.

 

I have also tried a test in JScript but with the same result "Permission Denied"

 

fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder("E:\\Student\\HomeFolders\\Year7");
fc = new Enumerator(f.SubFolders) ;
s = "" ;
  for (;!fc.atEnd(); fc.moveNext())
     {
         WScript.Echo(fc.fc.item().size);
        
     };

 

 

But the it the .size member works if I do it outside a For Each loop. Now as there is no way of refering to the constituents of the SubFolders collection without knowing their names this means I have to feed the names using FOR /F command subsitution:

 

 CD E:\Student\HomeFolders\Year7 & FOR /F %f in ('DIR /AD /B ') do cscript /NoLogo HomeSize.vbs  %f 

 

which calls

 

objFSO = CreateObject ("Scripting.FileSystemObject")
objHomeFolder = objFSO.GetFolder(WScript.Arguments(0))
WScript.Echo "Folder " & objHomeFolder.name & " consumes " & objHomeFolder.size

 

This works but why not enumarating through a subfolders collection using For Each?

 

Aaargh!!! What is going on????

Posted

Having failed to make it work in WSH I turned my attention to .Net scripting

 

First, PowerShell

PS_Prompt> $fso= New-Object -ComObject Scripting.FilesSystemObject
PS_Prompt> CD E:\HomeFolders\Year7
PS_Prompt> $hmrt = $fso.GetFolder($pwd) ; $hmrt.subfolders| where-object -filterscript {$_.size -gt 40000000} |  % {write-host $_.Name,($_.size/1024/1024) -Separator ","}

 

OK technically this is not a script but what I actually typed at the command line to get a list of all folders consuming more than 40MB. Because I used the $pwd variable I can just CD into the desired root folder and up arrow and re-use the same 'code'. PERL coders may recognise the use of $_ to represent the current record. The % is an alias for the ForEach-Object enumerator.

 

I also tried using the .NET version of JScript

This is more compiled rather interpreted 'scripting'

 

var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFolder(".\\");
var fc = new Enumerator(f.SubFolders) ;
for (;!fc.atEnd(); fc.moveNext())
     {
      
  	print(fc.item().path + " " + (fc.item().size/1024/1024));
        
     };

 

This compiles to an EXE using jsc and will tell you the number of megabytes consumed by the subfolders in the current directory.

 

Notice print() ather than wscript.echo(). This is .net not WSH so you loose alot of the properties and methods of the wscript object, which is quite annoying.

 

Anyway, it is clear that there wasn't a permissions issue as the relevant FSO methods work under .NET but not WSH

 

Go figure

  • 4 years later...
Posted
I had the same issue and I discovered that the problem was my fso.CurrentDirectory was set to the directory I was trying to delete. Hope that helps!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...