找回密码
 加入
搜索
查看: 6002|回复: 11

[AU3基础] 如何让自定义函数执行的结果赋值给函数名?

  [复制链接]
发表于 2014-2-11 18:05:16 | 显示全部楼层 |阅读模式
在使用自定义函数的时候,有些函数是在函数名那里设置一下变量,然后就可以从函数名那里得到运行结果。虽然用得不少,但是我今天用我写的这个自定义函数,就用懵了。

Local $sFiles

_Filelist("C:\app")

Func _Filelist($SearchDir)
        $Search = FileFindFirstFile($SearchDir & "\*.*") 
        If $Search = -1 Then Return -1 
        While 1
                $File = FileFindNextFile($Search) 
                If @error Then 
                        FileClose($Search) 
                        Return 
                ElseIf $file = "." Or $file = ".." Then 
                        ContinueLoop
                Else
                        _Filelist($SearchDir & "\" & $File) 
                EndIf
                If         $File = "TEMP01.DBF" Then 
                        $sFiles = $SearchDir & "\" & $File                
        EndIf
        WEnd
EndFunc   ;==>_Filelist

函数运行最后的结果是存储在$sFiles里面的,而我却用$a = _Filelist("C:\app")来接收运行结果,可想而知,只能得到0.想请教大家,怎么修改,可以这样接收函数运行结果呢?谢谢!
发表于 2014-2-11 20:29:41 | 显示全部楼层

 
$c=_Filelist("C:\app")
 msgbox(0,"",$c)

Func _Filelist($SearchDir)
        $Search = FileFindFirstFile($SearchDir & "\*.*") 
        If $Search = -1 Then Return -1 
        While 1
                $File = FileFindNextFile($Search) 
                If @error Then 
                        FileClose($Search) 
                        Return 
                ElseIf $file = "." Or $file = ".." Then 
                        ContinueLoop
                Else
                        _Filelist($SearchDir & "\" & $File) 
                EndIf
                If      $File = "TEMP01.DBF" Then 
                        $sFiles = $SearchDir & "\" & $File     
                       return $sFiles     
        EndIf
        WEnd
EndFunc   ;==>_Filelist
发表于 2014-2-11 21:54:09 | 显示全部楼层

LZ,你在程序主体中定义$sFiles为Local变量,但它在你的函数里是另一个local变量,而函数没有指定返回值,你这样运行当然得不到结果,正常的写法是二楼G大的代码,当然你也可以不走寻常路,在程序主体中定义$sFiles为Global变量,这时,只要执行_Filelist("C:\app"),$sFiles变量就会取到值(此法不推荐,返回值才是正道)。
 楼主| 发表于 2014-2-11 23:35:29 | 显示全部楼层
回复 2# gto250
G大,我测试了一下你修改后的代码,返回值是0。
发表于 2014-2-11 23:45:31 | 显示全部楼层
里面有递归,不能仅使用单次返回的结果
 楼主| 发表于 2014-2-12 10:58:14 | 显示全部楼层
回复 5# afan

A大,我尝试修改了,还是得到不结果,请教了。
发表于 2014-2-12 11:25:21 | 显示全部楼层
你这从头到尾只会找一个文件吗? $sFiles = $SearchDir & "\" & $File
如果有多个不同路径的同文件名,应该用 &= 连接所有
发表于 2014-2-12 14:09:00 | 显示全部楼层
gto250 发表于 2014-2-11 20:29



    漂亮的代码,值得学习。
 楼主| 发表于 2014-2-12 16:41:26 | 显示全部楼层
回复 7# afan

这个函数,可以搜索出多个文件的,只要文件名等于if条件里的,都被搜索出来。如果不设置if条件,则可以搜索整个给定路径下的所有文件。
发表于 2014-2-12 17:12:30 | 显示全部楼层
回复 9# smooth


    我说过了,你这个只能搜出一个文件,详见7#
发表于 2014-2-12 21:31:32 | 显示全部楼层
本帖最后由 gto250 于 2014-2-12 21:38 编辑

$d=findfile("C:\app")

 MsgBox(0,"",$d)
 
 
Func findfile($Dir) 
Local $sFiles
 _Filelist($Dir,$sFiles)
 Return $sFiles
 EndFunc
 
 
 
 
 
 
 
Func _Filelist($SearchDir,ByRef $sFiles)
        
        $Search = FileFindFirstFile($SearchDir & "\*.*") 
        If $Search = -1 Then Return -1 
        While 1
                $File = FileFindNextFile($Search) 
                If @error Then 
                        FileClose($Search) 
                        Return 
                ElseIf $file = "." Or $file = ".." Then 
                        ContinueLoop
                Else
                        _Filelist($SearchDir & "\" & $File,$sFiles) 
                EndIf
                If $File = "pcv.ini" Then 
                        $sFiles = $SearchDir & "\" & $File

        EndIf
        WEnd
EndFunc   ;==>_Filelist



用个折中的办法吧
 楼主| 发表于 2014-2-12 23:50:54 | 显示全部楼层
回复 10# afan

你说的是对的。我忘记我把&符号删掉了,因为我现在要查找的文件,就只有一个文件,所以我觉得就没有必要再加个&了,加个&的话,后面还要价格@CRLF,这样就带了空白符,用起来麻烦。谢谢提醒!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-6 11:19 , Processed in 0.076200 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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