We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Append date and time to file name in vb.net

sykotex
sykotex asked
on
Medium Priority
601 Views
Last Modified: 2012-05-11
Hi
How can I append the date and time to a filename in vb.net.
i.e
error_20_04_2011_1705.log
Comment
Watch Question

Commented:
I think it is not difficult.

Dim myfile1
dim myfile2
myfile1 = "C:\myfile_s.txt"
myfile2 = "C:\Newfile_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".txt" 
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(myfile1) = true then
    If fso.FileExists(myfile2) = true then
        fso.DeleteFile(myfile2)
    End if
    fso.MoveFile myfile1, myfile2
End If 
Set FSO = Nothing

Open in new window


using the above code we can easily write the file.
Paul JacksonSoftware Engineer
CERTIFIED EXPERT
Top Expert 2011

Commented:
Are you creating this file yourself? Got some code you can post as a starting point?
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
omegaomegaDeveloper
CERTIFIED EXPERT

Commented:
Hello, sykotex,

But please please use "_yyyy_MM-dd_HHmm" so that:

  a) these files will be sorted in a reasonable order

  b) a name like "error_02_04_2011_1705.log" won't be misinterpreted by:
        - an American that doesn't know you are European.
        - a European that doesn't know you are American.
        - the rest of us, who are constantly confused by your date convention.

Cheers,
Randy


    -
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Great advice Randy!  =)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.