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