读书人

请问vb编程有关问题!多谢

发布时间: 2012-01-26 19:40:46 作者: rapoo

请教vb编程问题!谢谢
我写了个程序从映射的盘符考文件到另一个映射盘符。X,P都是映射盘符,但有时会提示文件未找到,偶尔会提示文件已存在,但我已经在程序里做了判断了的,另外,如果我不想它跳提示,要怎么做,因为我是加在计划任务里的,如果跳提示了。没能及时点掉,就影响下次计划任务的运行了
我判断网络盘是否存在是这样写的
' Check the network if work
If Dir("X:\", 16) = "" Or Dir("P:\", 16) = "" Then
pId = Shell("Connect.bat", 1)
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 等待程序结束
Call CloseHandle(pHnd)
End If
End If
我判断文件是否存在是这样写的,我是先判断目的文件夹是不是有着个文件,有就不拷贝了:
If Dir(destpath & "\" & filename) = "" Then
FileCopy Sourcepath & "\" & Fielname, Destpath & "\" & Filename
End If

[解决办法]

VB code
Private Sub Command1_Click()    Dim sDriv As String         '盘符 (不含“\”)    Dim sDire As String         '目录 (不含“\”)    Dim sFile As String         '文件名        Dim sRes As String    Dim iRes As Integer        sDriv = "H:"    sDire = ""    sFile = "sql.txt"        On Error GoTo ErrLine        iRes = VBA.FileSystem.GetAttr(sDriv)    On Error GoTo 0    If Not ((iRes And VBA.vbDirectory) = VBA.vbDirectory) Then        MsgBox "磁盘“" + sDriv + "”不存在!"        Exit Sub    End If        If Len(sDire) > 0 Then      '如目录字符串不空,则检查目录        sRes = VBA.FileSystem.Dir(sDriv + "\" + sDire, vbDirectory)        sRes = LCase(sRes)        If LCase(Right(sDire, Len(sRes))) <> sRes Then            MsgBox "路径“" + sDriv + "\" + sDire + "”不存在!"            Exit Sub        End If    End If        sRes = VBA.FileSystem.Dir(sDriv + "\" + IIf(Len(sDire) > 0, sDire + "\", "") + sFile)    If LCase(sRes) <> LCase(sFile) Then        MsgBox "文件“" + sDriv + "\" + IIf(Len(sDire) > 0, sDire + "\", "") + sFile + "”不存在!"        Exit Sub    End If        MsgBox "文件“" + sDriv + "\" + IIf(Len(sDire) > 0, sDire + "\", "") + sFile + "”存在!", vbInformation, "提示"        Err.Clear    Exit Sub        ErrLine:    Err.Clear    MsgBox "磁盘“" + sDriv + "”不存在!"    End Sub 

读书人网 >VB

热点推荐