Home All Groups Group Topic Archive Search About

Displaying copied file names in textbox

Author
30 Apr 2006 12:30 AM
mmalinsky@gmail.com
I have the following code that I borrowed from this NG to copy files
from one location to another:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Const FOLDER_FROM As String = "D:\My Documents" ' Folder to
copy from
        Const FOLDER_TO As String = "D:\My Documents\Miscellaneous2"
'Folder to create above folder in
        Dim newDir As DirectoryInfo =
Directory.CreateDirectory(FOLDER_TO & "\" & New
DirectoryInfo(FOLDER_FROM).Name)
        CopyFiles(New DirectoryInfo(FOLDER_FROM), newDir)
    End Sub
    Private Sub CopyFiles(ByVal FolderFrom As DirectoryInfo, ByVal
FolderTo As DirectoryInfo)
        Dim Files() As String = Directory.GetFiles(FolderFrom.FullName)
        Dim SF() As String =
Directory.GetDirectories(FolderFrom.FullName)
        Dim Var As Integer
        Dim start, finish As Double
        For Var = Files.GetLowerBound(0) To Files.GetUpperBound(0)
            File.Copy(Files(Var), FolderTo.FullName & "\" & New
FileInfo(Files(Var)).Name)
        Next
        For Var = SF.GetLowerBound(0) To SF.GetUpperBound(0)
            Dim dName As String = New DirectoryInfo(SF(Var)).Name
            Dim newD As New DirectoryInfo(FolderTo.FullName & "\" &
dName)
            newD.Create()
            CopyFiles(New DirectoryInfo(FolderFrom.FullName & "\" &
dName), newD)
            Label2.Text = (Files(Var))
            Label2.Refresh()
        Next
    End Sub

My question is why the line Label2.Text=(Files(Var)) does not work.  I
get the following:

     An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in
     update.exe

     Additional information: Index was outside the bounds of the array.

TIA
Mike

Author
30 Apr 2006 11:32 PM
Göran_Andersson
Because you are looping throught the SF array and are trying to access
an element in the Files array. The SF array has obviously fewer elements
than the Files array.

mmalin***@gmail.com wrote:
Show quoteHide quote
> I have the following code that I borrowed from this NG to copy files
> from one location to another:
>
>     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>         Const FOLDER_FROM As String = "D:\My Documents" ' Folder to
> copy from
>         Const FOLDER_TO As String = "D:\My Documents\Miscellaneous2"
> 'Folder to create above folder in
>         Dim newDir As DirectoryInfo =
> Directory.CreateDirectory(FOLDER_TO & "\" & New
> DirectoryInfo(FOLDER_FROM).Name)
>         CopyFiles(New DirectoryInfo(FOLDER_FROM), newDir)
>     End Sub
>     Private Sub CopyFiles(ByVal FolderFrom As DirectoryInfo, ByVal
> FolderTo As DirectoryInfo)
>         Dim Files() As String = Directory.GetFiles(FolderFrom.FullName)
>         Dim SF() As String =
> Directory.GetDirectories(FolderFrom.FullName)
>         Dim Var As Integer
>         Dim start, finish As Double
>         For Var = Files.GetLowerBound(0) To Files.GetUpperBound(0)
>             File.Copy(Files(Var), FolderTo.FullName & "\" & New
> FileInfo(Files(Var)).Name)
>         Next
>         For Var = SF.GetLowerBound(0) To SF.GetUpperBound(0)
>             Dim dName As String = New DirectoryInfo(SF(Var)).Name
>             Dim newD As New DirectoryInfo(FolderTo.FullName & "\" &
> dName)
>             newD.Create()
>             CopyFiles(New DirectoryInfo(FolderFrom.FullName & "\" &
> dName), newD)
>             Label2.Text = (Files(Var))
>             Label2.Refresh()
>         Next
>     End Sub
>
> My question is why the line Label2.Text=(Files(Var)) does not work.  I
> get the following:
>
>      An unhandled exception of type 'System.IndexOutOfRangeException'
> occurred in
>      update.exe
>
>      Additional information: Index was outside the bounds of the array.
>
> TIA
> Mike
>