51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3050|回复: 6
打印 上一主题 下一主题

[求助] 自定义函数:查找文件路径的问题,返回值怎么一直为空

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2009-6-12 16:00:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
foldername = qtpdir
filename ="标准版测试用例_new.xls"
y =  FindFilePath(foldername,filename)
msgbox y   '为什么这里打印为空

'在foldername文件夹及其子文件夹下找到一个名字为filename的文件的路径
Function FindFilePath(foldername,filename)                       
        Dim fso,UtilFolder,UtilFolderCollection
        Dim Find
        Dim tpath
        find = False
        Set fso = CreateObject("Scripting.FileSystemObject")         
        On error resume next
        Set UtilFolder=fso.GetFolder(foldername)
        If  UtilFolder is nothing Then
                msgbox "FindFilePath的foldername参数不正确"
        End If
        '搜索当前所有子目录
        For each f in UtilFolder.files
                If StrComp(Lcase(f.name),Lcase(trim(filename))) = 0 Then
                        find = True
                        tpath = f.path
                        Exit For
                End If
        Next
        If not  find Then
                Set UtilFolderCollection = UtilFolder.SubFolders
                If Not UtilFolderCollection Is Nothing Then                '如果子目录不为空
                        For each ufolder in UtilFolderCollection
                                 FindFilePath ufolder,filename
                        Next
                End If
        Else
                FindFilePath = CStr(tpath)
                MsgBox "haha:" & FindFilePath                '只打印一次,FindFilePath能取到路径
        End If
        Set UtilFolder = Nothing
        Set UtilFolderCollection = Nothing
        Set fso = Nothing
End Function
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2009-6-12 16:57:27 | 只看该作者
如果定义了D:  则加相对路径。 如果没定义文件夹,则加个绝对路径    路径必须用“”引号
回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2009-6-12 18:02:58 | 只看该作者
问题解决了,是循环套循环引起的问题,终于可以轻松了
回复 支持 反对

使用道具 举报

该用户从未签到

4#
发表于 2009-6-12 18:22:04 | 只看该作者
吧正确的放上来共享一下吧
回复 支持 反对

使用道具 举报

该用户从未签到

5#
发表于 2009-6-13 11:42:30 | 只看该作者
找到了.是变量作用域的问题.
Dim tpath 改为定义成全局变量
FindFilePath = CStr(tpath) 不要在else下面
改为放最后一行就可以了.

[ 本帖最后由 onlonely 于 2009-6-13 14:09 编辑 ]
回复 支持 反对

使用道具 举报

  • TA的每日心情
    难过
    2015-9-21 13:50
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]测试排长

    6#
    发表于 2009-6-14 20:17:28 | 只看该作者
    花了点时间解决这个问题。放出来共享下....(PS:貌似上面很多人都已经解决了...)
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    难过
    2015-9-21 13:50
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]测试排长

    7#
    发表于 2009-6-14 20:18:12 | 只看该作者
    foldername =  "G:\Test2"
    filename ="标准版测试用例_new.xls"
    Dim find,temp
    find = False
    call FindFilePath(foldername,filename)
    msgbox temp   '为什么这里打印为空

    '在foldername文件夹及其子文件夹下找到一个名字为filename的文件的路径
    Function FindFilePath(foldername,filename)                        
            Dim fso,UtilFolder,UtilFolderCollection
    '        Dim Find
            Dim tpath

            Set fso = CreateObject("Scripting.FileSystemObject")         
    '        On error resume next
            Set UtilFolder=fso.GetFolder(foldername)
            If  UtilFolder is nothing Then
                    msgbox "FindFilePath的foldername参数不正确"
            End If
            '搜索当前所有子目录
                    If not find Then
                                For each f in UtilFolder.files
                                            If StrComp(Lcase(f.name),Lcase(trim(filename))) = 0 Then
                            find = True
                            tpath = f.path
    '                                                FindFilePath = CStr(tpath)
                                                    temp = Cstr(tpath)
                            Exit For
                                            End If
                                    Next
                    End If

                   
            If not  find Then
                    Set UtilFolderCollection = UtilFolder.SubFolders
                    If Not UtilFolderCollection Is Nothing Then                '如果子目录不为空
                            For each ufolder in UtilFolderCollection
                                     FindFilePath ufolder,filename
                            Next
                    End If
            Else
    '                FindFilePath = CStr(tpath)
    '                MsgBox "haha:" & FindFilePath                '只打印一次,FindFilePath能取到路径
    '                                find = True
            End If
            Set UtilFolder = Nothing
            Set UtilFolderCollection = Nothing
            Set fso = Nothing
    End Function
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-25 04:35 , Processed in 0.082773 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表