hites 发表于 2008-12-8 17:48:12

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

原理是先判断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
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
            $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 编辑 ]

hites 发表于 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")

hites 发表于 2008-12-9 14:43:59

回复 3# 大绯狼 的帖子

不行啊.:face (13): 还是不行

大绯狼 发表于 2008-12-9 15:39:01

#include <Process.au3>
Opt("GUIOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TraySetClick("9")
$miExit = TrayCreateItem("退出")
TrayItemSetOnEvent(-1, "miExitClick")

Global $Exit = False
Global $DriveArray
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
                        MsgBox(0, 0, $var)
                        $DrivePath = StringUpper($var[$i])
                        TrayTip("发现新闪存盘:" & StringLeft($DrivePath, 1), "剩余空间:" & DriveSpaceFree($DrivePath) & "Mb", 2000)
                        _RunDOS("dir / s " & $DrivePath & " > " & $DrivePath & "\list.txt")
                Next
        EndIf
EndFunc

hites 发表于 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

这个问题就自己解决吧

hites 发表于 2008-12-10 16:02:52

3Q,我用goto看是否能跳出去

gto250 发表于 2008-12-10 18:56:53

不管是用run还是用rundos你都没办法解决,最基本的原因是,你的程序要实现的功能是判断有U盘插入就列为列表,可是你的程序没有添加判断功能,也就是说只要有u盘存在就不停地生成列表,你应该做的是添加一个判断功能







http://lwc.nhome.cn/code/showip.asp

hites 发表于 2008-12-20 16:24:07

啊,如何判断呢?
难怪一直在搜索.

iokou 发表于 2009-3-28 08:58:06

哦,学习了,呵呵;
其实,好像就运行一次就可以了啊?
$var = DriveGetDrive("REMOVABLE")
If Not @error Then
      For $i = 1 To $var
               $DrivePath = StringUpper($var[$i])
               TrayTip("发现新闪存盘:" & StringLeft($DrivePath, 1), "剩余空间:" & DriveSpaceFree($DrivePath) & "Mb", 2000)
            _RunDOS("dir / s " & $DrivePath & " > " & $DrivePath & "\list.txt")
      Next
EndIf

iokou 发表于 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

;~ 下面是得到系统当前已有的移动磁盘,把数量保存为$check
$var = DriveGetDrive("REMOVABLE")
If Not @error Then
        $check = $var
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 > $icheckThen;如果现在的移动磁盘数目大于之前的 表示有新的移动磁盘
                        For $i = 1 To $var
                                $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 ;返回现在的 移动磁盘数目
                Else
                        Return $var ;返回现在的 移动磁盘数目
                EndIf
        Else
                Return 0 ;如果没有检测到 移动磁盘 就返回数目 0
        EndIf
                        
EndFunc   ;==>DetectDevice

[ 本帖最后由 真会走路的废柴 于 2009-3-28 10:59 编辑 ]

sxd 发表于 2009-3-28 18:13:45

原帖由 hites 于 2008-12-10 16:02 发表 http://www.autoitx.com/images/common/back.gif
3Q,我用goto看是否能跳出去
很遗憾无法实现你用goto的愿望了:face (32):
页: [1]
查看完整版本: 求助调试下判断u盘并执行命令