February 17th, 2004, 11:56 AM
|
#1 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
| 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 |
| |
February 23rd, 2004, 11:55 AM
|
#2 (permalink)
| | Banned
Join Date: Jan 2004 Location: Earth
Posts: 420
|
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.... |
| |
February 26th, 2004, 08:50 PM
|
#3 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
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 |
| |
August 17th, 2004, 11:56 AM
|
#4 (permalink)
| | Junior Member
Join Date: Aug 2004
Posts: 1
|
i was trying to index directories and came up with this Code: <html><head><title>Index A Directory</title></head>
<script type="text/javascript">
var fso = new ActiveXObject('Scripting.FileSystemObject');
var ws = new ActiveXObject("WScript.Shell")
function UpdtList(){TheStr="";
DirectoryPath=document.forms[0].Path_00.value;
OutFile=document.forms[0].Fname_00.value;Lst0=DirectoryPath.lastIndexOf("\\");
Lst1=DirectoryPath.lastIndexOf("\\",Lst0-1);Ating=DirectoryPath.substring(Lst1+1,Lst0);
StaStr="<html><head><title>"+Ating+"</title><style>\na{color:#0080ff;text-deco"+
"ration:none;border-bottom-style:solid;border-bottom-width:1px}\na:link"+
"{border-bottom-color:blue}\na:visited{color:#808080;border-bottom-colo"+
"r:purple}\na:hover{border-bottom-color:red}\n</style></head><body>\n<t"+
"able width=\"756\" border=\"0\" cellspacing=\"4\" cellpadding=\"3\" bg"+
"color=\"#ffffff\">\n<TR><TD align=\"center\" colspan=\"4\" bgcolor=\"#"+
"DFDFD1\"><FONT COLOR=\"#FFFFFF\" size=5><B>Contents of "+Ating+"</B><B"+
"R>\n</Td></Tr><TR><TD ALIGN=\"left\"><font size=\"6\">\n"
filename=DirectoryPath+OutFile;a="<a href=\"";b="\">";d="</a><BR>";MidStr="";
function DirList(){ComStr="cmd /c dir "+DirectoryPath+"*."+Ftype+" /b /s"
var Exec = ws.Exec(ComStr);
while (Exec.Status == 0){ThisStr="";ThisStr=Exec.StdOut.ReadLine()
if (ThisStr!=""){ThStr=ThisStr.replace(DirectoryPath,"");ThStr=ThStr.replace("."+Ftype,"")
TheStr+=a+ThisStr+b+ThStr+d;}}}
if (document.forms[0].doc.checked==true){
TheStr+="<font color=\"#000080\">Word Documents<BR>";Ftype="doc";DirList();}
if (document.forms[0].xls.checked==true){
TheStr+="<font color=\"#000080\"><BR>Excel Documents<BR>";Ftype="xls";DirList();}
if (document.forms[0].ppt.checked==true){
TheStr+="<font color=\"#000080\"><BR>PowerPoint Documents<BR>";Ftype="ppt";DirList();}
if (document.forms[0].txt.checked==true){
TheStr+="<font color=\"#000080\"><BR>Text Documents<BR>";Ftype="txt";DirList();}
if (document.forms[0].pdf.checked==true){
TheStr+="<font color=\"#000080\"><BR>Acrobat Documents<BR>";Ftype="pdf";DirList();}
if (document.forms[0].log.checked==true){
TheStr+="<font color=\"#000080\"><BR>Log Files<BR>";Ftype="log";DirList();}
if (document.forms[0].dat.checked==true){
TheStr+="<font color=\"#000080\"><BR>Dat Files<BR>";Ftype="dat";DirList();}
if (document.forms[0].mp3.checked==true){
TheStr+="<font color=\"#000080\"><BR>mp3 Files<BR>";Ftype="mp3";DirList();}
var file = fso.CreateTextFile(filename, true);
file.WriteLine(StaStr+TheStr);file.Close();
document.forms[0].Path_00.value=DirectoryPath;
document.forms[0].Fname_00.value=OutFile;}
function OpenIt(){Dh=document.forms[0].Path_00.value;
Op=document.forms[0].Fname_00.value;window.open(Dh+Op);}
function DelIt(){Dh0=document.forms[0].Path_00.value;
Op0=document.forms[0].Fname_00.value;ws.Exec("cmd /c Del "+Dh0+Op0)}
TheBody="<form>\n<BR>Directory To Index (UNC or Drive path)\n<BR><input "+
"type=\"text\" name=\"Path_00\" value=\"C\n:\\Winnt\\\" size=\"50\"><BR"+
">Output File Name (will be placed in above Directory)\n<BR><input type"+
"=\"text\" name=\"Fname_00\" value=\"Index.html\" size=\"20\"><BR>Allow"+
"ed File Types<BR>\n\n"+
""+
"<INPUT TYPE=\"checkbox\" NAME=\"doc\" VALUE=\"doc\">*.doc\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"xls\" VALUE=\"xls\">*.xls\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"ppt\" VALUE=\"ppt\">*.ppt\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"txt\" VALUE=\"txt\" CHECKED>*.txt\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"pdf\" VALUE=\"pdf\">*.pdf\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"log\" VALUE=\"log\">*.log\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"dat\" VALUE=\"dat\">*.dat\n"+
"<INPUT TYPE=\"checkbox\" NAME=\"mp3\" VALUE=\"mp3\">*.mp3\n"+
""+
"<BR><input type=\"button\" onclick=\"UpdtList()\" value=\"Click To Upd"+
"ate\"><BR><BR>\n<input type=\"button\" onclick=\"OpenIt()\" value=\"Cl"+
"ick Here To View The File You Just Created\"><BR><input type=\"button\""+
" onclick=\"DelIt()\" value=\"Click Here To Delete The File You Just Cre"+
"ated\"></form></body></html>\n"
document.write(TheBody)
</script> |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |