We help IT Professionals succeed at work.

Rename a file and add the System Date and Time

Fordraiders
Fordraiders asked
on
277 Views
Last Modified: 2008-02-01
vb.net 2003

I need to rename a file and ad the system date and system time?
Is this possible?
Dim Filename as "MyWork.xls"
RenameFile("C:\Dm2007\Outbox\" & FileName, "Completed_" & FileName)

Thanks
fordraiders
Comment
Watch Question

Commented:
Hi fordraiders,

Try this:

        Dim Path As String = "C:\Dm2007\Outbox\"
        Dim Filename As String = "MyWork.xls"
        System.IO.File.Move(Path & Filename, Path & "Completed_" & Filename)

Cheers,
Hillwaaa

Commented:
fordraiders,

although you should probably add some error checking like this:

        Dim Path As String = "C:\temp\"
        Dim Filename As String = "Test.doc"
        If System.IO.File.Exists(Path & Filename) Then
            If Not System.IO.File.Exists(Path & "Completed_" & Filename) Then
                System.IO.File.Move(Path & Filename, Path & "Completed_" & Filename)
            Else
                Messagebox.show("Can't rename file as destination file already exists.")
            End If
        Else
            Messagebox.show("Can't find target file.")
        End If
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi fordraiders;

This should do what you want.

        Dim Filename As String = "MyWork.xls"
        Dim NewFilename As String = Path.GetFileNameWithoutExtension(Filename)
        NewFilename &= "-" & Now.ToString("MMddyyyy-hhmmss")
        NewFilename &= Path.GetExtension(Filename)
        File.Move("C:\Dm2007\Outbox\" & Filename, "C:\Dm2007\Outbox\" & NewFilename")

Fernando
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
vbturbo,
VB.net

Does not like this line

NewName = "C:\Dm2007\Outbox\" & FileName, "Completed_" & FileName & DateAndTime


Commented:
That's because DateAndTime will probably have the "\" character in the date part - which the file system won't like :)

This is why I used the Format() in my last post.

Author

Commented:
Hillwaa,
vb.does not  like the "format" syntax  in the code.
 

Commented:
What error are you getting?  

Format(Now, "yyyyMMddhhmm") works fine for me?

Author

Commented:
its just underlined  

syntax is not correct....

I got this to work...
But When I try  to add date and time
It does not like it....


Rename("C:\Dm2007\Outbox\" & FileName, "C:\Dm2007\Outbox\" & "Completed_" & FileName)


Thanks
fordraiders

Author

Commented:
Got this to take at least the date



 Rename("C:\Dm2007\Outbox\" & FileName, "C:\Dm2007\Outbox\" & "Completed_" & Replace(FormatDateTime(Now(), 2), "/", "") & FileName)
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*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.