将C:\TEST目录下的文件拷贝到C:\DEST.
网络上只需要将C:\TEST定义为你的路径,比如:\\MACHINE1\SHAREFORLDER
Public Sub GetDir()
Dim sPath As String = "C:\Test\" '路径
Dim sName As String
Dim txtDestPath As String = "C:\Dest\"
txtDestPath = Microsoft.VisualBasic.Left(txtDestPath, Microsoft.VisualBasic.InStrRev(txtDestPath, "\"))
Try
sName = Dir(sPath, vbDirectory) ' Retrieve the first entry.
Catch ex As Exception
MsgBox("错误,你所指定的路径" & sPath & "可能不存在,请修改", MsgBoxStyle.Critical)
Exit Sub
End Try
Dim i As Long = 0
Do While sName <> "" ' Start the loop.
' Use bitwise comparison to make sure sName is a directory.
If (GetAttr(sPath & sName) And vbDirectory) <> FileAttribute.Directory Then
i = i + 1
End If
sName = Dir() ' Get next entry.
Loop
sName = Dir(sPath, vbDirectory) ' Retrieve the first entry.
Do While sName <> "" ' Start the loop.
' Use bitwise comparison to make sure sName is a directory.
If (GetAttr(sPath & sName) And vbDirectory) <> FileAttribute.Directory Then
Try
Dim xobj As New System.IO.FileInfo(sPath & sName)
Dim xobjLocal As New System.IO.FileInfo(txtDestPath & sName)
If xobj.LastWriteTime <> xobjLocal.LastWriteTime Then
System.IO.File.SetAttributes(txtDestPath & sName, IO.FileAttributes.Normal)
System.IO.File.Copy(sPath & sName, txtDestPath & sName)
End If
Catch ex As Exception
Try
System.IO.File.SetAttributes(txtDestPath & sName, IO.FileAttributes.Normal)
'Call Kill(txtDestPath & sName)
System.IO.File.Delete(txtDestPath & sName)
System.IO.File.Copy(sPath & sName, txtDestPath & sName)
Catch
End Try
End Try
End If
sName = Dir() ' Get next entry.
Loop
Exit Sub
End Sub