Results 1 to 20 of 20

Thread: [RESOLVED] My.Computer.FileSystem.CopyFile

  1. #1

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Resolved [RESOLVED] My.Computer.FileSystem.CopyFile


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim result As DialogResult = Me.OpenFileDialog1.ShowDialog()
    If result = Windows.Forms.DialogResult.OK Then
    For Each sFilePath As String In Me.OpenFileDialog1.FileNames
    Me.ListBox1.Items.Add(sFilePath)
    Next sFilePath
    End If
    End Sub
    Private Function getFileName(ByVal sFilePath As String) As String
    Dim iLenFilePath As Integer = sFilePath.Length
    Dim iLastSlashPos As Integer = Strings.InStrRev(sFilePath, "textbox1.text")
    Return Strings.Right(sFilePath, iLenFilePath - iLastSlashPos)
    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For i As Integer = 0 To Me.ListBox1.SelectedItems.Count - 1
    Dim sFileName = getFileName(Me.ListBox1.SelectedItems.Item(i))
    Try
    My.Computer.FileSystem.CopyFile(Me.ListBox1.SelectedItems.Item(i), "textbox1.text" & sFileName)
    MessageBox.Show("Copy was successfull for file " & sFileName)
    Catch ex As Exception
    MessageBox.Show("Error while copying file " & sFileName & vbNewLine & "Error message: " & ex.Message)
    End Try
    Next i

    End Sub





    hello this is my code
    I can select files to A listbox, but now have to copy them one by one
    to the directory I have choose in textbox1.text(the location), but how can I copie the selected files that in the listbox at the same time
    so I have selected the files That I wont to copy
    I say In the textbox1.text the location where to copy the files
    end now I want to have i button to copy the selected files At one time In the directory where to copy can someon help me
    Last edited by NeedHelp01; Jun 11th, 2007 at 02:42 AM.

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    I really don't understand what you mean by copying them at the same time.
    try to do a basic copy paste of multiple files in the explorer, you can see that the files are created one by one to the destination.
    I think that the way you are doing it now is the only of copying multiple files to the same destination. It allows to copy all the files with one click rather than the same number of clicks as there are files to be copied.

  3. #3

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    Ok
    stap 1: seleced the files
    stap 2: copy the selected files to de locations where to copy the selected files,the location can you give in textbox1.text.
    stap to copy file.

    If I copy the files, I can not open the openfile dialog again to copy notter files I must close the project en open the project again how to start the openfiledialog

    how can I the start openfiledialog without starting entirely project again

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    drag a button open in your form and name it open dialog

    vb Code:
    1. private sub buttonDialog()
    2.     openFileDialog1.showDialog()
    3. end sub

  5. #5

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    yes I thit that

    okay I have done that but when I choose a file he don't set in de listbox how come??

  6. #6
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    you have to implement the code you used in the form1_load event.
    Select the files
    click the ok button in the openFileDialog
    add the selected files to the listbox

  7. #7

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    oke thanx

    how can I refresh the listbox.
    When i select the files, I can not refresh the listbox to copy other files to a other location??
    Last edited by NeedHelp01; Jun 11th, 2007 at 03:30 AM.

  8. #8

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    when I select a file to copy.
    he copy the files to the same directory from where I copy the file
    how to copy the files to a other directory

    stap1 copy files from D:\templates
    stap2 copy the selected to D:templates\C-drive
    how can It that he don't copy the files to D:templates\C-drive
    I have a textbox where I give the location where to copy but he don't do that

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim result As DialogResult = Me.OpenFileDialog1.ShowDialog()
    4.         If result = Windows.Forms.DialogResult.OK Then
    5.             For Each sFilePath As String In Me.OpenFileDialog1.FileNames
    6.                 Me.ListBox1.Items.Add(sFilePath)
    7.             Next sFilePath
    8.         End If
    9.  
    10.     End Sub
    11.  
    12.     Private Function getFileName(ByVal sFilePath As String) As String
    13.  
    14.         Dim iLenFilePath As Integer = sFilePath.Length
    15.         Dim iLastSlashPos As Integer = Strings.InStrRev(sFilePath, "\")
    16.         Return Strings.Right(sFilePath, iLenFilePath - iLastSlashPos)
    17.  
    18.     End Function
    19.  
    20.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    21.  
    22.         For i As Integer = 0 To Me.ListBox1.SelectedItems.Count - 1
    23.             Dim sFileName = getFileName(Me.ListBox1.SelectedItems.Item(i))
    24.             Try
    25.                 My.Computer.FileSystem.CopyFile(Me.ListBox1.SelectedItems.Item(i), "textbox1.text" & sFileName)
    26.                 MessageBox.Show("Copy was successfull for file " & sFileName)
    27.             Catch ex As Exception
    28.                 MessageBox.Show("Error while copying file " & sFileName & vbNewLine & "Error message: " & ex.Message)
    29.             End Try
    30.         Next i
    31.  
    32.     End Sub
    33.  
    34.     Private Sub cmdSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelectFile.Click
    35.  
    36.         Dim result As DialogResult
    37.         result = FolderBrowserDialog1.ShowDialog
    38.  
    39.         If result = Windows.Forms.DialogResult.OK Then
    40.             TextBox1.Text = FolderBrowserDialog1.SelectedPath
    41.         End If
    42.  
    43.     End Sub

  9. #9
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    vb Code:
    1. My.Computer.FileSystem.CopyFile(Me.ListBox1.SelectedItems.Item(i), "textbox1.text" & sFileName)

    what does this line do?
    it saves the file (c:/mytest.txt) to the folder named textbox1.textmytest.txt

    when you run the program, do you get the successful or unsuccessfull messagebox?

  10. #10

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    hey say copy file succesful messagebox

    yess this is my big problem
    it saves the file (c:/mytest.txt) to the folder named textbox1.textmytest.txt

    but how else can I give the directory where to copy the file
    because I have a brows button to say in textbox1.text the path of the where to copy the selected files how can I say in the listbox to read from textbox1.text??

  11. #11
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: My.Computer.FileSystem.CopyFile

    i think
    Code:
    My.Computer.FileSystem.CopyFile(Me.ListBox1.SelectedItems.Item(i), "textbox1.text" & sFileName)
    should be

    Code:
    My.Computer.FileSystem.CopyFile(Me.ListBox1.SelectedItems.Item(i), textbox1.text & sFileName)
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  12. #12
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    remove the quotes around the textbox1.text and this will give you the value inside the textbox

    vb Code:
    1. My.Computer.FileSystem.CopyFile(Me.ListBox1.SelectedItems.Item(i), textbox1.text & "/" & sFileName)

  13. #13

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    yess it works but a nother problem how to refresh the listbox
    when i start the project he keeps the selected files in the listbox
    how to refresh the listbox to empty the listbox

  14. #14
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    vb Code:
    1. listbox1.items.clear

  15. #15

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    how can I give in the form titelbar the command
    vb Code:
    1. listbox1.items.clear
    that I have a option in the titelbar to clear the listbox

  16. #16

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    else I have a lot of buttons
    I don't wont to have a lot of buttons an my form

  17. #17
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    the code that you are using in the form1_load event is exactly the same as the one you have under the openFiledailog button. this means that insted of having the same code twice, simulate the click of the openfiledialog in the load event.

    then use this code to clear the listbox everytime you select some new files
    vb Code:
    1. Dim result As DialogResult = Me.OpenFileDialog1.ShowDialog()
    2.         If result = Windows.Forms.DialogResult.OK Then
    3.             me.listbox1.items.clear
    4.             For Each sFilePath As String In Me.OpenFileDialog1.FileNames
    5.                 Me.ListBox1.Items.Add(sFilePath)
    6.             Next sFilePath
    7.         End If

    Quote Originally Posted by NeedHelp01
    else I have a lot of buttons
    I don't wont to have a lot of buttons an my form
    this is not true because I only see 2 buttons and a listbox on the form.
    on button to open the openfiledialog
    1 button to copy the files and the listox
    3 controls only

  18. #18

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    Quote Originally Posted by talkro
    this is not true because I only see 2 buttons and a listbox on the form.
    on button to open the openfiledialog
    1 button to copy the files and the listox
    3 controls only
    yes that is true

    my problem is solved as I can see
    maby you can see more thinks that I can fixe??


    maby de files I have selected In the openfiledialog
    he set the files now in the listbox from the listbox I copy them to the directory I give IN THE TEXTBOX.
    how can I automatic copy the files to the directory which I have indicated in the textbox

    I do not want each time to say yes copy to
    I want to have the listbox viseble on false
    wath I have selected in the openfiledialog copy automatic to the directory

  19. #19
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: My.Computer.FileSystem.CopyFile

    you can only have a button and a textbox on your form called copy files. This button will open the openFileDialog. When the user clicks on ok, you loop through the selected files and use the instruction my.computer.copyfiles() to save them in the location specified in the textbox

  20. #20

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: My.Computer.FileSystem.CopyFile

    but how can I copy automatic the selected files in the listbox with out to say whit's files. I want to copy from the list box, how to select automatic the files that come in the listbox I don't have to see wath files I have selected in the openfiledialog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width