从具有多个级别的文件夹中获取子文件夹

Get subfolders from folder with multiple levels

本文关键字:文件夹 获取      更新时间:2023-09-26

我有代码检查子文件夹中的所有文件的文件夹。但如何改变它,不仅检查子文件夹级别还检查子文件夹的子文件夹等等?

这是我为文件夹及其子文件夹编写的代码:

            var fso = new ActiveXObject("Scripting.FileSystemObject");
            fso = fso.getFolder(path);
            var subfolders = new Object();
            subfolders = fso.SubFolders;
            var oEnumerator = new Enumerator(subfolders);
            for (;!oEnumerator.atEnd(); oEnumerator.moveNext())
            {
                var itemsFolder = oEnumerator.item().Files;
                var oEnumerator2 = new Enumerator(itemsFolder);
                var clientFileName = null;
                for(;!oEnumerator2.atEnd(); oEnumerator2.moveNext())
                {
                    var item = oEnumerator2.item();
                    var itemName = item.Name;
                    var checkFile = itemName.substring(itemName.length - 3);
                    if(checkFile == ".ac")
                    {
                        var clientFileName = itemName;
                        break;
                    }
                }
            }

在子文件夹的每一层,我需要检查所有的文件,如果它能找到一个。ac文件。

我在评论中提到的解决方案看起来像这样(我不太了解ActiveX,所以有很多评论,希望你能很容易地纠正任何错误):

//this is the function that looks for the file inside a folder.
//if it's not there, it looks in every sub-folder by calling itself
function getClientFileName(folder) {
    //get all the files in this folder
    var files = folder.Files;
    //create an enumerator to check all the files
    var enumerator = new Enumerator(files);
    for(;!enumerator.atEnd(); enumerator.moveNext()) {
        //get the file name we're about to check
        var file = enumerator.item().Name;
        //if the file name is too short skip this one
        if (file.length<3) continue;
        //check if this file's name matches, if it does, return it
        if (file.substring(file.length - 3)==".ac") return file;
    }
    //if we finished this loop, then the file was not inside the folder
    //so we check all the sub folders
    var subFolders = folder.SubFolders;
    //create an enumerator to check all sub folders
    enumerator = new Enumerator(subFolders);
    for(;!enumerator.atEnd(); enumerator.moveNext()) {
        //get the sub folder we're about to check
        var subFolder = enumerator.item();
        //try to find the file in this sub folder
        var fileName = getClientFileName(subFolder);
        //if it was inside the sub folder, return it right away
        if (fileName!=null) return fileName;
    }
    //if we get this far, we did not find the file in this folder
    return null;
}

然后像这样调用这个函数:

var theFileYouAreLookingFor = getClientFileName(theFolderYouWantToStartLookingIn);

再次,注意上面的代码:我没有测试它,我也不太了解ActiveX,我只是拿了你的代码并改变了它,所以它应该在所有子文件夹中查找。

您需要的是递归函数。下面是一个简单的递归函数,它遍历提供的路径中的每个文件,然后进行递归调用来遍历每个子文件夹文件。对于遇到的每个文件,该函数调用提供的回调(您将在其中执行任何处理逻辑)。

功能:

function iterateFiles(path, recursive, actionPerFileCallback){ 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    //Get current folder
    folderObj = fso.GetFolder(path);
    //Iterate thru files in thisFolder
    for(var fileEnum = new Enumerator(folderObj.Files); !fileEnum.atEnd(); fileEnum.moveNext()){
      //Get current file
      var fileObj = fso.GetFile(fileEnum.item());
      //Invoke provided perFile callback and pass the current file object
      actionPerFileCallback(fileObj);
    }
    //Recurse thru subfolders
    if(recursive){
      //Step into each sub folder
      for(var subFolderEnum = new Enumerator(folderObj.SubFolders); !subFolderEnum.atEnd(); subFolderEnum.moveNext()){
          var subFolderObj = fso.GetFolder(subFolderEnum.item());
          //Make recursive call
          iterateFiles(subFolderObj.Path, true, actionPerFileCallback);
        }   
    }
};

用法(这里我传入一个匿名函数,每个文件调用):

iterateFiles(pathToFolder, true, function(fileObj){
    Wscript.Echo("File Name: " + fileObj.Name); 
};

现在. .这是一个非常基本的例子。下面是一个类似函数的更复杂的实现。在这个函数中,我们可以像前面一样递归地遍历每个文件。然而,现在调用者可以为函数提供一个"调用上下文",然后将其传递回回调。这是非常强大的,因为现在调用者可以使用它自己闭包中的先前信息。此外,我还为调用者提供了在每个递归级别更新调用上下文的机会。在设计这个函数时,为了满足我的特定需求,有必要提供检查每个回调函数是否成功的选项。你会在这个函数中看到对它的检查。我还包含了对遇到的每个文件夹执行回调的选项。

More Complex Function:

function iterateFiles(path, recursive, actionPerFileCallback, actionPerFolderCallback, useFnReturnValue, callingContext, updateContextFn){ 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    //If 'useFnReturnValue' is true, then iterateFiles() should return false IFF a callback fails. 
    //This function simply tests that case.
    var failOnCallbackResult = function(cbResult){
        return !cbResult && useFnReturnValue;
    }
    //Context that is passed to each callback
    var context = {};
    //Handle inputs
    if(callingContext != null){
        context.callingContext = callingContext;
    }
    //Get current folder
    context.folderObj = fso.GetFolder(path);
    //Do actionPerFolder callback if provided
    if(actionPerFolderCallback != null){
        var cbResult = Boolean(actionPerFolderCallback(context));
        if (failOnCallbackResult(cbResult)){
            return false;
        }
    }
    //Iterate thru files in thisFolder
    for(var fileEnum = new Enumerator(context.folderObj.Files); !fileEnum.atEnd(); fileEnum.moveNext()){
        //Get current file
        context.fileObj = fso.GetFile(fileEnum.item());
        //Invoke provided perFile callback function with current context
        var cbResult = Boolean(actionPerFileCallback(context));
        if (failOnCallbackResult(cbResult)){
            return false;
        }
     }
     //Recurse thru subfolders
     if(recursive){
         //Step into sub folder
         for(var subFolderEnum = new Enumerator(context.folderObj.SubFolders); !subFolderEnum.atEnd(); subFolderEnum.moveNext()){
             var subFolderObj = fso.GetFolder(subFolderEnum.item());
             //New calling context that will be passed into recursive call
             var newCallingContext;
             //Provide caller a chance to update the calling context with the new subfolder before making the recursive call
             if(updateContextFn != null){
                 newCallingContext = updateContextFn(subFolderObj, callingContext); 
             }
             //Make recursive call
             var cbResult = iterateFiles(subFolderObj.Path, true, actionPerFileCallback, actionPerFolderCallback, useFnReturnValue, newCallingContext, updateContextFn);
             if (failOnCallbackResult(cbResult)){
                 return false;
             }
         }
     }
     return true; //if we made it here, then all callbacks were successful 
 };

用法:

//Note: The 'lib' object used below is just a utility library I'm using.
function copyFolder(fromPath, toPath, overwrite, recursive){
    var actionPerFileCallback = function(context){
        var destinationFolder = context.callingContext.toPath;
        var destinationPath = lib.buildPath([context.callingContext.toPath, context.fileObj.Name]);
        lib.createFolderIfDoesNotExist(destinationFolder);
        return copyFile(context.fileObj.Path, destinationPath, context.callingContext.overwrite);
    };
    var actionPerFolderCallback = function(context){
        var destinationFolder = context.callingContext.toPath;
        return lib.createFolderIfDoesNotExist(destinationFolder);
    };
    var callingContext = {
        fromPath : fromPath,
        toPath : lib.buildPath([toPath, fso.GetFolder(fromPath).Name]), //include folder in copy
        overwrite : overwrite,
        recursive : recursive
    };
    var updateContextFn = function(currentFolderObj, previousCallingContext){
        return {
            fromPath : currentFolderObj.Path,
            toPath : lib.buildPath([previousCallingContext.toPath, currentFolderObj.Name]), 
            overwrite : previousCallingContext.overwrite,
            recursive : previousCallingContext.recursive
        }
    }
    return iterateFiles(fromPath, recursive, actionPerFileCallback, null, true, callingContext, updateContextFn);
};

我知道这个问题很老了,但我偶然发现了它,希望我的答案能帮助别人!