Search function to search through subfolders

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • molle
    New Member
    • May 2007
    • 11

    Search function to search through subfolders

    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.FileS ystemObject");
    var fold = fso.GetFolder(N path);
    FileEnum = new Enumerator(fold .files);

    for (; !FileEnum.atEnd (); FileEnum.moveNe xt()) {
    document.editor .originfile.val ue += FileEnum.item() .Name;
    //When it finds the file I want it to store its path
    if(FileEnum.ite m().Name == document.editor .showfile.value )
    document.editor .originfile.val ue = 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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    I think SubFolders (instead of "files") should contain the sub-directories.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Changed thread title (a bit more detail)

      Comment

      • molle
        New Member
        • May 2007
        • 11

        #4
        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?

        Comment

        • molle
          New Member
          • May 2007
          • 11

          #5
          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?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by molle
            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.

            Comment

            • molle
              New Member
              • May 2007
              • 11

              #7
              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:

              Code:
              function findFile(PathArray, itr, nextFree){
              	var newitr = nextFree;
              	var fso = new ActiveXObject("Scripting.FileSystemObject"); 
              	var fold = fso.GetFolder(PathArray[itr]);
              	FileEnum = new Enumerator(fold.files);
              	FolderEnum = new Enumerator(fold.SubFolders);
              	document.editor.originfile.value = "NOT FOUND!";
              	
              	for(; !FileEnum.atEnd(); FileEnum.moveNext()){
              		document.editor.originfile.value += "F+" + FileEnum.item().Name;
              		if(FileEnum.item().Name == document.editor.showfile.value){
              			document.editor.originfile.value = "FOUND!";
              			return true;
              		}
              	}
              	for (; !FolderEnum.atEnd(); FolderEnum.moveNext()) {					
              		PathArray[newitr] = FolderEnum.item().Path;
              		document.editor.originfile.value += "D+" + FolderEnum.item().Name;
              		newitr++;
              	}
              	findFile(PathArray, itr+1, newitr);
              }

              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)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by molle
                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.

                Comment

                Working...