VB代码搜索给定文件夹或目录中的所有excel文件

时间:2014-06-17 08:17:54

标签: vba excel-vba qtp excel

请参阅下面的代码,它的工作正常并搜索给定目录中的所有excel文件。但我无法理解这段代码

怀疑:NoOfFolders(Iterator1)是一个动态数组,当再次调用函数fnFolderStructure时,再次创建NoOfFolders数组。但是,即使值NoOfFolders(0)NoOfFolders(1)已更改,但仍然会再次使用之前的值调用它。

即使在撰写NoOfFolders(Iterator1)之后,如何保留价值。

fnCheckFiles("C:\Temp\Sahil")

Function fnFolderStructure(sAddress)
        i=0
        Dim NoOfFolders()
        Set objFSO1 = CreateObject("Scripting.FileSystemObject") 
        Set objFolder2 = objFSO1.GetFolder(sAddress) 
        Set FolderIn=objFolder2.SubFolders
        Set FileIn=objFolder2.Files

'       If FileIn.Count>0 Then
'            fnCheckFiles(sAddress)
'       End If  

    If FolderIn.Count>0 Then    
        y=FolderIn.Count        
        ReDim NoOfFolders(y-1)  

        For Each objSubfolder in FolderIn
             NoOfFolders(i)= objSubfolder.Name 
             i=i+1
        Next

        For Iterator1 = 0 To y-1
            sPath1=sAddress&"\"&NoOfFolders(Iterator1)
            fnCheckFiles(sPath1)
        Next

'            Set colSubfolders = FolderIn.Subfolders
'           Call fnFolderStructure(sPath1)
        End If
End Function

Function fnCheckFiles(sAddress)
            Set objFSO1 = CreateObject("Scripting.FileSystemObject") 
            Set objFolder4 = objFSO1.GetFolder(sAddress)        
            ''sFileName=objFolder4.Name
            For Each objFile In objFolder4.Files 
                sFileName=objFile.Name              
                if (InStr(1,sFileName,"xlsx",1)) then       
                    msgbox objFile.Name     
                End IF      
            Next


        if objFolder4.SubFolders.Count>0 then
            fnFolderStructure(sAddress)
        End if
End Function

2 个答案:

答案 0 :(得分:0)

  

即使在写完之后它如何能够保留值   NoOfFolders(的iterator1)

该语句没有写入数组。数组NoOfFolders用于四个地方......

1)将值声明为动态数组

Dim NoOfFolders()

2)重新设置变量以匹配它将容纳的文件夹名称的大小

ReDim NoOfFolders(y - 1)

3)将文件夹的名称分配给数组中的相应位置。这是将值写入数组的唯一位置。

NoOfFolders(i) = objSubFolder.Name

4)从数组中读取文件夹的名称以构建子文件夹的路径

sPath1 = sAddress & "\" & NoOfFolders(Iterator1)

答案 1 :(得分:0)

   ====================================================================================================================*/
def getTagValue(def filepath, def tagName,log)
{
    def tagValue = ""
    try
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance()
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
        Document doc = docBuilder.parse(filepath)
        // Get the root element
        Node company = doc.getFirstChild()
        NodeList nodes = doc.getElementsByTagName(tagName)
        //log.info "Nodes length :" +nodes.getLength()
        Node node = nodes.item(0)
        //log.info node.getTextContent()
        tagValue = node.getTextContent()
        if(tagValue == "")
        {
            tagValue = "tag not found"
        }
    }
    catch(Exception e)
    {
        log.error e.message
        tagValue = "tag not found"
    }
    return tagValue
}

============================== def列表readXpaths(def expectedExcelPath,def expectedExcelSheet,log,context)         {

        Fillo.Connection con=getFilloConnection(expectedExcelPath,log)
        String sQuery="Select * from "+expectedExcelSheet+" where TestCaseName='"+context.testCase.name+"'"
        //log.info sQuery

        Recordset rs=getRecordSet(con,sQuery,log)
        List<String> colList=rs.getFieldNames()
        colList.remove("TestCaseName")
        colList.remove("DATA_SET")
        colList.remove("RUN")
        List<String> xpaths=new ArrayList<String>()
        while(rs.next())
        {
            for(String str:colList)
            {           
                if(rs.getField(str)!="")
                {
                    xpaths.add(rs.getField(str))

                }
            }
        }
        return xpaths;
    }