Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy directory from textbox data 1

Status
Not open for further replies.
Apr 4, 2001
34
0
0
GB
Hi,
I want to copy the contents of a directory to another directory. I have tried using My.computer.filesystem.copydirectory(textbox1.text, "c:\...", true)
but it wont accept the textbox1.text. how do i get it to read the text in textbox1 to be put in to the copydirectory or is there a better way to do this.

cheers Yesterdays
 

there are couple of ways to copy contents of a directory to another. Below is one of them.

Dim sourceDir As String = "C:\Test"
Dim destinationDir As String = "C:\MyTest"
Dim destinationFileName As String

'Get the names of file in source directory.
Dim myArr() As String = Directory.GetFiles(sourceDir)

For Each strFileName As String In myArr
'Use the same file name as source.
destinationFileName = Microsoft.VisualBasic.Right(strFileName, Microsoft.VisualBasic.InStr(Microsoft.VisualBasic.StrReverse(strFileName), "\"))

'Copy thr file from source to destination. Overwrite if file already exists.
File.Copy(strFileName, destinationDir & destinationFileName, True)

Next strFileName
 
Hi PankajBanga,
I get what you are saying but I need it to work from a textbox. So it gets the data from the textbox.text and then copies it to a string.
 
Do you what to copy file name specified in the textbox to a directory or directory name specified in textbox to another location?
 
Hi PankajBanga,
I want to copy everything in a directory to another directory. The destination can be set by a sting as this will always be the same. The source is specified by the user i.e the reason for a textbox. So it needs to pick up the textbox.text info and then put it in a string.

Cheers Yesterdays
 
Dim sourceDir As String = "C:\Test"
'User specified destination.
Dim destinationDir As String = TextBox1.Text
Dim destinationFileName As String

'Create directory.
Directory.CreateDirectory(destinationDir)

'Get the names of file in source directory.
Dim myArr() As String = Directory.GetFiles(sourceDir)

For Each strFileName As String In myArr
'Use the same file name as source.
destinationFileName = Microsoft.VisualBasic.Right(strFileName, Microsoft.VisualBasic.InStr(Microsoft.VisualBasic.StrReverse(strFileName), "\"))

'Copy thr file from source to destination. Overwrite if file already exists.
File.Copy(strFileName, destinationDir & destinationFileName, True)

Next strFileName
 
Is there an idea how to modify this method to copy the folders in the sourcedir too? Maybe a loop doing this for each folders in sourcedir, but that feels very clumsy.

Basically, I need some ideas how to copy a complete directory to anoher location, fast and efficiently. I've got a working duck taped solution but it is really slow and it tends to freeze up the whole execution when dealing with large amount of data (100mb+).
 
You'll want to create a recursive search function:

Code:
  Sub RecursiveFileCopy(ByVal SourcePath As String, ByVal DestPath As String)

    Directory.CreateDirectory(DestPath)
    Dim SourceFileName As String
    Dim DestFileName As String
    Dim Files() As String = Directory.GetFiles(SourcePath)

    For Each SourceFileName In Files
      DestFileName = SourceFileName.Substring(SourceFileName.LastIndexOf("\") + 1)
      File.Copy(SourceFileName, DestPath & "\" & DestFileName, True)
    Next

    Dim Dirs() As String = Directory.GetDirectories(SourcePath)
    Dim Path As String
    For Each Path In Dirs
      RecursiveFileCopy(Path, DestPath & "\" & Path.Substring(Path.LastIndexOf("\") + 1))
    Next
  End Sub

call it like this:

Code:
  Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    RecursiveFileCopy("C:\User", "C:\User2")
  End Sub

Tried, tested and good ;)

-Rick

----------------------
 
Thx Rick, it really was good for my purposes after some slight additions. Wonder why there are no classes ready for this kind of need.. Anyway, nice code.
 
When I try to use the example from ThatRickGuy I get an error on the line

Dim Files() As String = Directory.GetFiles(SourcePath)

VBE underlines Directory and tells me "Name 'Directory' is not declared"

What must I add to get this name declared?

Rien.
 
Thanks Rick, works like a charm.

Trouble is now that, when I add the statement to my project, VBE starts complaining about my usage of Filesystem.Dir, .FileOpen, .FileClose, GetAttr, .Print, .Rename ...

What's the scoop here?

Rien.
 
where is the FileSystem namespace?

All file operations that I know of are in System.IO where the File and Directory objects are.

-Rick

----------------------
 
This code used to work unitl I added the Imports statement:

strFile = FileSystem.Dir(strSearch, intAttr)
...
strFile = FileSystem.Dir()
...
FileSystem.FileClose(1)
...
FileSystem.FileOpen(1, "D:\VB\" & UndoLog, OpenMode.Output, OpenAccess.Write)
...
FileSystem.Print(1, """" & txtFrom.Text & """,""" & txtTo.Text & """" & vbCr)
...
attr = FileSystem.GetAttr(txtFrom.Text)
...
FileSystem.Rename(txtFrom.Text, txtTo.Text)
...
FileSystem.FileCopy(txtFrom.Text, txtTo.Text)

And now the system complains that all these are not members of System.IO.FileSystem...
 
We'll just flip it arround, remove the Imports System.IO line, and change
Code:
Directory.GetFiles(SourcePath)
to
Code:
System.IO.Directory.GetFiles(SourcePath)

-Rick



----------------------
 
Thanks Rick, that helped. It now does not flag my old code and also accepts the System.IO.Directory.GetFiles.

But now I get another problem. When the parameter I pass to the routine (SourcePath) is a textbox (txtSrc.Text) VBE bombs out completely, it even generates an error report and sends it away...
When I replace the textbox with "D:\TestSrc" it works as expected, also when I put that string into a variable. But when I try to copy the contents of the textbox into the same variable it bombs out again.
It's just like the guy who started this thread says: it does not seem to work from a textbox...

Rien.
 
define "bombs out completely"...

is an error getting thrown? or is the IDE shutting down (ie: Black box of impending doom shows up then VB.Net closes)

if an error is occuring, put the offending line in a try block. catch the exception and check it's .message.

Also, check the simple things.
1) Are you referencing the correct text box?
2) Are you using the .Text property of the text box?
3) Is there text in the text box?
4) Is the text in the text box a valid path?

if all else fails post a copy of the function and the call to it.

-Rick

----------------------
 
Hi Rick,

The thing is, your little example (above) works beautifully, even when I remove the Imports statement and change all the calls to System.IO.call and get the path from a textbox.
It's my own application... When I use the textbox in any way in the call to GetFiles, VBE does not even get around to bringing up the generated .exe. It yells "source code not available" and throws an InnerException: "An error occurred creating the form. See Exception.InnerException for details. The error is: The path is not of a legal form." So it seems to me it never gets to the sub where the "offending" statement is located.
Maybe I'm being punished for running VBE 2005 Beta1? :)

Rien.
 
When I change:

Dim myFiles() as String = System.IO.Directory.GetFiles(txtSrc.Text)

to:

Dim myFiles() As String
Try
myFiles = System.IO.Directory.GetFiles(txtSrc.Text)
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try

it works! Without the try block:

Dim myFiles() As String
myFiles = System.IO.Directory.GetFiles(txtSrc.Text)

it does not.

Rien (baffled).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top