none
move file one by one according to the date they were created RRS feed

  • Question

    • Hello I have a folder with multiple files that I need to move one by one. but according to the date and time that were created starting with the oldest one until ending with the most recent one. I have this code but I move them all at once. Thank you
          Dim files As List(Of FileInfo) = (From file As FileInfo In New DirectoryInfo("D:\NC\").GetFiles("*.Dat")
                                                Order By file.CreationTime
                                                Select file).ToList()
      
              Try
      
                  For Each file As FileInfo In files
      
                      Dim newFileName As String = String.Concat(file.CreationTime.Ticks, ".Dat")
                      file.MoveTo(Path.Combine("D:\hist\", newFileName))
      
                  Next
      
              Catch ex As IOException
      
                  MessageBox.Show(ex.Message)
      
              Catch ex As Exception
      
                  MessageBox.Show(ex.Message)
      
              End Try
      
          End Sub

    Sunday, January 21, 2018 6:07 AM

All replies

    I have this code but I move them all at once.

    The code you have posted will move the files one after the other in the sorted order.  You should indicate what you need to do that this code is not doing. For instance, do you mean that each file should only be moved when the user clicks an OK button?   Or do you mean that there should be some delay between each move?  Or something else?

    • Marked as answer by cjcastillo Sunday, January 21, 2018 8:17 AM
    • Unmarked as answer by cjcastillo Sunday, January 21, 2018 8:17 AM
    Sunday, January 21, 2018 7:33 AM
  • Hello,

    Correct, each file will move white the click event of a button. from the old to the most recent. one by one.

    thank you vey much.

    Sunday, January 21, 2018 8:43 AM
  • Hello,

    Correct, each file will move white the click event of a button. from the old to the most recent. one by one.

    thank you vey much.

    I think we miss something. The sense. Or is this just a personal goal to solve?

    You know, when persons find code on Internet they start using it, and without sense this is a wrong way, which as far as I can see now serves nothing. 

    A file can be presented in a certain way, but that is not how it is stored.


    Success Cor


    Sunday, January 21, 2018 9:47 AM
  • Correct, each file will move white the click event of a button. from the old to the most recent. one by one.

    Put the code to move the file in the button click event handler.

    Select the file to move using a counter as the index into the list.  This counter should be a variable declared at the class (eg, form) level, so that it can be initialised to 0 when you first create the list.  The code that does the moving can increment the list index, checking for end of list.

    Sunday, January 21, 2018 10:36 AM
  • Correct, each file will move white the click event of a button. from the old to the most recent. one by one.

    Put the code to move the file in the button click event handler.

    Select the file to move using a counter as the index into the list.  This counter should be a variable declared at the class (eg, form) level, so that it can be initialised to 0 when you first create the list.  The code that does the moving can increment the list index, checking for end of list.

    @Acamar,

    Dangerous, if another process is adding files or deleting files this can become a big mess. 


    Success Cor

    Sunday, January 21, 2018 10:45 AM
  • Check this too:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Try
    
                Dim file As FileInfo = (From f As FileInfo In New DirectoryInfo("D:\NC\").GetFiles("*.Dat")
                                        Order By f.CreationTime
                                        Select f).FirstOrDefault
    
                If file Is Nothing Then
                    MessageBox.Show("No more files")
                Else
                    Dim oldFileName = file.Name
                    Dim newFileName As String = String.Concat(file.CreationTime.Ticks, ".Dat")
                    file.MoveTo(Path.Combine("D:\hist\", newFileName))
                    MessageBox.Show("File moved: " & oldFileName & " to " & newFileName)
                End If
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message)
    
            End Try
        End Sub
    

    • Edited by Viorel_MVP Sunday, January 21, 2018 11:01 AM
    Sunday, January 21, 2018 10:59 AM
  • Dangerous, if another process is adding files or deleting files this can become a big mess. 

    Also pointless, without displaying the filename about to be moved, providing a Yes/No option for the move, and a Cancel button to cancel the whole process.   The task makes no sense.

    Sunday, January 21, 2018 8:25 PM