423,822 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,822 IT Pros & Developers. It's quick & easy.

Rename File if Exist with DateTime

P: n/a
Hello All,

I would i check to see if a file exist and if it does exist, then rename the
file with the date and time attached to the name:

Example:

If File.Exists("\\server\share\folder\myfile.txt") Then
rename \\server\share\folder\myfile.txt
\\server\share\folder\myfile_date_time.txt
End If

I need to have the date and time added because the file can be generated
more than once a day and we do not want to over write the existing.

Would I need to crreate a function and then call that function in a macro

Thanks for any and all help

Argus
Aug 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

look up format and Date or Now in Help...

now will return a date/time, e.g.
7/31/2006 9:44:33 PM
so you could use format to strip out the spaces and all that fun stuff.
Then just use Name to rename the file.

Aug 1 '06 #2

P: n/a
On Tue, 01 Aug 2006 02:40:37 GMT, "OdAwG" <Od***@goneloose.comwrote:
>Hello All,

I would i check to see if a file exist and if it does exist, then rename the
file with the date and time attached to the name:

Example:

If File.Exists("\\server\share\folder\myfile.txt") Then
rename \\server\share\folder\myfile.txt
\\server\share\folder\myfile_date_time.txt
End If

I need to have the date and time added because the file can be generated
more than once a day and we do not want to over write the existing.

Would I need to crreate a function and then call that function in a macro

Thanks for any and all help

Argus
You're saving the file as a txt file so the date and time can be added when the
file is saved. Make a macro to save the file. first command:

Open Report: exact report name
OutputTo.
Output type: Report, Output Name: The exact name of the report
Output Format: MS-DOS TEXT
Output File: "\\server\folder\myfile "
&(Format(Now(),"yymmdd-hhmmss AM/PM"))&".txt"

With quotes as shown. Unwrap text. Note: there is no .txt after myfile, but
there is a space before the closing quote.

The saved file will be: myfile 060801-084537 AM

Just a wizzard prodder
Chuck
--
Aug 1 '06 #3

P: n/a
.... you need Dir as well to find if the file exists.

A quick example of how this might be implemented

'******************************
Option Compare Database
Option Explicit

Enum RENAME_FILE_RESULT
rfrRenamed = 0
rfrFileNotExist = 1
rfrRenameFailed = 2
End Enum

Function RenameFile(FileName As String) As RENAME_FILE_RESULT
Dim strFileName As String
Dim strExt As String
Dim strNow As String
Dim intInstr As Integer
Dim rfrRet As RENAME_FILE_RESULT

' This determines the format of the date/time string
' you can change this butyou can't include any illegal
' filename charactrs
Const DATE_TIME_FORMAT = "yyyymmdd hh:nn:ss"
strFileName = Trim(FileName)

If Len(Dir(strFileName)) 0 Then
intInstr = InStrRev(strFileName, ".")
If intInstr 0 Then
strExt = Mid(strFileName, intInstr)
End If

strNow = Format(Now, " yyyymmdd hh_nn_ss")
strFileName = Left(strFileName, Len(strFileName) - Len(strExt))

strFileName = strFileName & strNow & strExt
' If you get an error 5 at this point then the format you've
' set for DATE_TIME_FORMAT probably contains
' illegal filename characters
Name FileName As strFileName

If Len(Dir(strFileName)) 0 Then
rfrRet = rfrRenamed
FileName = strFileName
Else
rfrRet = rfrRenameFailed
End If
Else
rfrRet = rfrFileNotExist
End If

RenameFile = rfrRet
End Function

Function TestRenameFile()
' This is a sample call
Dim strFN As String
Dim rfrRet As RENAME_FILE_RESULT

strFN = "d:\text.txt"

rfrRet = RenameFile(strFN)
Select Case rfrRet
Case rfrRenamed
MsgBox "The file was successfully renamed to '" & strFN & "'"
Case rfrRenameFailed
MsgBox "The file '" & strFN & "' was not renamed"
Case rfrFileNotExist
MsgBox "The file '" & strFN & "' does not exist"
End Select
End Function
'******************************
--

Terry Kreft
<pi********@hotmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>
look up format and Date or Now in Help...

now will return a date/time, e.g.
7/31/2006 9:44:33 PM
so you could use format to strip out the spaces and all that fun stuff.
Then just use Name to rename the file.

Aug 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.