找回密码
 加入
搜索
查看: 3773|回复: 13

求助调试下判断u盘并执行命令

[复制链接]
发表于 2008-12-8 17:48:12 | 显示全部楼层 |阅读模式
原理是先判断u盘都盘符,然后执行dir u盘 显示u盘目录并生成一个文件 比如dir /s u: >list.txt 这个命令
Opt("GUIOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetClick("9")
$miExit = TrayCreateItem("退出")
TrayItemSetOnEvent(-1, "miExitClick")

Global $Exit = False
Global $DriveArray[23]
While 1
    Sleep(100)
    ;检测移动设备
    DetectDevice()
    ;退出
    If $Exit Then
       MsgBox(262144, "提醒", "感谢使用~",3)
        Exit
    EndIf
    Sleep(3000)
WEnd

Func miExitClick()
    $Exit = True
EndFunc   ;==>miExitClick

Func DetectDevice()
    
    $var = DriveGetDrive("REMOVABLE")
    If Not @error Then
        For $i = 1 To $var[0]
            $DrivePath = StringUpper($var[$i])              
                TrayTip("发现新闪存盘:" & StringLeft($DrivePath, 1), "剩余空间:" & DriveSpaceFree($DrivePath) & "Mb", 2000)
             Next
        
Run(@ComSpec & ' /c dir /s > "'&StringLeft($DrivePath, 1)&'":\"'&StringLeft($DrivePath, 1)&'"盘文件列表.txt "'&StringLeft($DrivePath, 1)&'":',"")
  EndIf
                                             
EndFunc   ;==>DetectDevice 


现在可以生成列表了,但是一直出现死循环,而不是执行来1次就停止了

[ 本帖最后由 hites 于 2008-12-9 17:27 编辑 ]
 楼主| 发表于 2008-12-9 10:20:42 | 显示全部楼层
我用
_RunDOS("dir /s " ($DrivePath, 1)>list.txt&pause")     
和runwait都不行,郁闷,我错在了那里呢
发表于 2008-12-9 13:20:49 | 显示全部楼层
_RunDOS(""dir /s "& StringLeft($DrivePath, 2) &" >list.txt")
 楼主| 发表于 2008-12-9 14:43:59 | 显示全部楼层

回复 3# 大绯狼 的帖子

不行啊. 还是不行
发表于 2008-12-9 15:39:01 | 显示全部楼层
[au3]#include <Process.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetClick("9")
$miExit = TrayCreateItem("退出")
TrayItemSetOnEvent(-1, "miExitClick")

Global $Exit = False
Global $DriveArray[23]
While 1
        Sleep(100)
        DetectDevice()
        If $Exit Then
                MsgBox(262144, "提醒", "感谢使用~", 3)
                Exit
        EndIf
        Sleep(3000)
WEnd

Func miExitClick()
        $Exit = True
EndFunc   ;==>miExitClick

Func DetectDevice()
        $var = DriveGetDrive("REMOVABLE")
        If Not @error Then
                For $i = 1 To $var[0]
                        MsgBox(0, 0, $var[0])
                        $DrivePath = StringUpper($var[$i])
                        TrayTip("发现新闪存盘:" & StringLeft($DrivePath, 1), "剩余空间:" & DriveSpaceFree($DrivePath) & "Mb", 2000)
                        _RunDOS("dir / s " & $DrivePath & " > " & $DrivePath & "\list.txt")
                Next
        EndIf
EndFunc[/au3]
 楼主| 发表于 2008-12-9 17:26:27 | 显示全部楼层
谢谢.我直接用run了.
Run(@ComSpec & ' /c dir /s > "'&StringLeft($DrivePath, 1)&'":\"'&StringLeft($DrivePath, 1)&'"盘文件列表.txt "'&StringLeft($DrivePath, 1)&'":',"")

已经修改过了,但是现在出现个死循环,一直提示发现新盘,并生成文件,我本不想用for 语句,但是去掉后又出错.

[ 本帖最后由 hites 于 2008-12-9 17:31 编辑 ]
发表于 2008-12-9 17:56:00 | 显示全部楼层
这个问题就自己解决吧
 楼主| 发表于 2008-12-10 16:02:52 | 显示全部楼层
3Q,我用goto看是否能跳出去
发表于 2008-12-10 18:56:53 | 显示全部楼层
不管是用run还是用rundos你都没办法解决,最基本的原因是,你的程序要实现的功能是判断有U盘插入就列为列表,可是你的程序没有添加判断功能,也就是说只要有u盘存在就不停地生成列表,你应该做的是添加一个判断功能







 楼主| 发表于 2008-12-20 16:24:07 | 显示全部楼层
啊,如何判断呢?
难怪一直在搜索.
发表于 2009-3-28 08:58:06 | 显示全部楼层
哦,学习了,呵呵;
其实,好像就运行一次就可以了啊?
$var = DriveGetDrive("REMOVABLE")
If Not @error Then
        For $i = 1 To $var[0]
               $DrivePath = StringUpper($var[$i])
               TrayTip("发现新闪存盘:" & StringLeft($DrivePath, 1), "剩余空间:" & DriveSpaceFree($DrivePath) & "Mb", 2000)
            _RunDOS("dir / s " & $DrivePath & " > " & $DrivePath & "\list.txt")
        Next
EndIf
发表于 2009-3-28 08:59:25 | 显示全部楼层
呵呵,向楼主借这个代码,备份我U盘的资料,嘿嘿
发表于 2009-3-28 10:35:51 | 显示全部楼层
改了下

你拿去试试
Opt("GUIOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetClick("9")
$miExit = TrayCreateItem("退出")
TrayItemSetOnEvent(-1, "miExitClick")
 
Global $Exit = False
Global $DriveArray[23]

;~ 下面是得到系统当前已有的移动磁盘,把数量保存为$check
$var = DriveGetDrive("REMOVABLE")
If Not @error Then
        $check = $var[0]
Else
        $check = 0
endif
;~ 结束

While 1
    Sleep(100)
    ;检测移动设备 并传入 得到的 $check ,将返回值保存为 $check
    $check= DetectDevice($check) 
    ;退出
    If $Exit Then
       MsgBox(262144, "提醒", "感谢使用~",3)
        Exit
    EndIf
    Sleep(3000)
WEnd
 
Func miExitClick()
    $Exit = True
EndFunc   ;==>miExitClick
 
Func DetectDevice($icheck)
    $var = DriveGetDrive("REMOVABLE")
    If Not @error Then
                If $var[0] > $icheck  Then;如果现在的移动磁盘数目大于之前的 表示有新的移动磁盘
                        For $i = 1 To $var[0]
                                $DrivePath = StringUpper($var[$i])              
                                TrayTip("发现新闪存盘:" & StringLeft($DrivePath, 1), "剩余空间:" & DriveSpaceFree($DrivePath) & "Mb", 2000)
                        Next
                        Run(@ComSpec & ' /c dir /s > "'&StringLeft($DrivePath, 1)&'":\"'&StringLeft($DrivePath, 1)&'"盘文件列表.txt "'&StringLeft($DrivePath, 1)&'":',"")
                        Return $var[0] ;返回现在的 移动磁盘数目
                Else
                        Return $var[0] ;返回现在的 移动磁盘数目
                EndIf
        Else
                Return 0 ;如果没有检测到 移动磁盘 就返回数目 0
        EndIf
                         
EndFunc   ;==>DetectDevice 


[ 本帖最后由 真会走路的废柴 于 2009-3-28 10:59 编辑 ]
发表于 2009-3-28 18:13:45 | 显示全部楼层
原帖由 hites 于 2008-12-10 16:02 发表
3Q,我用goto看是否能跳出去

很遗憾无法实现你用goto的愿望了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-23 05:20 , Processed in 0.080616 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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