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

Open if file less than 7 days old

Status
Not open for further replies.

cg084

Technical User
May 3, 2002
67
GB
Hello,

I am trying to write a macro that will check to see if a text file is less than 7 days old. If it is to open the file. The name of the file is fixed and is created though an automated query on a database. The file output is a pipe (|) delimited text file. The file is called. Cluster.txt.

Preferably I would like to test this before opening the file. But it is acceptable for the file to be open to test this.

Thank you for any assistance.
 
Function ShowFileInfo(filespec)
Dim fso as FileSystemObject
dim f as Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowFileInfo = f.DateCreated
End Function
or change the last line to ShowFileInfo = f.DateLastModified

Sam
 
p.s. add a reference to the Windows Script Host Object Model to your project.

Sam
 
Sorry, my brain is malfunctioning this AM.
change
Dim fso as FileSystemObject
to
Dim fso as object
and the project reference won't be necessary as this code uses late binding.

Sam
 
Sam,

Cheers for the help. I am still a little stuck though. The function seems to do not a lot. I have entered the funtion in to my personal.xls spreadsheet that loads by default when excel opens. From this file i have a macro that will open the file i need from a web server for example. And the file is called Cluster.txt. Is there any way to code this in to the function to display the properties for that file.

Cheers CG084
 
the calling code could be this:
Code:
Private sub YourMacro()
  dim strFilePath as string
  dim dtLastWeek as date
  dtLastweek = DateAdd("d", -7, Date)
  'Change C:\Test\Cluster.txt to your filepath
  strFilePath = "C:\Test\Cluster.txt"
  If ShowFileInfo(strFilePath) > dtLastWeek then
    'put your open code in here
  end if
end sub

Sam
 
Sam,

Greatly appreciate the help. It now works with a local or a named drive.

Your help is greatly appreciated.

There is still a problem in accessing the file on an internet server. My open file macro works with the same string but does not work in this method.

Due to the nature of the file. Here is a sample of what 1 address would be.


I have though about the case of the txt extension. Commas length of the path. IS there anything you can think of.

Thank you for your help.
CG084
 
Hi,

I just wanted to see if anyone had any thoughts with why a normal filepath works ok with the above but when i change to use a path to a webfile it does not.

Thank you.
CG084
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top