Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename existing file with current date... 2

Status
Not open for further replies.

arravalli12

Programmer
May 3, 2005
62
0
0
US
I have written a code that copies from my shared drive and copies that file to my local drive.
my existing code is like:
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\Documents and Settings\moparikh\Desktop\test1.csv") Then
objFSO.DeleteFile "C:\Documents and Settings\moparikh\Desktop\test1.csv"
End If
objFSO.CopyFile "G:/Public/web_services/CustomerContacts/Customers.csv" , "C:\Documents and Settings\moparikh\Desktop\", OverwriteExisting
msg = " copied"
MsgBox msg.

This works fine.
Now I want to rename the file in my shared drive with same name + Current date ex: Customers-2005_12_14.csv.
Any suggestions will be appreciated.
Thanks
 
arravalli12,
this should get you started:
Code:
Dim char_date
char_date = Year(Now) & Right("0" & Month(Now),2) & Right("0" & Day(Now),2)
obj_FSO.CopyFile "G:/Public/web_services/CustomerContacts/Customers.csv" , "C:\Documents and Settings\moparikh\Desktop\" & char_date & ".csv"
you will just need to add your underscores.
regards,
longhair
 
Thanks longhair for quick reply and appreciate the code.
I need to rename my existing Customers.csv file in shared G drive (from Customers.csv to Customers-2005_12_14.csv).
For this I may need to use fso.MoveFile command.
I have never used this. Can you help me out.
Thanks
 
arravalli12,
you can use the fso.copyfile command. for example:
Code:
Option Explicit

Dim obj_FSO
Dim char_Dir1
Dim obj_ArcFile1
Dim obj_Folder1
Dim obj_FileColl1
Dim obj_File
Dim obj_File1
Dim char_date

char_Dir1 = "G:\Public\web_services\CustomerContacts\" 'target dir

char_date = Year(Now) & "_" & Right("0" & Month(Now),2) & "_" & Right("0" & Day(Now),2)
Set obj_FSO = CreateObject("Scripting.FileSystemObject")
Set obj_Folder1 = obj_FSO.GetFolder(char_Dir1)
Set obj_FileColl1 = obj_Folder1.Files
Set obj_ArcFile1 = obj_FSO.CreateTextFile("c:Documents and Settings\moparikh\Desktop\ArcFile1_" & char_date & ".txt", True)

For each obj_File in obj_FileColl1
	If Left(obj_File,12) = "G:\Customers" Then
			obj_ArcFile1.WriteLine(obj_File & "    " & obj_File.DateLastModified & "    Copied")
			obj_FSO.CopyFile obj_File , "C:\Documents and Settings\moparikh\Desktop\Customers-" & char_date & ".csv"
	End If
Next
obj_ArcFile1.Close


'clear objects
Set obj_FSO = Nothing
Set obj_Folder1 = Nothing
Set obj_FileColl1 = Nothing
Set obj_File = Nothing
Set obj_ArcFile1 = Nothing
Set obj_File1 = Nothing
put togeather real quick but should work - you may have to tweak a bit. it should copy your file from g: to c: and make an achive file wich has the last modified time of the original file.

regards,
longhair
 
Thanks longhair and PHV for real quick help and learning. You both deserve stars.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top