473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Search function to search through subfolders

11
Hi!

Does anyone know a searchscript to create a javascript file-search.
I want it to be able to search for a specific filename in a folder INCLUDING
its sub-folders.

I know there is a way to search a folder for a file but not its sub-folders, here
is what I got:

function findFile(Npath){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fold = fso.GetFolder(Npath);
FileEnum = new Enumerator(fold.files);

for (; !FileEnum.atEnd(); FileEnum.moveNext()) {
document.editor.originfile.value += FileEnum.item().Name;
//When it finds the file I want it to store its path
if(FileEnum.item().Name == document.editor.showfile.value)
document.editor.originfile.value = FileEnum.item().Path;
}
}


This function looks through the mainfolder that you give
as parameter but not its sub-folders, when I tried to come
up with a algorithm for going through each subfolder, my
head started to hurt =). Is there a neat way to do it?

Thx
May 28 '07 #1
7 7358
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

I think SubFolders (instead of "files") should contain the sub-directories.
May 29 '07 #2
acoder
16,027 Expert Mod 8TB
Changed thread title (a bit more detail)
May 29 '07 #3
molle
11
Thx for the reply!

But this doesn't work as I want it to.
If I change:
FileEnum = new Enumerator(fold.files);
to
FolderEnum = new Enumerator(fold.SubFolders);

All it does is list the subfolders in the main-folders and not the
the subfolders of the subfolders etc...
I want it to look for the file in the main-folder, sub-folders, the sub-sub-folders =
all folders in the path I've specified as paramter, is it possible?
May 30 '07 #4
molle
11
I know I must create a recursive function, i.e. a function that looks through a
folder, looks at the files there, then takes the next subfolder and calls itself
again. The súbfolder's relative path needs to be stored in an array I guess, but I
don't feel that smart this early in the morning, and this must have been done a
thousand times before, but I can't find any algorithm on google. Does anyone
here know of one?
May 30 '07 #5
acoder
16,027 Expert Mod 8TB
I know I must create a recursive function, i.e. a function that looks through a
folder, looks at the files there, then takes the next subfolder and calls itself
again. The súbfolder's relative path needs to be stored in an array I guess, but I
don't feel that smart this early in the morning, and this must have been done a
thousand times before, but I can't find any algorithm on google. Does anyone
here know of one?
You're on the right track. Having never worked with the filesystem object, I can only give you hints. You'd get the files in each folder/sub-folder. Then on each iteration, recursively call the same function which would again get the files and the sub-folders and so on until they were no sub-folders left. You would call the function using the path of the sub-folder. Since you're just searching, you can break out as soon as you find a match.
May 30 '07 #6
molle
11
Woooo!
With quite a lot tea-drinking and weird background-music, I solved
the algorithm that checks all files in all subfolders of a folder, here
it is:

Expand|Select|Wrap|Line Numbers
  1. function findFile(PathArray, itr, nextFree){
  2.     var newitr = nextFree;
  3.     var fso = new ActiveXObject("Scripting.FileSystemObject"); 
  4.     var fold = fso.GetFolder(PathArray[itr]);
  5.     FileEnum = new Enumerator(fold.files);
  6.     FolderEnum = new Enumerator(fold.SubFolders);
  7.     document.editor.originfile.value = "NOT FOUND!";
  8.  
  9.     for(; !FileEnum.atEnd(); FileEnum.moveNext()){
  10.         document.editor.originfile.value += "F+" + FileEnum.item().Name;
  11.         if(FileEnum.item().Name == document.editor.showfile.value){
  12.             document.editor.originfile.value = "FOUND!";
  13.             return true;
  14.         }
  15.     }
  16.     for (; !FolderEnum.atEnd(); FolderEnum.moveNext()) {                    
  17.         PathArray[newitr] = FolderEnum.item().Path;
  18.         document.editor.originfile.value += "D+" + FolderEnum.item().Name;
  19.         newitr++;
  20.     }
  21.     findFile(PathArray, itr+1, newitr);
  22. }
  23.  

nextFree is the next free spot in the array of paths, so that a child of the
recursive function doesn't write over a place in the array which its parent
has written to. (Don't know if child and parent is the right terminology)
May 31 '07 #7
acoder
16,027 Expert Mod 8TB
Woooo!
With quite a lot tea-drinking and weird background-music, I solved
the algorithm that checks all files in all subfolders of a folder
Excellent! Thanks for sharing.
May 31 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g....
1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
0
by: mm | last post by:
Can someone with Win 2K please help me here. Best yet, can some MVP duplicate it and report it to MS. I can't find any reference to this issue on the Web, or in MS Knowledge Base. I have been...
8
by: Roomy | last post by:
Guys, I'd love if any of you can help me with this one. I need a script that will take out the words Error, Warning, and Aborted from a 12mb text file and into a new text file after running the...
2
by: Philip Wagenaar | last post by:
How do I search for a directory on a windows xp machine and delete the directory including all subfolders and files in them?
6
by: Joris De Groote | last post by:
Hi, I have a program that must look in in certain folder and take out every file (also the files in subfolders). Now it's no problem to do this with a few for's and if's. However, I don't know...
1
by: Bigrabid | last post by:
Howdy, I have an access database that I use to keep track of projects that my group is working on. There are a number of files associated with any one project, so I have as a parent to , which...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
0
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
Hello group. I've migrated from Win 2003 server to Win 2008 server. I've been banging my head agaist a wall for several days now trying to figure this out. I have the following script that will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.