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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename File

Status
Not open for further replies.

vespin

Technical User
Sep 17, 2001
17
US
Greetings,

I'm really new to visual basic, but I'm attempting to create a program that will rename two files on a certain date. The files are:

c:\orrtax\2001\sys\ot2001.bat and c:\orrtax\2001\sys\ot2001nd.bat and I want to have them renamed on october 31st 2003. Does anyone have any sample code that will help with this?

In the meantime I will search for a book.

Kelly

I would use a batch file and put it in the scheduler, but it's for a friend who has win 95.

 
Kelly,

Here's some code to get you started:
-----------------------------------------------
Dim m_objFSO As Scripting.FileSystemObject
Dim dtToday As Date
Dim oldFile1 as String
Dim oldFile2 as String

Set m_objFSO = New Scripting.FileSystemObject

dtToday = Now()
oldFile1 = "c:\orrtax\2001\sys\ot2001.bat"
oldFile2 = "c:\orrtax\2001\sys\ot2001nd.bat"

newName1 = "whatever.bkup"
newName2 = "whatever2.bkup"

If dtToday > "10/30/2003" Then
m_objFSO.CopyFile oldFile1, newName1, True
m_objFSO.CopyFile oldFile2, newName2, True
m_objFSO.DeleteFile oldFile1, True
m_objFSO.DeleteFile oldFile2, True
End If
-----------------------------------------------

The only solution that comes to mind is to create an .exe from the above code and place the .exe in the startup folder of the computer. This way it will check the date every time the pc is rebooted to see if it needs to delete the files. You may want to either add another check to make sure the file exists or add another couple of lines of code to delete the .exe from the startup folder once operation is carried out on 10/31/2003 (or the first time the pc is logged onto after that date.

I hope this answers your question.

~Kevin
 

If Date = #10/4/2003# Then
FileCopy "c:\orrtax\2001\sys\ot2001.bat", "c:\orrtax\2001\sys\NewName.bat"
Kill "c:\orrtax\2001\sys\ot2001.bat"
End If [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

By the way, in the first example given by kshanes you can shorten if by removing the CopyFile and DeleteFile and just coding:

m_objFSO.MoveFile oldFile1, newName1
m_objFSO.MoveFile oldFile2, newName2
[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Use the file system object but just use the Name property.

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
f.Name = "NewFileName.bat"
 
Wow I really appreciate your help! The only problem that I have come across is if I delete those files ahead of time I get a path not found. I started vb yesterday, but Im sure I can find something that will look if the file exists.

Again I really appreciate your help. All I have is this, but it works...

If Date = #10/4/2002# Then
FileCopy "c:\orrtax\2002\sys\ot2002.bat", "c:\orrtax\2002\sys\ot2002.old"
Kill "c:\orrtax\2002\sys\ot2002.bat"
FileCopy "c:\orrtax\2002\sys\ot2002nd.bat", "c:\orrtax\2002\sys\ot2002nd.old"
Kill "c:\orrtax\2002\sys\ot2002nd.bat"

Else: End
End If
 
Is there a way to add some file checking with the code I have? If the file doesn't exist I get a file not found error message and if the directory doesn't exist I get a path not found. If there a way to run a function that will look for the files and if they exist and the date is a specific date the files will be renamed?
 
If DIR(&quot;c:\orrtax\2002\sys\ot2002.bat&quot;) <>vbNullString Then
'File Exists!
'Code to rename
Else
'Files Doesn't exist
End If


Of course, you can use the FSO, but if this is just a samll exe, and the file handling is simple, then why bother?

Otherwise, I also like using the FSO in my larger projects where alot more file handling (and/or more complex file handling) takes place. [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top