home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2025
Discussions: 188,394, Posts: 2,243,574, Members: 232,623
Old February 17th, 2004, 11:56 AM   Digg it!   #1 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
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 February 23rd, 2004, 11:55 AM     #2 (permalink)
Banned
 
Iturea's Avatar
 
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....

Iturea is offline   Reply With Quote
Old February 26th, 2004, 08:50 PM     #3 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
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
Old August 17th, 2004, 11:56 AM     #4 (permalink)
Junior Member
 
Join Date: Aug 2004
Posts: 1
Cheeky Code

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>
PaddyR is offline   Reply With Quote
Reply


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

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

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

Most Active Discussions
Is It Just Me? (2904)
Building a gaming computer advice (5)
New Build ( Finally ) (6)
Looks like Burris will get his Sena.. (12)
CPU wont boot (7)
3-days in and no threads about Gaza (160)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (444)
Upgrading RAM (6)
Recent Discussions
building a gaming computer, inp.. (0)
Iming and surfing slowed down (3)
Laptop proccesor to desktop mob.. (0)
Please help! multiple problems! (3)
Left 4 Dead Small Freezes (1)
Install Problem for Windows Def.. (1)
RCA 52Inch HDTV wont turn on (2)
nVidia GTX 295 now available (1)
Error ~ BAD BLOCK (19)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 01:55 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28