Results 1 to 17 of 17

Thread: [RESOLVED] [2005] cut and paste

  1. #1

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Resolved [RESOLVED] [2005] cut and paste

    hie guys

    i want to create a project which would cut and paste txt files one at a time from a directory to another at an interval of about a second apart. can anybody explain how i can do this.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] cut and paste

    Just add a timer to the form, set the Interval property to 1000 and enable it when you want the process to start.

    In the Timer Tick routine just add this:
    Code:
    My.Computer.FileSystem.MoveFile("\SourceFile.txt", "\NewDestinationFile.txt", True)
    Although, what if the previous file hasn't completed being transferred and you try to start another transfer? Would this cause an error? I don't know if the way you want to do this is exactly correct.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    i did clealy show were i needed help but thanks for that either. i would like to know how i would write the code to select only text files from the folder regardless of the text file name, cut it then paste to the destination folder.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] cut and paste

    You would use the Directory.GetFiles method. The documentation will show you how to filter the results.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    i have tryied this (below) and im an having a problem in passing the value of "filename1" to the movefile source and destination the reason being filename1 can not be converted to string. can somebody explain to me how i can pass the values such that i will have a unique value for each loop not specifying the name as on the example.

    Code:
    'Dim MyString As String 
    Dim di As New IO.DirectoryInfo("c:\GARA")
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim filename1 As IO.FileInfo
            For Each filename1 In diar1
                My.Computer.FileSystem.MoveFile("c:\GARA\test.txt", "C:\GARA1\test.txt", True)
                'MyString = MyString & CStr(filename1)
                'MsgBox "& filename1& "<br />""
                If  Then
                    Exit For
                End If
            Next

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] cut and paste

    Code:
    Dim di As New IO.DirectoryInfo("c:\GARA\")
    Dim diar1 As IO.FileInfo() = di.GetFiles("*.txt")  'add filter
    
    For Each filename1 As IO.FileInfo In diar1
    
         My.Computer.FileSystem.MoveFile(filename1.FullName, "C:\GARA1\" & filename1.Name, True)
              
    Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    Thanks So Much Stimbo.

    That Means If I Want To Set My Timer To Say 3 Seconds I Will Just Write The Code Under Timer1 And Set The Interval To 3000 Is It? Such That The Loop Will Run After 3 Seconds

  8. #8

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    anyone help please

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] cut and paste

    Help with what?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  10. #10

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    as i had explained ealier, i want my tool to move one text file at a time with an interval of 3 seconds. i made my declarations pablic and under timer i specified the move command and set my inteval but im having an error of which this is were i need help, "variable used before it is assigned a value" and if i put my index it also gives me the same err can not be indexed because of the above err'sand the code and declarations are as follows
    Code:
    Dim di As New IO.DirectoryInfo(txtsource.Text)
        Dim diar1 As IO.FileInfo() = di.GetFiles("*.txt")  'add filter
        Dim mycounter As Integer = 0
        Dim files As IO.FileInfo
    for the declaractions and for timer
    Code:
    My.Computer.FileSystem.MoveFile(files(mycounter).FullName, txtdestination.Text & files.Name, True)
            txtnowexporting.Text = files.FullName
            mycounter = mycounter = 1

  11. #11
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] cut and paste

    Yes you explained it earlier, the code has pretty much been written for you, but you didn't mention what the current problem is and I'm not a mind reader so didn't know what errors you were getting as you didn't explain or show the new code that was causing the problems.

    What is this supposed to do??
    mycounter = mycounter = 1

    Do you mean: myCounter = myCounter + 1

    I think the main problem is that you have declared the variable myCounter in one sub routine and are try to use it in another which you can't do. You have to declare the variable at the form level (usually under the Public Class Form1 line of code if you haven't renamed your form).
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  12. #12

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    Quote Originally Posted by GARAKWAZVO
    Code:
    Dim di As New IO.DirectoryInfo(txtsource.Text)
        Dim diar1 As IO.FileInfo() = di.GetFiles("*.txt")  'add filter
        Dim mycounter As Integer = 0
        Dim files As IO.FileInfo
    as on my above post i have declared mycounter on the public declarations and my concern is on the errors on how i can assign a value to files

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] cut and paste

    Stimbo asked a question:
    Quote Originally Posted by stimbo
    What is this supposed to do??
    mycounter = mycounter = 1

    Do you mean: myCounter = myCounter + 1
    Stimbo also said:
    Quote Originally Posted by stimbo
    you didn't mention what the current problem is and I'm not a mind reader so didn't know what errors you were getting as you didn't explain or show the new code that was causing the problems.
    You've now said this:
    Quote Originally Posted by GARAKWAZVO
    my concern is on the errors
    You haven't answered stimbo's question and you are still yet to tell us what errors you're talking about. Stimbo is not the only one here who isn't a mind-reader. If you really do want us to provide a solution then you will provide us with the information we need. How can we solve the problem if we don't know what the problem is?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    Quote Originally Posted by GARAKWAZVO
    Code:
    My.Computer.FileSystem.MoveFile(files(mycounter).FullName, txtdestination.Text & files.Name, True)
            txtnowexporting.Text = files.FullName
            mycounter = mycounter + 1
    the higlighted areas are giving "cannot be indexed because it has no default property". how can i get rid of that error.

    i have also corrected the line to what it should have looked like at the first place.

  15. #15
    Lively Member
    Join Date
    May 2004
    Posts
    72

    Re: [2005] cut and paste

    Hi,

    you define

    Code:
    Dim files As IO.FileInfo
    then you used in 2 ways

    Code:
    My.Computer.FileSystem.MoveFile(files(mycounter).FullName, txtdestination.Text & files.Name, True)
    txtnowexporting.Text = files.FullName
    So, "files" is an instance or a 1D array?

  16. #16
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] cut and paste

    That's where the problem is. You are trying to use a FileInfo object as an array which you can't. Here is what you probably want to do:
    vb Code:
    1. 'FORM level variables
    2.     Dim myCounter As Integer = 0
    3.     Dim myFiles() As String
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    6.     Handles MyBase.Load
    7.  
    8.         Try
    9.             myFiles = IO.Directory.GetFiles("C:\GARA\", "*.txt")
    10.         Catch ex As Exception
    11.             MessageBox.Show(ex.Message)
    12.         End Try
    13.  
    14.     End Sub
    15.  
    16.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    17.  
    18.         Try
    19.             If myCounter <= myFiles.Length - 1 Then
    20.                 My.Computer.FileSystem.MoveFile(myFiles(myCounter), "C:\GARA1\" & IO.Path.GetFileName(myFiles(myCounter)))
    21.                 myCounter += 1
    22.             End If
    23.         Catch ex As Exception
    24.             MessageBox.Show(ex.Message)
    25.         End Try
    26.  
    27.     End Sub
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  17. #17

    Thread Starter
    Member GARAKWAZVO's Avatar
    Join Date
    Mar 2007
    Location
    Zimbabwe
    Posts
    56

    Re: [2005] cut and paste

    thats exactly what i was looking for thanks so much
    GARA

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