»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 02-17-2004, 08:56 AM   #1 (permalink)
Registered User
 
Join Date: Oct 2001
Posts: 691
zskillz is on a distinguished road
Send a message via AIM to zskillz
recursive javascript to list all files/folders in a given folder...

Hi all... I want to do something that I believe should be quite simple... however, I seem to be doing something wrong, and I'm not sure what it is.

I want to write a simple javascript that will create a text file containing the contents (just by name for now) of all of the files and folders within the directory that the script is being run from. I know that I want to use recursion, but it's been some time since I've had to write any recursive code, so I'm really rusty

I have shown below what I have done thus far, and I'm not sure why it's not working honestly.... I think there may be a problem with the statement [i]if (fc.atEnd){... ...}[/b]

Code:
//The purpose of this script is to enumerate all of the
//folders and files contained within the folder that the
//script is run in.  I intend to use it to create a log 
//file when backing up my research data.

var WshShell = WScript.CreateObject ("WScript.Shell");

var fs, otf, ForAppending;
ForAppending = 8;
fs = new ActiveXObject("Scripting.FileSystemObject");
otf = fs.OpenTextFile(WshShell.CurrentDirectory+"\\Contents.zRBU", ForAppending, true);

//I'm not really sure, but I declare these here so that the fso doesn't have to be redeclared during the start of each recursive loop... (perhaps that's part of my problem too??)
	var fso, f, fc, s;
	fso = new ActiveXObject("Scripting.FileSystemObject");

createFolderList(WshShell.CurrentDirectory);
otf.Close();


function createFolderList(folderspec)
{
	f = fso.GetFolder(folderspec);
	fc = new Enumerator(f.SubFolders);
	if (fc.atEnd())
	{
		otf.WriteLine(folderspec + "\r\n");
//		createFileList(folderspec);
	}//end if
	else
	{
		for (; !fc.atEnd(); fc.moveNext())
		{
			createFolderList(fc.item());
		}//end for
		createFileList(folderspec);
		
	}//end else

}//end createFolderList()


function createFileList(folderspec)
{
	var fso, f, fc, s;
	fso = new ActiveXObject("Scripting.FileSystemObject");
	f = fso.GetFolder(folderspec);
	fc = new Enumerator(f.files);
	s = "";
	for (; !fc.atEnd(); fc.moveNext())
	{
		
		s += fc.item();
		s += "\r\n";
	}
	otf.WriteLine(s);
}//end createFileList()

any pointers would be helpful.

Thanks,
-Z

zskillz is offline   Reply With Quote
Old 02-23-2004, 08:55 AM   #2 (permalink)
Registered User
 
Iturea's Avatar
 
Join Date: Jan 2004
Posts: 80
Iturea is on a distinguished road
I think object scope is giving you problems.

You declare fs in the same scope as fso and use them (the same type of object) in the same function by making otf a type of fs.

The function may not know were to look for the definition of fs and otf just by the fact that they may need to be declared in the scope of the function.

Try just creating one object for ActiveXObject("Scripting.FileSystemObject"); and put it in the scope of the function you want to use it in.

I don't know - just a guess....
Iturea is offline   Reply With Quote
Old 02-26-2004, 05:50 PM   #3 (permalink)
Registered User
 
Join Date: Oct 2001
Posts: 691
zskillz is on a distinguished road
Send a message via AIM to zskillz
thx iturea.... I can't believe I didn't just try this before (obviously..i knew it was a problem since i commented it!!)

it was all a question of scope.

I moved these lines inside the createFolderList method

Code:
	var fso, f, fc, s;
	fso = new ActiveXObject("Scripting.FileSystemObject");
-Z
zskillz is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 06:02 PM.