复制锁定的文件 [英] Copying locked files

查看:75
本文介绍了复制锁定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常大的问题。


我在vb.net中写了一个脚本来制作文件夹和子文件夹的副本到

另一个目的地:


- 在''from.txt''我指定要复制的文件夹

- 在''to.txt''我指定在哪里复制它

- 在我阅读''to.txt'的内容后,我按当前日期再创建了一个名为

的子文件夹,那就是所有内容都要复制的内容/>
当我执行程序时,它开始复制我的文件夹和

一切运行正常。


这是问题发生的时间:

我试图以这种方式备份一个文件夹,其中一些文件是由我们的网络用户使用的



一旦我的程序达到要复制目前使用的文件的程度,就会崩溃。


我不知道如何修改我的程序甚至复制

文件被锁定或被某人使用。


你能帮助我吗?


如果你想下载整个解决方案并查看它,我就把它留下了

在网络服务器上下载,它是压缩的(23kb):

http://www.doprocess.com/_users/joe/xcopy.zip


我我没有想法。


如果您不想出于任何原因下载代码,请输入以下代码:


公开Class Form1

继承System.Windows.Forms.Form


#Region" Windows窗体设计器生成的代码


Public Sub New()

MyBase.New()


' 'Windows窗体设计器需要此调用。

InitializeComponent()

''在InitializeComponent()调用后添加任何初始化
< br $>
结束子


''表格覆盖处理以清理组件列表。

受保护的重载覆盖子处理(ByVal处理为

布尔值)

如果处理那么

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase.Dispose(处置)

End Sub


''Windows窗体设计器要求

私有组件As System.ComponentModel.IContainer


''注意:以下过程是Windows窗体要求

设计器

''可以使用Windows窗体设计器修改它。

''不要使用代码编辑器修改它。

< System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()

''

''Form1

''

Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)

Me.ClientSize = New System.Drawing.Size(504,266)

Me。 Name =" Form1"

Me.Text =" Form1"

End Sub


#End地区


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

Dim dest,findest as String


Dim SR1 As New System.IO.StreamReader(" to.txt")

Dim strCurrentLine1 As String

strCurrentLine1 = SR1.ReadLine


dest = strCurrentLine1.ToString + Date.Now.Day.ToString +"。"

+ Date.Now.Month.ToString +"。" + Date.Now.Year.ToString +" - " +

Date.Now.Hour.ToString +"。" + Date.Now.Minute.ToString +"。" +

Date.Now.Second.ToString

System.IO.Directory.CreateDirectory(dest)


Dim SR As New System.IO.StreamReader(" from.txt")

Dim strCurrentLine As String


Do While SR.Peek<> -1

strCurrentLine = SR.ReadLine

findest = dest +" \" +

strCurrentLine.ToString.Replace(" \"," - ")。替换(":","")

System.IO.Directory.CreateDirectory(最完整的)


RecursiveCopyFiles((strCurrentLine.ToString),findest,

True)


循环

SR1.Close()

SR.Close()

关闭()

结束Sub


Private Sub RecursiveCopyFiles(ByVal sourceDir As String,ByVal

destDir As String,ByVal fRecursive As Boolean)

Dim i As Integer

Dim posSep As Integer

Dim sDir As String

Dim aDirs()As String

Dim sFile As String

Dim aFiles()As String


''如果他们不是,那么将尾随分隔符添加到提供的路径中/>
存在。

如果不是

sourceDir.EndsWith(System.IO.Path.DirectorySeparat orChar.ToString())

然后

sourceDir& = System.IO.Path.DirectorySeparatorChar
结束如果


如果不是

destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())

然后

destDir& = System.IO.Path.DirectorySeparatorChar

结束如果


''递归切换继续钻进dir

结构。

如果fRecursive那么


''从当前父级获取目录列表。

aDirs = System.IO.Directory.GetDirectories(sourceDir)


For i = 0 to aDel.GetUpperBound(0)


''获取

当前路径中最后一个分隔符的位置。

posSep = aDirs(i).LastIndexOf(" \" ;)


''获取源目录的路径。

sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))


''在目的地

目录中创建新目录。

System.IO.Directory.CreateDirectory(destDir + sDir)


''由于我们处于递归模式,复制孩子



RecursiveCopyFiles(aDirs(i) ,(destDir + sDir),

fRecursive)

下一页


结束如果


''从当前父级获取文件。

aFiles = System.IO.Directory.GetFiles(sourceDir)


''复制所有文件。

For i = 0 to aFiles.GetUpperBound(0)


''获取尾随分隔符的位置。

posSep = aFiles(i).LastIndexOf(" \")


''获取源文件的完整路径。

sFile = aFiles( i).Substring((posSep + 1),aFiles(i).Length

- (posSep + 1))


''复制文件。

System.IO.File.Copy(aFiles(i),destDir + sFile)


下一页我


结束分


结束课


* -------------------- --- *

发表于:
www.GroupSrv.com

* ---------------- ------- *

I am running into one really big problem.

I wrote a script in vb.net to make a copy of folders and subfolder to
another destination:

- in ''from.txt'' I specify which folders to copy
- in ''to.txt'' I specify where to copy it
- After I read content of ''to.txt'' I create one more subfolder named
by current date and thats where everything gets to be copied
When I execute the program, it starts to copy my folders and
everything runs just fine.

Here is when the problem occurs:
I am trying to backup this way a folder, where some files are being
used by our network users.

Once my program gets to the point that it wants to copy file which is
being currently used, it crashes.

I can''t figure out how to amend my program to make a copy even that
the file is locked or being used by someone.

Can you help me?

If you want to download the whole solution and look at it, I left it
on the the webserver for download, it''s zipped (23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

I am running out of ideas.

Here is the code, if you don''t want to download it for any reasons:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form
Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dest, findest As String

Dim SR1 As New System.IO.StreamReader("to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine

dest = strCurrentLine1.ToString + Date.Now.Day.ToString + "."
+ Date.Now.Month.ToString + "." + Date.Now.Year.ToString + "-" +
Date.Now.Hour.ToString + "." + Date.Now.Minute.ToString + "." +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader("from.txt")
Dim strCurrentLine As String

Do While SR.Peek <> -1
strCurrentLine = SR.ReadLine

findest = dest + "\" +
strCurrentLine.ToString.Replace("\", "-").Replace(":", "")
System.IO.Directory.CreateDirectory(findest)

RecursiveCopyFiles((strCurrentLine.ToString), findest,
True)

Loop
SR1.Close()
SR.Close()
Close()
End Sub


Private Sub RecursiveCopyFiles(ByVal sourceDir As String, ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String

'' Add trailing separators to the supplied paths if they don''t
exist.
If Not
sourceDir.EndsWith(System.IO.Path.DirectorySeparat orChar.ToString())
Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

'' Recursive switch to continue drilling down into dir
structure.
If fRecursive Then

'' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For i = 0 To aDirs.GetUpperBound(0)

'' Get the position of the last separator in the
current path.
posSep = aDirs(i).LastIndexOf("\")

'' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

'' Create the new directory in the destination
directory.
System.IO.Directory.CreateDirectory(destDir + sDir)

'' Since we are in recursive mode, copy the children
also
RecursiveCopyFiles(aDirs(i), (destDir + sDir),
fRecursive)
Next

End If

'' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)

'' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)

'' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")

'' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length
- (posSep + 1))

'' Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)

Next i

End Sub

End Class

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*

推荐答案

这是我的代码递归移动文件夹(及其子文件夹)。我不确定
确定我的处理锁定文件的方式,但我会假设try / catch块

会允许它继续。


HTH,

Greg

选项严格开启


进口System.IO

进口系统.Configuration


模块模块1


Sub Main()


Dim Source As String = ConfigurationSettings .AppSettings(" source")

Dim Dest As String =

ConfigurationSettings.AppSettings(" destination")& " \" &安培;年(今天)&

右(0&月(今天),2)&正确(0&天(今天),2)


Directory.CreateDirectory(Dest)


''获取日志文件名

Dim logFileName As String = Dest& " \ trace.log"


''打开日志文件


Dim fileLog As StreamWriter = File.CreateText(logFileName)


''定义日志文件跟踪监听器

Dim logListener As TextWriterTraceListener = New

TextWriterTraceListener(fileLog)


''将新的跟踪侦听器添加到侦听器集合中

Trace.Listeners.Add(logListener)


Dim consoleListener As MyTrace = New MyTrace

Trace.Listeners.Add(consoleListener)


''确保我们实际写出数据

Trace.AutoFlush = True


RecursiveCopyFiles(Source,Dest,True)

Trace.WriteLine(" Finished。")


''刷新并关闭输出流。

fileLog.Flush()

fileLog.Close()


System.Environment.ExitCode = 0

End Sub


''递归复制全部从

''指定的源到指定目的地的文件和子目录。

私有函数RecursiveCopyFiles(_

ByVal sourceDir As String,_

ByVal destDir As String,_

ByVal bTop As Boolean)As Boolean


Dim aDirs()As String

Dim aFiles()as String


Dim ok As Boolean = True


Trace.WriteLine(" Inspecting folder" &安培; sourceDir)


尝试

''从当前父级获取目录列表。

aDirs = System.IO.Directory .GetDirectories(sourceDir)


For each folderpath As String in aDirs

Dim sDir As String


''获取源目录的路径。

sDir = System.IO.Path.GetFileName(folderpath)


''在目标目录中创建新目录。

System.IO.Directory.CreateDirectory(System.IO.Path .Combine(destDir,

sDir))


''由于我们处于递归模式,复制孩子也是

ok = RecursiveCopyFiles(folderpath,

System.IO.Path.Combine(destDir,sDir),False)


如果可以的话

尝试

Trace.WriteLine(" Deleting"& destDir& sDir)

System.IO.Directory.Delete(destDir& sDir)

Catch ex As Exception

Trace.WriteLine(" Error deletion" ;& destDir& sDir)

Trace.WriteLine(ex.Message)

ok = False

结束尝试

结束如果

下一页

Catch ex As Exception

Trace.WriteLine(" Error reading directory"& sourceDir)

结束尝试


''从当前父级获取文件。

aFiles = System.IO.Directory.GetFiles(sourceDir)


''复制所有文件。

每个文件路径为字符串在aFiles

Dim sFile为字符串


''获取源文件的完整路径。

sFile = System.IO.Path.GetFileName(filepath)


尝试

''复制文件。

Trace.WriteLine(" Copying"& filepath)

System.IO.File.Copy(filepath,< br $>
System.IO.Path.Combine(destDir,sFile))


''删除文件。

Trace.WriteLine(" Deleting"& filepath)

System.IO.File.Delete(filepath)

Catch ex As Exception

Trace.WriteLine(" Error deletion" &安培; filepath)

Trace.WriteLine(ex.Message)

ok = False

结束尝试


Catch ex As Exception

Trace.WriteLine(复制错误和&文件路径)

Trace.WriteLine(ex.Message)

ok = False

结束尝试


下一页


如果不是bTop那么

尝试

Trace.WriteLine(删除文件夹& sourceDir)

System.IO.Directory.Delete(sourceDir)

Catch ex As Exception

Trace.WriteLine(删除文件夹错误& sourceDir)

Trace.WriteLine(ex.Message)

ok = False

结束尝试

结束如果


结束功能


结束模块


Public NotInheritable Class MyTrace

继承TraceListener


''禁止实例化

Public Sub New()

MyBase.New()

End Sub


< Conditional(" TRACE&qu OT;)> _

公共过载覆盖子写(ByVal消息为字符串)

Console.Write(消息)

结束子


< Conditional(" TRACE")> _

公共重载覆盖Sub WriteLine(ByVal消息为字符串)

Console.WriteLine(消息)

结束子

End Class

" Martin Ho" < JA ******* @ Gmail的双点com.no-spam.invalid>在消息中写道

news:41 ********** @ Usenet.com ...
Here is my code that recursively moves a folder (and its subfolders). I not
sure how mine treats locked files, but I would assume the try/catch blocks
would allow it to continue.

HTH,
Greg
Option Strict On

Imports System.IO
Imports System.Configuration

Module Module1

Sub Main()

Dim Source As String = ConfigurationSettings.AppSettings("source")
Dim Dest As String =
ConfigurationSettings.AppSettings("destination") & "\" & Year(Today) &
Right("0" & Month(Today), 2) & Right("0" & Day(Today), 2)

Directory.CreateDirectory(Dest)

''get the log file name
Dim logFileName As String = Dest & "\trace.log"

''open the log file

Dim fileLog As StreamWriter = File.CreateText(logFileName)

''define the log file trace listener
Dim logListener As TextWriterTraceListener = New
TextWriterTraceListener(fileLog)

''add the new trace listener to the collection of listeners
Trace.Listeners.Add(logListener)

Dim consoleListener As MyTrace = New MyTrace
Trace.Listeners.Add(consoleListener)

''make sure that we actually write the data out
Trace.AutoFlush = True

RecursiveCopyFiles(Source, Dest, True)

Trace.WriteLine("Finished.")

'' Flush and close the output stream.
fileLog.Flush()
fileLog.Close()

System.Environment.ExitCode = 0

End Sub

'' Recursively copy all files and subdirectories from the
'' specified source to the specified destination.
Private Function RecursiveCopyFiles( _
ByVal sourceDir As String, _
ByVal destDir As String, _
ByVal bTop As Boolean) As Boolean

Dim aDirs() As String
Dim aFiles() As String

Dim ok As Boolean = True

Trace.WriteLine("Inspecting folder " & sourceDir)

Try
'' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For Each folderpath As String In aDirs
Dim sDir As String

'' Get the path of the source directory.
sDir = System.IO.Path.GetFileName(folderpath)

'' Create the new directory in the destination directory.
System.IO.Directory.CreateDirectory(System.IO.Path .Combine(destDir,
sDir))

'' Since we are in recursive mode, copy the children also
ok = RecursiveCopyFiles(folderpath,
System.IO.Path.Combine(destDir, sDir), False)

If ok Then
Try
Trace.WriteLine("Deleting " & destDir & sDir)
System.IO.Directory.Delete(destDir & sDir)
Catch ex As Exception
Trace.WriteLine("Error deleting " & destDir & sDir)
Trace.WriteLine(ex.Message)
ok = False
End Try
End If
Next
Catch ex As Exception
Trace.WriteLine("Error reading directory " & sourceDir)
End Try

'' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)

'' Copy all files.
For Each filepath As String In aFiles
Dim sFile As String

'' Get the full path of the source file.
sFile = System.IO.Path.GetFileName(filepath)

Try
'' Copy the file.
Trace.WriteLine("Copying " & filepath)
System.IO.File.Copy(filepath,
System.IO.Path.Combine(destDir, sFile))

Try
'' Delete the file.
Trace.WriteLine("Deleting " & filepath)
System.IO.File.Delete(filepath)
Catch ex As Exception
Trace.WriteLine("Error deleting " & filepath)
Trace.WriteLine(ex.Message)
ok = False
End Try

Catch ex As Exception
Trace.WriteLine("Error copying " & filepath)
Trace.WriteLine(ex.Message)
ok = False
End Try

Next

If Not bTop Then
Try
Trace.WriteLine("Deleting folder " & sourceDir)
System.IO.Directory.Delete(sourceDir)
Catch ex As Exception
Trace.WriteLine("Error deleting folder " & sourceDir)
Trace.WriteLine(ex.Message)
ok = False
End Try
End If

End Function

End Module

Public NotInheritable Class MyTrace
Inherits TraceListener

'' disallow instantiation
Public Sub New()
MyBase.New()
End Sub

<Conditional("TRACE")> _
Public Overloads Overrides Sub Write(ByVal message As String)
Console.Write(message)
End Sub

<Conditional("TRACE")> _
Public Overloads Overrides Sub WriteLine(ByVal message As String)
Console.WriteLine(message)
End Sub
End Class
"Martin Ho" <ja*******@gmail-dot-com.no-spam.invalid> wrote in message
news:41**********@Usenet.com...
我遇到了一个非常大的问题。我在vb.net上写了一个脚本来制作文件夹和子文件夹的副本到另一个目的地:

- 在''from.txt''我指定哪个要复制的文件夹
- 在''to.txt''我指定复制它的位置
- 在我阅读''to.txt'的内容后,我再创建一个名为
的子文件夹当前日期和那些东西都被复制

当我执行程序时,它开始复制我的文件夹并且
一切运行得很好。

这是当问题发生时:
我试图以这种方式备份一个文件夹,我们的网络用户正在使用某些文件。

一旦我的程序达到了这一点它想要复制当前正在使用的文件,它会崩溃。

我无法弄清楚如何修改我的程序来制作副本甚至文件
被锁定或被某人使用。

你能帮帮我吗?

如果你想下载整个解决方案并查看它,我就把它留下了
用于下载的网络服务器,它是压缩的(23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

我的想法已经不多了。
以下是代码,如果您不想出于任何原因下载它:

公共类Form1
继承System.Windows.Forms.Form
#Region" Windows窗体设计器生成的代码

Public Sub New()
MyBase.New()

''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化

End Sub

''表格覆盖处理清理组件列表。
受保护的过载覆盖子处理(ByVal处理为布尔)
如果处理则
如果不是(组件什么都没有)那么
components.Dispose ()
结束如果
结束如果
MyBase.Dispose(处理)
结束Sub

''Windows窗体设计师要求
私有组件作为System.ComponentModel.IContainer

''注意:Windows窗体
设计器需要以下过程
''它可以使用Windows窗体设计器进行修改。 <无线电通信/>''不要使用代码编辑器修改它。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5,13) Me.ClientSize = New System.Drawing.Size(504,266)
Me.Name =" Form1"
Me.Text =" Form1"

End Sub

#End Region
私有子Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim dest,findest as String

Dim SR1 As New System.IO.StreamReader(" to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine
dest = strCurrentLine1.ToString + Date.Now.Day.ToString +"。"
+ Date.Now.Month.ToString +"。" + Date.Now.Year.ToString +" - " +
Date.Now.Hour.ToString +"。" + Date.Now.Minute.ToString +"。" +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader(" from.txt" )
Dim strCurrentLine As String

同时SR.Peek<> -1
strCurrentLine = SR.ReadLine

findest = dest +" \" +
strCurrentLine.ToString.Replace(" \"," - ")。替换(":","")
System.IO.Directory.CreateDirectory(最好的)

RecursiveCopyFiles((strCurrentLine.ToString),findest,
True)

循环

SR1.Close()
SR.Close()
关闭()
End Sub

私有Sub RecursiveCopyFiles(ByVal sourceDir As String,ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs()As String
Dim sFile As String
Dim aFiles()As String

''如果它们不存在,则将尾随分隔符添加到提供的路径。
如果不是
sourceDir.EndsWith( System.IO.Path.DirectorySeparat orChar.ToString())
那么
sourceDir& = System.IO.Path.DirectorySeparat orChar
结束如果

如果不是
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
然后
destDir& = System.IO.Path.DirectorySeparatorChar
结束如果

''递归切换继续钻进dir
结构。
如果fRecursive那么

''从当前父级获取目录列表。
aDirs = System.IO.Directory.GetDirectories(sourceDir)

对于i = 0到aDirs.GetUpperBound(0)

''获取
当前路径中最后一个分隔符的位置。
posSep = aDirs(i).LastIndexOf(" \")

''获取源目录的路径。
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

''在目的地
directo中创建新目录ry。
System.IO.Directory.CreateDirectory(destDir + sDir)

''因为我们处于递归模式,所以复制孩子们

RecursiveCopyFiles(aDirs) (i),(destDir + sDir),
fRecursive)
下一页

结束如果

''从当前父级获取文件。
aFiles = System.IO.Directory.GetFiles(sourceDir)
''复制所有文件。
对于i = 0到aFiles.GetUpperBound(0)

''获取尾随分隔符的位置。
posSep = aFiles(i).LastIndexOf(" \")

''获取源文件的完整路径。
sFile = aFiles(i).Substring((posSep + 1),aFiles(i).Length
- (posSep + 1))

''复制文件。
System.IO.File.Copy(aFiles(i),destDir + sFile)

接下来我

End Sub

* ----------------------- *
发表于:
www.GroupSrv.com
* ------------ ----------- *
I am running into one really big problem.

I wrote a script in vb.net to make a copy of folders and subfolder to
another destination:

- in ''from.txt'' I specify which folders to copy
- in ''to.txt'' I specify where to copy it
- After I read content of ''to.txt'' I create one more subfolder named
by current date and thats where everything gets to be copied
When I execute the program, it starts to copy my folders and
everything runs just fine.

Here is when the problem occurs:
I am trying to backup this way a folder, where some files are being
used by our network users.

Once my program gets to the point that it wants to copy file which is
being currently used, it crashes.

I can''t figure out how to amend my program to make a copy even that
the file is locked or being used by someone.

Can you help me?

If you want to download the whole solution and look at it, I left it
on the the webserver for download, it''s zipped (23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

I am running out of ideas.

Here is the code, if you don''t want to download it for any reasons:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form
Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dest, findest As String

Dim SR1 As New System.IO.StreamReader("to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine

dest = strCurrentLine1.ToString + Date.Now.Day.ToString + "."
+ Date.Now.Month.ToString + "." + Date.Now.Year.ToString + "-" +
Date.Now.Hour.ToString + "." + Date.Now.Minute.ToString + "." +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader("from.txt")
Dim strCurrentLine As String

Do While SR.Peek <> -1
strCurrentLine = SR.ReadLine

findest = dest + "\" +
strCurrentLine.ToString.Replace("\", "-").Replace(":", "")
System.IO.Directory.CreateDirectory(findest)

RecursiveCopyFiles((strCurrentLine.ToString), findest,
True)

Loop
SR1.Close()
SR.Close()
Close()
End Sub


Private Sub RecursiveCopyFiles(ByVal sourceDir As String, ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String

'' Add trailing separators to the supplied paths if they don''t
exist.
If Not
sourceDir.EndsWith(System.IO.Path.DirectorySeparat orChar.ToString())
Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

'' Recursive switch to continue drilling down into dir
structure.
If fRecursive Then

'' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For i = 0 To aDirs.GetUpperBound(0)

'' Get the position of the last separator in the
current path.
posSep = aDirs(i).LastIndexOf("\")

'' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

'' Create the new directory in the destination
directory.
System.IO.Directory.CreateDirectory(destDir + sDir)

'' Since we are in recursive mode, copy the children
also
RecursiveCopyFiles(aDirs(i), (destDir + sDir),
fRecursive)
Next

End If

'' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)

'' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)

'' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")

'' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length
- (posSep + 1))

'' Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)

Next i

End Sub

End Class

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*



你真的想要读一个正在使用的文件,你将没有

想知道文件处于什么状态。我在这些情况下所做的是包装

a尝试...捕获并记录文件无法进行的事实备份。我不认为
认为Copy方法有办法做到这一点。您必须使用FileStream对象打开文件,然后逐字节复制它...


希望它有所帮助。

Chris


" Martin Ho" < JA ******* @ Gmail的双点com.no-spam.invalid>在消息中写道

news:41 ********** @ Usenet.com ...
Do you really want to be reading a file that is in use, you will have no
idea what state the file is in. What I''ve done in these cases is to wrap in
a try... catch and log the fact the file could not be backed up. I don''t
think there is a way for the Copy method to do this. You would have to open
up the file with a FileStream object and copy it byte by byte...

Hope it helps.
Chris

"Martin Ho" <ja*******@gmail-dot-com.no-spam.invalid> wrote in message
news:41**********@Usenet.com...
我遇到了一个非常大的问题。我在vb.net上写了一个脚本来制作文件夹和子文件夹的副本到另一个目的地:

- 在''from.txt''我指定哪个要复制的文件夹
- 在''to.txt''我指定复制它的位置
- 在我阅读''to.txt'的内容后,我再创建一个名为
的子文件夹当前日期和那些东西都被复制

当我执行程序时,它开始复制我的文件夹并且
一切运行得很好。

这是当问题发生时:
我试图以这种方式备份一个文件夹,我们的网络用户正在使用某些文件。

一旦我的程序达到了这一点它想要复制当前正在使用的文件,它会崩溃。

我无法弄清楚如何修改我的程序来制作副本甚至文件
被锁定或被某人使用。

你能帮帮我吗?

如果你想下载整个解决方案并查看它,我就把它留下了
用于下载的网络服务器,它是压缩的(23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

我的想法已经不多了。
以下是代码,如果您不想出于任何原因下载它:

公共类Form1
继承System.Windows.Forms.Form
#Region" Windows窗体设计器生成的代码

Public Sub New()
MyBase.New()

''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化

End Sub

''表格覆盖处理清理组件列表。
受保护的过载覆盖子处理(ByVal处理为布尔)
如果处理则
如果不是(组件什么都没有)那么
components.Dispose ()
结束如果
结束如果
MyBase.Dispose(处理)
结束Sub

''Windows窗体设计师要求
私有组件作为System.ComponentModel.IContainer

''注意:Windows窗体
设计器需要以下过程
''它可以使用Windows窗体设计器进行修改。 <无线电通信/>''不要使用代码编辑器修改它。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5,13) Me.ClientSize = New System.Drawing.Size(504,266)
Me.Name =" Form1"
Me.Text =" Form1"

End Sub

#End Region
私有子Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim dest,findest as String

Dim SR1 As New System.IO.StreamReader(" to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine
dest = strCurrentLine1.ToString + Date.Now.Day.ToString +"。"
+ Date.Now.Month.ToString +"。" + Date.Now.Year.ToString +" - " +
Date.Now.Hour.ToString +"。" + Date.Now.Minute.ToString +"。" +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader(" from.txt" )
Dim strCurrentLine As String

同时SR.Peek<> -1
strCurrentLine = SR.ReadLine

findest = dest +" \" +
strCurrentLine.ToString.Replace(" \"," - ")。替换(":","")
System.IO.Directory.CreateDirectory(最好的)

RecursiveCopyFiles((strCurrentLine.ToString),findest,
True)

循环

SR1.Close()
SR.Close()
关闭()
End Sub

私有Sub RecursiveCopyFiles(ByVal sourceDir As String,ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs()As String
Dim sFile As String
Dim aFiles()As String

''如果它们不存在,则将尾随分隔符添加到提供的路径。
如果不是
sourceDir.EndsWith( System.IO.Path.DirectorySeparat orChar.ToString())
那么
sourceDir& = System.IO.Path.DirectorySeparat orChar
结束如果

如果不是
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
然后
destDir& = System.IO.Path.DirectorySeparatorChar
结束如果

''递归切换继续钻进dir
结构。
如果fRecursive那么

''从当前父级获取目录列表。
aDirs = System.IO.Directory.GetDirectories(sourceDir)

对于i = 0到aDirs.GetUpperBound(0)

''获取
当前路径中最后一个分隔符的位置。
posSep = aDirs(i).LastIndexOf(" \")

''获取源目录的路径。
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

''在目的地
directo中创建新目录ry。
System.IO.Directory.CreateDirectory(destDir + sDir)

''因为我们处于递归模式,所以复制孩子们

RecursiveCopyFiles(aDirs) (i),(destDir + sDir),
fRecursive)
下一页

结束如果

''从当前父级获取文件。
aFiles = System.IO.Directory.GetFiles(sourceDir)
''复制所有文件。
对于i = 0到aFiles.GetUpperBound(0)

''获取尾随分隔符的位置。
posSep = aFiles(i).LastIndexOf(" \")

''获取源文件的完整路径。
sFile = aFiles(i).Substring((posSep + 1),aFiles(i).Length
- (posSep + 1))

''复制文件。
System.IO.File.Copy(aFiles(i),destDir + sFile)

接下来我

End Sub

* ----------------------- *
发表于:
www.GroupSrv.com
* ------------ ----------- *
I am running into one really big problem.

I wrote a script in vb.net to make a copy of folders and subfolder to
another destination:

- in ''from.txt'' I specify which folders to copy
- in ''to.txt'' I specify where to copy it
- After I read content of ''to.txt'' I create one more subfolder named
by current date and thats where everything gets to be copied
When I execute the program, it starts to copy my folders and
everything runs just fine.

Here is when the problem occurs:
I am trying to backup this way a folder, where some files are being
used by our network users.

Once my program gets to the point that it wants to copy file which is
being currently used, it crashes.

I can''t figure out how to amend my program to make a copy even that
the file is locked or being used by someone.

Can you help me?

If you want to download the whole solution and look at it, I left it
on the the webserver for download, it''s zipped (23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

I am running out of ideas.

Here is the code, if you don''t want to download it for any reasons:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form
Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dest, findest As String

Dim SR1 As New System.IO.StreamReader("to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine

dest = strCurrentLine1.ToString + Date.Now.Day.ToString + "."
+ Date.Now.Month.ToString + "." + Date.Now.Year.ToString + "-" +
Date.Now.Hour.ToString + "." + Date.Now.Minute.ToString + "." +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader("from.txt")
Dim strCurrentLine As String

Do While SR.Peek <> -1
strCurrentLine = SR.ReadLine

findest = dest + "\" +
strCurrentLine.ToString.Replace("\", "-").Replace(":", "")
System.IO.Directory.CreateDirectory(findest)

RecursiveCopyFiles((strCurrentLine.ToString), findest,
True)

Loop
SR1.Close()
SR.Close()
Close()
End Sub


Private Sub RecursiveCopyFiles(ByVal sourceDir As String, ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String

'' Add trailing separators to the supplied paths if they don''t
exist.
If Not
sourceDir.EndsWith(System.IO.Path.DirectorySeparat orChar.ToString())
Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

'' Recursive switch to continue drilling down into dir
structure.
If fRecursive Then

'' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For i = 0 To aDirs.GetUpperBound(0)

'' Get the position of the last separator in the
current path.
posSep = aDirs(i).LastIndexOf("\")

'' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

'' Create the new directory in the destination
directory.
System.IO.Directory.CreateDirectory(destDir + sDir)

'' Since we are in recursive mode, copy the children
also
RecursiveCopyFiles(aDirs(i), (destDir + sDir),
fRecursive)
Next

End If

'' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)

'' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)

'' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")

'' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length
- (posSep + 1))

'' Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)

Next i

End Sub

End Class

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*



Martin,


我查看了你的程序但是我认为这个问题本身对我来说很明显。在我看来,你可以为此目前只使用一个

尝试并抓住一块


就像伪造的那样

\\\

如果file.exist(路径)

dim notfree as boolean

do notfree = false

尝试

readfile

notfree = true

catch

threading.thread.sleep(10000)

free = false

end catch

loop

结束如果

// /


有点改变,因为我认为由Herfried,K。Wagner提供给这个

新闻组

(你必须当有人永远不会停止使用时,创造一个逃脱的机会)


我希望这有帮助吗?


Cor


" Martin Ho" < ja ******* @ gmail-dot-com.no-spam.invalid>
Martin,

I looked at your program, however I thought that the question itself is
clear for me. In my opinion can you for that you at the moment only use a
try and catch block

something as in pseudo
\\\
If file.exist(path)
dim notfree as boolean
do while notfree = false
Try
readfile
notfree = true
catch
threading.thread.sleep(10000)
free = false
end catch
loop
end if
///

A little bit changed as I thought provided by Herfried, K. Wagner to this
newsgroup
(You have to create an escape of course when somebody never stops using it)

I hope this helps?

Cor

"Martin Ho" <ja*******@gmail-dot-com.no-spam.invalid>
我遇到了一个非常大的问题。

- 在''from.txt''我指定要复制的文件夹
- 在''to.txt''我指定复制它的位置
- 在我阅读''to.txt''的内容后,我按当前日期再创建了一个名为
的子文件夹所有东西都被复制

当我执行程序时,它开始复制我的文件夹并且
一切运行正常。

这是问题发生的时间:
我试图以这种方式备份一个文件夹,我们的网络用户正在使用某些文件。

一旦我的程序达到要复制的程度目前正在使用的文件,它崩溃了。

我无法弄清楚如何修改我的程序来制作副本,即使文件是l某人ocked或被人使用。

你能帮助我吗?

如果你想下载整个解决方案并查看它,我把它留在了
上下载的网络服务器,它是压缩的(23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

我的想法已经不多了。

以下是代码,如果您不想出于任何原因下载它:

公共类Form1
继承System.Windows.Forms.Form

#Region" Windows窗体设计器生成的代码

Public Sub New()
MyBase.New()

''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化

End Sub

''表格覆盖处理清理组件列表。
受保护的过载覆盖子处理(ByVal处理为布尔)
如果处理则
如果不是(组件什么都没有)那么
components.Dispose ()
结束如果
结束如果
MyBase.Dispose(处理)
结束Sub

''Windows窗体设计师要求
私有组件作为System.ComponentModel.IContainer

''注意:Windows窗体
设计器需要以下过程
''它可以使用Windows窗体设计器进行修改。 <无线电通信/>''不要使用代码编辑器修改它。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5,13) Me.ClientSize = New System.Drawing.Size(504,266)
Me.Name =" Form1"
Me.Text =" Form1"

End Sub

#End Region
私有子Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim dest, findest As String

Dim SR1 As New System.IO.StreamReader("to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine

dest = strCurrentLine1.ToString + Date.Now.Day.ToString + "."
+ Date.Now.Month.ToString + "." + Date.Now.Year.ToString + "-" +
Date.Now.Hour.ToString + "." + Date.Now.Minute.ToString + "." +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader("from.txt")
Dim strCurrentLine As String

Do While SR.Peek <> -1
strCurrentLine = SR.ReadLine

findest = dest + "\" +
strCurrentLine.ToString.Replace("\", "-").Replace(":", "")
System.IO.Directory.CreateDirectory(findest)

RecursiveCopyFiles((strCurrentLine.ToString), findest,
True)

Loop

SR1.Close()
SR.Close()
Close()
End Sub


Private Sub RecursiveCopyFiles(ByVal sourceDir As String, ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String

’’ Add trailing separators to the supplied paths if they don’’t
exist.
If Not
sourceDir.EndsWith(System.IO.Path.DirectorySeparat orChar.ToString())
Then
sourceDir &= System.IO.Path.DirectorySeparat orChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

’’ Recursive switch to continue drilling down into dir
structure.
If fRecursive Then

’’ Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For i = 0 To aDirs.GetUpperBound(0)

’’ Get the position of the last separator in the
current path.
posSep = aDirs(i).LastIndexOf("\")

’’ Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

’’ Create the new directory in the destination
directo ry.
System.IO.Directory.CreateDirectory(destDir + sDir)

’’ Since we are in recursive mode, copy the children
also
RecursiveCopyFiles(aDirs(i), (destDir + sDir),
fRecursive)
Next

End If

’’ Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)

’’ Copy all files.
For i = 0 To aFiles.GetUpperBound(0)

’’ Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")

’’ Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length
- (posSep + 1))

’’ Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)

Next i

End Sub

End Class

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
I am running into one really big problem.

I wrote a script in vb.net to make a copy of folders and subfolder to
another destination:

- in ''from.txt'' I specify which folders to copy
- in ''to.txt'' I specify where to copy it
- After I read content of ''to.txt'' I create one more subfolder named
by current date and thats where everything gets to be copied
When I execute the program, it starts to copy my folders and
everything runs just fine.

Here is when the problem occurs:
I am trying to backup this way a folder, where some files are being
used by our network users.

Once my program gets to the point that it wants to copy file which is
being currently used, it crashes.

I can''t figure out how to amend my program to make a copy even that
the file is locked or being used by someone.

Can you help me?

If you want to download the whole solution and look at it, I left it
on the the webserver for download, it''s zipped (23kb):

http://www.doprocess.com/_users/joe/xcopy.zip

I am running out of ideas.

Here is the code, if you don''t want to download it for any reasons:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form
Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(504, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dest, findest As String

Dim SR1 As New System.IO.StreamReader("to.txt")
Dim strCurrentLine1 As String
strCurrentLine1 = SR1.ReadLine

dest = strCurrentLine1.ToString + Date.Now.Day.ToString + "."
+ Date.Now.Month.ToString + "." + Date.Now.Year.ToString + "-" +
Date.Now.Hour.ToString + "." + Date.Now.Minute.ToString + "." +
Date.Now.Second.ToString
System.IO.Directory.CreateDirectory(dest)

Dim SR As New System.IO.StreamReader("from.txt")
Dim strCurrentLine As String

Do While SR.Peek <> -1
strCurrentLine = SR.ReadLine

findest = dest + "\" +
strCurrentLine.ToString.Replace("\", "-").Replace(":", "")
System.IO.Directory.CreateDirectory(findest)

RecursiveCopyFiles((strCurrentLine.ToString), findest,
True)

Loop
SR1.Close()
SR.Close()
Close()
End Sub


Private Sub RecursiveCopyFiles(ByVal sourceDir As String, ByVal
destDir As String, ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String

'' Add trailing separators to the supplied paths if they don''t
exist.
If Not
sourceDir.EndsWith(System.IO.Path.DirectorySeparat orChar.ToString())
Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If

If Not
destDir.EndsWith(System.IO.Path.DirectorySeparator Char.ToString())
Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If

'' Recursive switch to continue drilling down into dir
structure.
If fRecursive Then

'' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)

For i = 0 To aDirs.GetUpperBound(0)

'' Get the position of the last separator in the
current path.
posSep = aDirs(i).LastIndexOf("\")

'' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1),
aDirs(i).Length - (posSep + 1))

'' Create the new directory in the destination
directory.
System.IO.Directory.CreateDirectory(destDir + sDir)

'' Since we are in recursive mode, copy the children
also
RecursiveCopyFiles(aDirs(i), (destDir + sDir),
fRecursive)
Next

End If

'' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)

'' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)

'' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")

'' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length
- (posSep + 1))

'' Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)

Next i

End Sub

End Class

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*



这篇关于复制锁定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆