找回密码
 加入
搜索
查看: 8247|回复: 19

[AU3基础] 请教达人,遍历分区搜索的代码哪错了?

  [复制链接]
发表于 2014-12-19 17:28:37 | 显示全部楼层 |阅读模式
本帖最后由 dhwc 于 2014-12-19 19:59 编辑

写一个自动查找驱动并安装相应版本的程序,代码如下:
#include <file.au3>
Dim $osVersion,$osVer,$osBit,$DrvVer,$DrvFileNmae,$DriverNumber
$osVersion = FileGetVersion( @WindowsDir & "\system32\kernel32.dll" )
$osVer = StringLeft ( $osversion, 3 )
$osBit=@OSArch
$DriverNumber = DriveGetDrive("all")

TrayTip("", "检测操作系统版本,请稍候...", 5, 1)
If $osBit="X86" Then
                Select
                        Case $osVer="5.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 2000", 5, 1)
                        $DrvFileNmae="none"
                        
                        Case $osVer="5.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows XP x86", 5, 1)
                        $DrvFileNmae="[WinXP.x86]WanDrv6.exe"
                        
                        Case $osVer="5.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Server 2003 x86", 5, 1)
                        $DrvFileNmae="none"
                        
                        Case $osVer="6.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Vista x86", 5, 1)
                        $DrvFileNmae="none"
                        
                        Case $osVer="6.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 7 x86", 5, 1)
                        $DrvFileNmae="[Win7.x86]WanDrv6.exe"
                        
                        Case $osVer="6.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8 x86", 5, 1)
                    $DrvFileNmae="[Win8.x86]WanDrv6.exe"
                        
                        Case $osVer="6.3"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8.1 x86", 5, 1)
                        $DrvFileNmae="[Win8.x86]WanDrv6.exe"
                        
                        Case $osVer="6.4"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 x86 beat", 5, 1)
                        $DrvVer=1032
                        $DrvFileNmae="[Win10.x86]WanDrv6.exe"
                        
                        Case $osVer="10.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 x86", 5, 1)
                        $DrvVer=1032
                        $DrvFileNmae="[Win10.x86]WanDrv6.exe"
        EndSelect                        
    ElseIf $osBit="X64" Then
                Select
                        Case $osVer="5.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows XP x64", 5, 1)
                        $DrvFileNmae="none"
                        
                        Case $osVer="5.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Server 2003 x64", 5, 1)
                        $DrvFileNmae="none"
                        
                        Case $osVer="6.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Vista x64", 5, 1)
                        $DrvFileNmae="none"
                        
                        Case $osVer="6.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 7 x64", 5, 1)
                        $DrvFileNmae="[Win7.x64]WanDrv6.exe"
                        
                        Case $osVer="6.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8 x64", 5, 1)
                    $DrvFileNmae="[Win8.x64]WanDrv6.exe"
                        
                        Case $osVer="6.3"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8.1 x64", 5, 1)
                        $DrvFileNmae="[Win8.x64]WanDrv6.exe"
                        
                        Case $osVer="6.4"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 beat x64", 5, 1)
                        $DrvVer=1032
                        $DrvFileNmae="[Win10.x64]WanDrv6.exe"
                        
                        Case $osVer="10.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 x64", 5, 1)
                        $DrvVer=1064
                        $DrvFileNmae="[Win10.x64]WanDrv6.exe"
        EndSelect        
    ElseIf $osBit="IA64" Then
        MsgBox(64,"","系统架构为" & $osBit & "!本程序不支持安腾处理器!",10)
        Exit
EndIf

If $DrvFileNmae="none" Then
        MsgBox(64,"","没有当前系统的驱动安装程序~!" & @CRLF & "请确认你的系统为WinXP、Win7、Win8、Win10!", 10)
        Exit
EndIf

If FileExists ( @HomeDrive & "" & "Drivers" ) Then
MsgBox(64,"","已存在" & @HomeDrive & "盘下Drivers目录,3秒后删除...",3)
FileSetAttrib ( @HomeDrive & "" & "Drivers", "-RASH") 
DirRemove ( @HomeDrive & "" & "Drivers", 1)
EndIf

If Not @error Then
    For $i = 1 To $DriverNumber[0]
                FindAllFile(StringUpper($DriverNumber[$i]),$DrvFileNmae)
    Next
EndIf

Func FindAllFile($sDir,$FName)
        Local $hSearch = FileFindFirstFile($sDir & "\*.*")
        If $hSearch = -1 Then Return
        
        While 1
                Local $sFile = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If @extended Then
                    FindAllFile($sDir & "" & $sFile,$FName)
                    ContinueLoop
                                EndIf
                If $sFile = $FName Then 
                                MsgBox(64,"","找到驱动安装程序:" & $sDir & "" & $sFile & @CRLF & "3秒之后开始安装驱动..." ,3)
                                Run( $sDir & "" & $sFile )
                                ExitLoop
                                EndIf
                WEnd
                If $hSearch = -1 Then        
                    MsgBox(64,"","未找到驱动安装程序!" & @CRLF & "请确认下载自由天空驱动包并解压(任意位置均可)..." ,5)
                EndIf        
        FileClose($hSearch)

        EndFunc 
现在有两个问题:
1.找不到驱动的话,没有弹出提示?
2.如果存在两个以上的文件名相同的程序(比如不同版本驱动),他会全部调用,怎样只调用找到的第一个?我用ExitLoop为什么没用?

恳请解答,谢谢
 楼主| 发表于 2014-12-20 09:16:56 | 显示全部楼层
达人们 帮我看看吧 感谢不尽
发表于 2014-12-20 10:47:59 | 显示全部楼层
90行改为以下试试
If $DrvFileNmae = "none" Or Not $DrvFileNmae Then
发表于 2014-12-20 10:55:53 | 显示全部楼层
本帖最后由 afan 于 2014-12-22 14:44 编辑
#include <file.au3>
Dim $osVersion, $osVer, $osBit, $DrvVer, $DrvFileNmae, $DriverNumber
$osVersion = FileGetVersion(@WindowsDir & "\system32\kernel32.dll")
$osVer = StringLeft($osVersion, 3)
MsgBox(0, 0, '"' & $osVer & '"')
$osBit = @OSArch
$DriverNumber = DriveGetDrive("all")

TrayTip("", "检测操作系统版本,请稍候...", 5, 1)
If $osBit = "X86" Then
        Select
                Case $osVer = "5.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 2000", 5, 1)
                        $DrvFileNmae = "none"

                Case $osVer = "5.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows XP x86", 5, 1)
                        $DrvFileNmae = "[WinXP.x86]WanDrv6.exe"

                Case $osVer = "5.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Server 2003 x86", 5, 1)
                        $DrvFileNmae = "none"

                Case $osVer = "6.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Vista x86", 5, 1)
                        $DrvFileNmae = "none"

                Case $osVer = "6.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 7 x86", 5, 1)
                        $DrvFileNmae = "[Win7.x86]WanDrv6.exe"

                Case $osVer = "6.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8 x86", 5, 1)
                        $DrvFileNmae = "[Win8.x86]WanDrv6.exe"

                Case $osVer = "6.3"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8.1 x86", 5, 1)
                        $DrvFileNmae = "[Win8.x86]WanDrv6.exe"

                Case $osVer = "6.4"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 x86 beat", 5, 1)
                        $DrvVer = 1032
                        $DrvFileNmae = "[Win10.x86]WanDrv6.exe"

                Case $osVer = "10.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 x86", 5, 1)
                        $DrvVer = 1032
                        $DrvFileNmae = "[Win10.x86]WanDrv6.exe"
        EndSelect
ElseIf $osBit = "X64" Then
        Select
                Case $osVer = "5.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows XP x64", 5, 1)
                        $DrvFileNmae = "none"

                Case $osVer = "5.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Server 2003 x64", 5, 1)
                        $DrvFileNmae = "none"

                Case $osVer = "6.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows Vista x64", 5, 1)
                        $DrvFileNmae = "none"

                Case $osVer = "6.1"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 7 x64", 5, 1)
                        $DrvFileNmae = "[Win7.x64]WanDrv6.exe"

                Case $osVer = "6.2"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8 x64", 5, 1)
                        $DrvFileNmae = "[Win8.x64]WanDrv6.exe"

                Case $osVer = "6.3"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 8.1 x64", 5, 1)
                        $DrvFileNmae = "[Win8.x64]WanDrv6.exe"

                Case $osVer = "6.4"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 beat x64", 5, 1)
                        $DrvVer = 1032
                        $DrvFileNmae = "[Win10.x64]WanDrv6.exe"

                Case $osVer = "10.0"
                        TrayTip("", "系统架构为" & $osBit & @CRLF & "系统为 Windows 10 x64", 5, 1)
                        $DrvVer = 1064
                        $DrvFileNmae = "[Win10.x64]WanDrv6.exe"
        EndSelect
ElseIf $osBit = "IA64" Then
        MsgBox(64, "", "系统架构为" & $osBit & "!本程序不支持安腾处理器!", 10)
        Exit
EndIf

If $DrvFileNmae = "none" Or Not $DrvFileNmae Then
        MsgBox(64, "", "没有当前系统的驱动安装程序~!" & @CRLF & "请确认你的系统为WinXP、Win7、Win8、Win10!", 10)
        Exit
EndIf

If FileExists(@HomeDrive & "" & "Drivers") Then
        MsgBox(64, "", "已存在" & @HomeDrive & "盘下Drivers目录,3秒后删除...", 3)
        FileSetAttrib(@HomeDrive & "" & "Drivers", "-RASH")
        DirRemove(@HomeDrive & "" & "Drivers", 1)
EndIf

If Not @error Then
        For $i = 1 To $DriverNumber[0]
                FindAllFile(StringUpper($DriverNumber[$i]), $DrvFileNmae)
                If IsDeclared("~" & StringToBinary($DrvFileNmae)) Then ExitLoop
        Next
        If Not IsDeclared("~" & StringToBinary($DrvFileNmae)) Then
                MsgBox(64, "", "未找到驱动安装程序!" & @CRLF & "请确认下载自由天空驱动包并解压(任意位置均可)...", 5)
        EndIf
EndIf

Func FindAllFile($sDir, $FName)
        If IsDeclared("~" & StringToBinary($FName)) Then Return
        Local $hSearch = FileFindFirstFile($sDir & "\*.*")
        If $hSearch = -1 Then Return
        While 1
                Local $sFile = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If @extended Then
                        FindAllFile($sDir & "" & $sFile, $FName)
                        ContinueLoop
                EndIf
                If $sFile = $FName Then
                        Assign("~" & StringToBinary($FName), 1, 2)
                        MsgBox(64, "", "找到驱动安装程序:" & $sDir & "" & $sFile & @CRLF & "3秒之后开始安装驱动...", 3)
                        Run($sDir & "" & $sFile)
                        ExitLoop
                EndIf
        WEnd
        FileClose($hSearch)
EndFunc   ;==>FindAllFile

评分

参与人数 1金钱 +10 收起 理由
dhwc + 10

查看全部评分

 楼主| 发表于 2014-12-20 11:50:19 | 显示全部楼层
90行改为以下试试
afan 发表于 2014-12-20 10:47


提示:error: syntax error

你这个加的不对吧?我是当系统类型为2003、2008的时候提示没有当前系统类型的驱动,而不是搜索不到驱动的提示啊
 楼主| 发表于 2014-12-20 12:06:04 | 显示全部楼层
第二个问题,用以下试试
afan 发表于 2014-12-20 10:55


测试通过 ,谢谢

现在就是奇怪为嘛不提示找不到驱动...
发表于 2014-12-20 12:21:45 | 显示全部楼层
回复 6# dhwc


    4#修改了下
 楼主| 发表于 2014-12-20 12:43:26 | 显示全部楼层
回复  dhwc


    4#修改了下
afan 发表于 2014-12-20 12:21


不对,改了之后明明有驱动也提示没有,然后弹出找到驱动
发表于 2014-12-20 12:56:24 | 显示全部楼层
回复 8# dhwc


    用递归的问题。4#
 楼主| 发表于 2014-12-20 14:34:37 | 显示全部楼层
回复  dhwc


    用递归的问题。4#
afan 发表于 2014-12-20 12:56



    那就是递归的bug无法避免了吗?
发表于 2014-12-20 14:43:21 | 显示全部楼层
那就是递归的bug无法避免了吗?
dhwc 发表于 2014-12-20 14:34



    4#没用?
 楼主| 发表于 2014-12-20 14:51:34 | 显示全部楼层
事实上 本来我还想加上个判断,但不知道怎么加。如果系统为win10(假设为win10x64),那么先搜索[Win10.x64]WanDrv6.exe,如果搜索不到则搜索[Win8.x64]WanDrv6.exe——因为现阶段自由天空没出win10的驱动包,用win8可替代。

如果不用递归,怎么写这个代码??恳请赐教
发表于 2014-12-22 12:32:26 | 显示全部楼层
学习中,,好东东
 楼主| 发表于 2014-12-22 14:34:24 | 显示全部楼层
4#没用?
afan 发表于 2014-12-20 14:43



    测试是明明有驱动,却提示没有找到,然后搜索到的驱动程序开始运行了
发表于 2014-12-22 14:46:08 | 显示全部楼层
测试是明明有驱动,却提示没有找到,然后搜索到的驱动程序开始运行了
dhwc 发表于 2014-12-22 14:34



    4#再次修改了,我这没办法测试
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-16 09:11 , Processed in 0.101084 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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