注册 登录
编程论坛 VB6论坛

VB列举文件名的问题

linandceline 发布于 2014-12-19 16:53, 1093 次点击
1、我的目的是列举文件夹包括子文件夹下面的文件,

2、以下代码,检测路径Mypath为本地硬盘路径时测试没有问题,如D:\COUNT
     但是用网络路径测试就有问题, 如\\192.168.1.2\COUNT

3、求大神指正

Sub Cfile1(MyPath As String)
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i, m, n, idir As Long

If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)

Do While Myname <> ""
  If Myname <> "." And Myname <> ".." Then
    If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '提示错误的行
      idir = idir + 1
      ReDim Preserve dir_i(idir) As String
      dir_i(idir - 1) = Myname
    Else
      List3.AddItem Myname   
      List7.AddItem FileDateTime(MyPath & Myname)
    End If
  End If
  Myname = Dir
Loop
For i = 0 To idir - 1

Call Cfile1(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
14 回复
#2
xzlxzlxzl2014-12-19 19:43
如果你前面dir(网络路径)执行正常,而GetAttr出错,则可能是权限问题,你可以在网络机上用net user命令建立一个超级用户试试。
net user 主机用户名 密码 /add
net localgroup administrators 主机用户名 /add
#3
linandceline2014-12-20 09:30
权限是没有问题的
而且我曾有另一个程序,一样是监控网络路径,只是路径稍有不同
#4
xzlxzlxzl2014-12-20 11:15
可不可以提供多点信息,如是什么错误提示,出现错误时MyPath & Myname值。
#5
linandceline2014-12-20 15:46
实时错误52
错误的文件名或号码
#6
linandceline2014-12-20 15:49
回复 5楼 linandceline
补充:实际路径有中文名,但是我的其他程序也是一样可以执行
#7
xzlxzlxzl2014-12-21 17:00
没有调试环境,无法解答你的问题。如果你觉得是字符问题,你可以试着加引号试试,语句为If (GetAttr(chr(34) & MyPath & Myname & chr(34)) And vbDirectory) = vbDirectory Then '提示错误的行。如果还不行的话,建议查查网络文件所在主机共享设置问题,如果你觉得你在本地盘的情况下正常,你可以将网络盘直接映射为本地盘试下。
#8
linandceline2014-12-22 10:02
以下是全部代码
Dim y As Integer
Sub Cfile1(MyPath As String)
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i, m, n, idir As Long

If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)

Do While Myname <> ""
  If Myname <> "." And Myname <> ".." Then
    If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
      idir = idir + 1
      ReDim Preserve dir_i(idir) As String
      dir_i(idir - 1) = Myname
    Else
      List3.AddItem Myname   '把找到的文件显示到列表框中
      List7.AddItem FileDateTime(MyPath & Myname)
      List9.AddItem MyPath
    End If
  End If
  Myname = Dir '搜索下一项
Loop

For i = 0 To idir - 1
Call Cfile1(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String

List1.Clear
For m = 0 To (List3.ListCount - 1)
  If InStr(List3.List(m), ".") > 0 Then
    n = Len(Left(List3.List(m), InStr(List3.List(m), ".")))
    List1.AddItem Left(List3.List(m), n - 1)
  End If
Next

List5.Clear
For i = 0 To (List7.ListCount - 1)
  List5.AddItem List3.List(i) & " " & List7.List(i)
Next

End Sub
Sub Cfile2(MyPath As String)
Dim Myname As String
Dim a As String
Dim B() As String
Dim dir_i() As String
Dim i, m, n, idir As Long

If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)

Do While Myname <> ""
  If Myname <> "." And Myname <> ".." Then
    If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
      idir = idir + 1
      ReDim Preserve dir_i(idir) As String
      dir_i(idir - 1) = Myname
    Else
      List4.AddItem Myname   '把找到的文件显示到列表框中
      List8.AddItem FileDateTime(MyPath & Myname)
      List10.AddItem MyPath
    End If
  End If
  Myname = Dir '搜索下一项
Loop

For i = 0 To idir - 1
Call Cfile2(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String

List2.Clear
For m = 0 To (List4.ListCount - 1)
  If InStr(List4.List(m), ".") > 0 Then
    n = Len(Left(List4.List(m), InStr(List4.List(m), ".")))
    List2.AddItem Left(List4.List(m), n - 1)
  End If
Next

List6.Clear
For i = 0 To (List8.ListCount - 1)
  List6.AddItem List4.List(i) & " " & List8.List(i)
Next

End Sub
Sub Sentense()
List2.Clear
List4.Clear
List6.Clear
List8.Clear
List10.Clear
Cfile2 ("D:\count\")
Dim i, j, k As Integer
For i = 0 To (List4.ListCount - 1)
  For j = 0 To (List3.ListCount - 1)
    If List4.List(i) = List3.List(j) Then Exit For
  Next
  If j = List3.ListCount Then
    MsgBox "新发放文件" & vbCrLf & List2.List(i) & vbCrLf & vbCrLf & "发放时间" & vbCrLf & List8.List(i) & vbCrLf & vbCrLf & "文件路径:" & vbCrLf & List10.List(i), 64, "提醒"
  End If
Next

For i = 0 To (List3.ListCount - 1)
  For j = 0 To (List4.ListCount - 1)
    If List3.List(i) = List4.List(j) Then Exit For
  Next
  If j = List4.ListCount Then
    MsgBox "以下文件已删除" & vbCrLf & List1.List(i), 64, "提醒"
  End If
Next

For i = 0 To (List4.ListCount - 1)
  For j = 0 To (List3.ListCount - 1)
    If List4.List(i) = List3.List(j) Then
      If List10.List(i) = List9.List(j) Then
        If List8.List(i) <> List7.List(j) Then
          MsgBox "以下文件有更新" & vbCrLf & List2.List(i) & vbCrLf & vbCrLf & "更新时间" & vbCrLf & List8.List(i) & vbCrLf & vbCrLf & "文件路径:" & vbCrLf & List10.List(i), 64, "提醒"
        End If
      End If
    End If
  Next
Next

List1.Clear
List3.Clear
List5.Clear
List7.Clear
List9.Clear
Cfile1 ("D:\count\")
End Sub
Private Sub Form_Load()

List1.Clear
List3.Clear
List5.Clear
List7.Clear
Cfile1 ("D:\count\")
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
y = y + 1
If y = 10 Then
y = 0
Call Sentense
End If
End Sub
#9
lianyicq2014-12-22 10:20
试试...
把count文件夹网络共享后就可以了。


[ 本帖最后由 lianyicq 于 2014-12-22 10:41 编辑 ]
#10
linandceline2014-12-22 10:53
回复 9楼 lianyicq
D:的路径只是一个测试路径,如果在本地测试没有问题了,我就改成网络路径再测试
实际需求的,仅是网络路径而已
#11
风吹过b2014-12-22 11:31
我测试了代码没问题。

Cfile2 ("\\10.92.29.2\pub\VHD\")     这个是改成了网络地址
Cfile1 ("E:\VHD\")                   这个是改成了本地地址,有二个地方都改了。
#12
风吹过b2014-12-22 11:37
For i = 0 To (List4.ListCount - 1)
  For j = 0 To (List3.ListCount - 1)
    If List4.List(i) = List3.List(j) Then
      If List10.List(i) = List9.List(j) Then               '这句有问题。这二个列表框应该是保存的二个文件的具体位置。如果你是比较不同的文件,那这里的位置是肯定不同的,如果是相同的位置,那修改时间又是相同的。

      其他代码没去研究你的。我这二个路径是有一个文件有更新的,但没看到提示,就调试了一下。
#13
linandceline2014-12-27 15:37
经过尝试,发现了一个问题:

以下这段如果不是引用自身,而是另一个SUB的话就不会出现之前出现的问题
For i = 0 To idir - 1         
Call Cfile1(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String  

但是,这样的话我没有办法追塑到子文件夹
#14
linandceline2014-12-27 17:49
我想应该是文件夹的问题了
因为我把检测路径设置到子文件夹的时候是部分可以,部分不行

请问,文件夹名或者文件夹内部有什么要求?
名称长度?名称所包含的字符?或者有什么系统文件的需求?
#15
linandceline2014-12-29 17:38
答案:
1、文件名称过长
2、文件名称包含特殊字符

此贴问题已解决,感谢各位
1