# ReadDir() and "Access denied"

**URL:** https://resource.dopus.com/t/readdir-and-access-denied/18786
**Category:** Help & Support
**Created:** [July 22, 2014, 8:50am UTC](https://resource.dopus.com/t/readdir-and-access-denied/18786 "2014-07-22T08:50:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)
#### Post date: [July 22, 2014, 8:50am UTC](https://resource.dopus.com/t/readdir-and-access-denied/18786/1 "2014-07-22T08:50:00Z")

</div>

Yesterday I posted a script command in the scripts section of the forum: [Command: RenameFolderAddCount](https://resource.dopus.com/t/command-renamefolderaddcount/18781/1)  
It uses ReadDir() to recursively step through subfolders and works as expected so far, but..

If I use ReadDir() recursively (by scripting) more than once in non-recurse mode, a final "RENAME FROM.. TO.." on a top-level folder results in an "Access denied" message:  
 ![](https://resource.dopus.com/uploads/default/original/2X/3/34274a5f832824be5e652c80f52c1456039c288d.png)  
  
This does not happen if I use ReadDir() and it's builtin recurse option and also not if ReadDir() is called on that folder only once (in non-recurse mode).  
So it's my own recursion implementation that leads to that problem it seems, but actually I don't see why that should happen, it does not stall or run endless, it exactly yields the results I am after.

I tried to resolve things by nulling all enumerators in use and calling the RENAME out of the loop of while(!enumerator.complete){...} etc., but it does not help.  
Now that ReadDir() has that builtin recurse mode, I wonder if this is for convinience only or if it intentionally exists to get around things I encounter? I doubt that though! o)

Maybe anybody has a clue why that happens? The problem can be seen when calling "RenameFolderAddCount deepness=9999" to activate my own recursion.  
Thx! o)

---

<div class="post-metadata">

### Author: ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)
#### Post date: [July 22, 2014, 9:28am UTC](https://resource.dopus.com/t/readdir-and-access-denied/18786/2 "2014-07-22T09:28:47Z")

</div>

I put some smallish demo-code together to make things easier for you and me, find it attached and in the code-box.  
The script adds a command "ReadDirAndRename", which gives "Access Denied" whenever used with a folder that contains subfolders (so ReadDir() was called more than once).

`///////////////////////////////////////////////////////////////////////////////
function OnInit(data){
	data.name = "Command.Folder: ReadDirAndRename";
	data.default_enable = true;
	var cmd = data.AddCommand();
	cmd.name = "ReadDirAndRename";
	cmd.method = "Command_ReadDirAndRename";
	cmd.label = "ReadDirAndRename";
}
///////////////////////////////////////////////////////////////////////////////
function Command_ReadDirAndRename(data) {
	var foldersEnum = new Enumerator(data.func.sourcetab.selected_dirs);
	while (!foldersEnum.atEnd()) {
		var folder = foldersEnum.item(); foldersEnum.moveNext();
		Crawl(folder.realpath);
		var cmd = 'RENAME PATTERN=* FROM="'+folder.realpath+'" TO="* (x)"';
		data.func.command.RunCommand(cmd);
	}
}
//////////////////////////////////////////////////////////////////////////////
function Crawl(path){
	DOpus.Output("Crawling ["+path+"]..");
	var folderEnum = DOpus.FSUtil.ReadDir( path, false);
	while(!folderEnum.Complete){
		var childItem = folderEnum.Next();
		if (childItem.is_dir)
			Crawl(childItem.realpath);
	}
}`  
[Command.Folder\_ReadDirAndRename.js.txt](https://resource.dopus.com/uploads/default/original/3X/1/9/194e40ccce756167226678a5e72eb764d5ab9313.txt) (1.07 KB)

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [July 24, 2014, 9:36pm UTC](https://resource.dopus.com/t/readdir-and-access-denied/18786/3 "2014-07-24T21:36:04Z")

</div>

Add a call to CollectGarbage(); before the line that runs the command. That will force JScript to release the out-of-scope objects it is holding on to.

(CollectGarbage() is a Microsoft extension to JavaScript, and not technically guaranteed to do anything, but it seems to solve the problem.)

---

<div class="post-metadata">

### Author: ![Leo](https://resource.dopus.com/user_avatar/resource.dopus.com/leo/32/69732_2.png) [@Leo](https://resource.dopus.com/u/Leo)
#### Post date: [July 24, 2014, 11:40pm UTC](https://resource.dopus.com/t/readdir-and-access-denied/18786/4 "2014-07-24T23:40:21Z")

</div>

We'll also add a folderEnum.Close(); method in the next update, which may be needed for Perl/Python/etc. and will also be more efficient for JScript (since doing a whole GC to release one object is overkill).

---

<div class="post-metadata">

### Author: ![tbone](https://resource.dopus.com/user_avatar/resource.dopus.com/tbone/32/25726_2.png) [@tbone](https://resource.dopus.com/u/tbone)
#### Post date: [July 25, 2014, 12:34am UTC](https://resource.dopus.com/t/readdir-and-access-denied/18786/5 "2014-07-25T00:34:37Z")

</div>

Yes, it does the trick here too! Thank you very much for your investigation and research! o)  
I was about to reimplement the Crawl() method to base on "Scripting.FilesystemObject", but prefer it this way of course! o)
