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
*-----------------------*