locked
How to rename file from visual basic

    Question

  • Hello,

    I want to rename a file from my vb application, to append system timestamp to the name of the file. How do i do this please?

    Thanks


    tolu

    Tuesday, September 25, 2012 7:12 AM

Answers

  • Hi,

    you could use DateTime.Now to get the current date and time and access its Properties like

                DateTime dt = DateTime.Now;
    
                string s = dt.Month.ToString() + "." + dt.Day.ToString() + "." + dt.Year.ToString() + "_" + dt.Hour.ToString();

    You also could access theMinute and Second Properties like above...

    Note that I dont use / as a separator, because the filesystem wouldnt allow it since its a DirectorySeparatorChar.

    Edit: I saw its a VB Forum code will be updated soon...

    Here's the VB Version

            Dim dt As DateTime = DateTime.Now
            Dim s As String = dt.Month.ToString() & "." & dt.Day.ToString() & "." & dt.Year.ToString() & "_" & dt.Hour.ToString()

    Regards,

      Thorsten






    Tuesday, September 25, 2012 2:20 PM

All replies

  • Hi,

    to rename see:

    http://msdn.microsoft.com/en-us/library/5w05844e.aspx

    Regards,

      Thorsten

    • Marked as answer by newbie-tolu Tuesday, September 25, 2012 9:36 AM
    • Unmarked as answer by newbie-tolu Tuesday, September 25, 2012 10:09 AM
    Tuesday, September 25, 2012 9:02 AM
  • Thank you very much. Got it, but am not able to add the date and time to the new file name. Look i have something like this, dont know if i have done it correctly

    My.Computer.FileSystem.RenameFile("result.txt", "result" + "_" + Today + "_" + TimeOfDay + ".txt")

    i want the resultant file to be something like this

    result_10/12/2001_19:30.txt

    Thanks


    tolu


    • Edited by newbie-tolu Tuesday, September 25, 2012 10:11 AM
    Tuesday, September 25, 2012 9:36 AM
  • Hi,

    you could use DateTime.Now to get the current date and time and access its Properties like

                DateTime dt = DateTime.Now;
    
                string s = dt.Month.ToString() + "." + dt.Day.ToString() + "." + dt.Year.ToString() + "_" + dt.Hour.ToString();

    You also could access theMinute and Second Properties like above...

    Note that I dont use / as a separator, because the filesystem wouldnt allow it since its a DirectorySeparatorChar.

    Edit: I saw its a VB Forum code will be updated soon...

    Here's the VB Version

            Dim dt As DateTime = DateTime.Now
            Dim s As String = dt.Month.ToString() & "." & dt.Day.ToString() & "." & dt.Year.ToString() & "_" & dt.Hour.ToString()

    Regards,

      Thorsten






    Tuesday, September 25, 2012 2:20 PM
  • Thanks, that was perfect answer, what i wanted. Thanks

    tolu

    Wednesday, September 26, 2012 10:20 AM
  • That was a "Visual Basic" solution.  The "VB.NET" solution is to use System.IO.File.Move() to rename files.

    http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx  File.Move

    Hope this helps.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, September 26, 2012 2:11 PM