473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rename File if Exist with DateTime

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
3 11808

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
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
.... 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1429
by: Tim Richardson | last post by:
I am attempting to copy some files from one machine to another. I am having difficulty with the File.Exists() in the code below. The program enumerates the files in a directory, checks them...
7
9389
by: Tom | last post by:
I'm having a problem using a path with spaces as a parameter to os.rename() in a program on WinXP. This works fine at the command line (where the folder "c:\aa bb" exists) > os.rename( "c\aa...
7
2664
by: Chuck | last post by:
Problem: to rename a file sent to a PDF writer A macro opens a report in print mode the printer is a pdf writer The report name is: "Directory.pdf" The desired name is: "Directory " &...
3
2810
by: Mullin Yu | last post by:
i've created a console application, and by default the filename will be my project/solution name, ie. testCmd.exe but, can i change it during the build/deployment process to testCmdNew.com ...
5
2025
by: microsoft.news.com | last post by:
I need to save a file in this format: fileName_YYYYMMdd.txt. I forget the syntax to get the date in the file name, could some one help me out on this one?
13
4281
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
2557
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
4
2184
by: Dynamo | last post by:
I am trying to upload a file and then rename the file as something else. Everything worked fine unless the renamed filename already existed. So I added some extra code to check if the filename...
1
5687
by: achotto | last post by:
hi, i try to upload a multiple image files. after that i will rename the files name. the problem is when i upload a 2 or more same files name exp-goal.jpg, it will return "files already exist". ok...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.