Visual Basic Script that can be used to rename a file

Here I am trying to automatically load log files into a MS Access file. Log files are actually txt files which need their extension changed if like me you wish to automate their import into MS Access. The following Visual Basic Script takes a file called FS_LS.log and changes it to FS_LS.txt. (I have another function that then takes this file and imports it into an MS Access application.

For safety this script creates a copy of the FS_LS log and renames it with a txt extension combining hours and minutes into the title. Thus it needs to be run one minute apart otherwise it will throw an error.

For testing purposes I have the following line

Fso.Copyfile "C:\Users\mark\Documents\vbs\" & vardatefile, "C:\Users\mark\Documents\vbs\" & vardatefile3

If you don’t have a feed to somekind of constant stream of log files this will copy the txt back to a log file ready to be copied again.(something I found useful for testing). Now next you want to call this from within your MS Access application prior to your data import function using the previously posted function.

Dim Fso
Dim vardatefile
Dim vardatefile2
Dim vardatefile3
Dim varhoursleft
Dim varminsright
Dim varnow

varnow= FormatDateTime(now,4)
varhoursleft = left(varnow,2)
'msgbox varhoursleft

varminsright = right(varnow,2)
'msgbox varminsright
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
vardatefile2 = "FS_LS.txt"
vardatefile3 = "FS_LS.log"
vardatefile = "FSLS-" & varhoursleft & varminsright & ".txt"
Fso.MoveFile "C:\Users\Mark\Downloads\FS_LS.log", "C:\Users\Mark\Downloads\" & vardatefile
Fso.Copyfile "C:\Users\Mark\Downloads\" & vardatefile, "C:\Users\Mark\Downloads\" & vardatefile2
'Fso.Copyfile "C:\Users\Mark\Downloads\" & vardatefile, "C:\Users\Mark\Downloads\" & vardatefile3

Set FSO = nothing

About Mark

Mark Brooks a forty something individual working and living in and around Edinburgh
This entry was posted in All, VBS Scripts. Bookmark the permalink.