473,804 Members | 3,271 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\fo lder\myfile.txt ") Then
rename \\server\share\ folder\myfile.t xt
\\server\share\ folder\myfile_d ate_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 11855

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***@goneloos e.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\fo lder\myfile.txt ") Then
rename \\server\share\ folder\myfile.t xt
\\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\folde r\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_RES ULT
rfrRenamed = 0
rfrFileNotExist = 1
rfrRenameFailed = 2
End Enum

Function RenameFile(File Name As String) As RENAME_FILE_RES ULT
Dim strFileName As String
Dim strExt As String
Dim strNow As String
Dim intInstr As Integer
Dim rfrRet As RENAME_FILE_RES ULT

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

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

strNow = Format(Now, " yyyymmdd hh_nn_ss")
strFileName = Left(strFileNam e, 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_FORMA T probably contains
' illegal filename characters
Name FileName As strFileName

If Len(Dir(strFile Name)) 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_RES ULT

strFN = "d:\text.tx t"

rfrRet = RenameFile(strF N)
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********@hot mail.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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
1470
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 against a cuttoff DateTime, and copies them if thet do not already exist. All progress is written to a log (a snippet is included below the code). The File.Exists() method returns false even when the file does in fact exist. Have I coded this...
7
9514
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 bb", "c:\cc dd" ); > But, I can't get it to work in my program, eg.
7
2688
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 " & (format(Date(),"yymmdd")) & ",pdf" So that the file name becomes: "Directory 050126.pdf" I have tried the NAME {oldfilename} AS {newfilename} as an event procedure In: On Close and On Deactivate.
3
2829
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 my ultimate goal is to write a deployment script to deploy the application with different filename and extension to a folder like the following devenv /Build "testCmd.sln"
5
2041
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
4330
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 that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I cleanup files that were uploaded -- but obviously left stranded when the users aborted/gave up writting...
1
2592
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? My code is creating a file correctly, but the CreationTime is (sometimes) set to the CreationTime of an old file that has previously been removed. The line of code creating the file is _LogFileWriter = newFile.CreateText()
4
2207
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 already existed and if so to delete it before renaming the uploaded file. Everything now works fine in the following code: $uploaddir = 'images/'; $uploadfile = $uploaddir . basename($_FILES); echo '<pre>'; if (move_uploaded_file($_FILES,...
1
5725
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 this is my codes, Set Upload = Server.CreateObject("Persits.Upload.1") Count = Upload.Save("d:\cmsupload\cimg\") for each File in upload.Files ' '...sme operation here..
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.