Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

Rename a file and add the System Date and Time

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
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

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
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
SOLUTION
Avatar of vbturbo
vbturbo
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fernando Soto
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fordraiders

ASKER

vbturbo,
VB.net

Does not like this line

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


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.
Hillwaa,
vb.does not  like the "format" syntax  in the code.
 
What error are you getting?  

Format(Now, "yyyyMMddhhmm") works fine for me?
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
Got this to take at least the date



 Rename("C:\Dm2007\Outbox\" & FileName, "C:\Dm2007\Outbox\" & "Completed_" & Replace(FormatDateTime(Now(), 2), "/", "") & FileName)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial