Help ! I want read always the last report saved

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Help ! I want read always the last report saved

Post by wandila » Sat Jul 22, 2006 7:31 am

I want read always the last report saved and send to a variable. These reports are saved all time. Example:
Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 15.10.txt
Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 16.11.txt
Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 16.35.txt
Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 22.10.txt

In this case i want read: Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 22.10.txt and store in a variable to use string functions later.

Ty for your time
Regards

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Jul 22, 2006 7:51 am

The last report saved should have a later file date than the rest, so you shouldn't need to parse the file names, and should just be able to use this script here:

http://www.mjtnet.com/forum/viewtopic.php?t=1462

This finds the last file in a directory based on its file date. You shouldn't need to parse the file names.

If I am wrong in your case then we could still write code to parse the file names and work out the latest based on the date and time portions of the name. Let me know if the above doesn't work and I can help you with this instead.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Sat Jul 22, 2006 8:04 am

There are others files in directory.
I want parse the names , but i want take just the main line ignoring the date and hour in name. Then i take the most updated file with "Updated Report - 'xxxxxxxx'" in name and read the file.
Example:
Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 15.10.txt
Updated Report - 'Car Sales' - Date 2006.7.22 - Hour 17.11.txt
Last Sales - 'Truck Sales' - Date 2006.7.22 - Hour 14.11.txt
Last Sales - 'Truck Sales' - Date 2006.7.22 - Hour 18.19.txt
Last Acquisitions - 'Motorbike' - Date 2006.7.22 - Hour 12.10.txt
Last Acquisitions - 'Motorbike' - Date 2006.7.22 - Hour 14.10.txt

I want read and store into a variable the most updated:
Updated Report - 'Car Sales' - XXXXXX.txt
Last Sales - 'Truck Sales' - XXXXXXX.txt
Last Acquisitions - 'Motorbike' - XXXXXXX.txt

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Jul 22, 2006 8:20 am

Try this:

Code: Select all

VBSTART

Function LastModified(folderspec,filespec)
   Dim fso, f, f1, fc, s, lastfiledate, latestfile
   lastfiledate = 0
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fc = f.Files
   For Each f1 in fc
      if InStr(f1.Name,filespec) > 0 then
	     If f1.DateLastModified > lastfiledate then
      	 	lastfiledate = f1.DateLastModified
		 	latestfile = f1.Name
  		 end if
	  end if
   Next
   LastModified = latestfile
End Function

VBEND

VBEval>LastModified("c:\some folder","Updated Report - 'Car Sales' -"),URCS
MessageModal>URCS
VBEval>LastModified("c:\some folder","Last Sales - 'Truck Sales' -"),LSTS
MessageModal>LSTS
VBEval>LastModified("c:\some folder","Last Acquisitions - 'Motorbike' -"),LAMB
MessageModal>LAMB
This uses a modified version of the function I linked to which also checks for a match against the file name. So you can call it with the directory and the identifying part of the file name. It will then return the latest file that contains that text in the file name.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Sat Jul 22, 2006 8:39 am

Thank you very much ! :D
I will test soon
Regards

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Sat Jul 22, 2006 9:05 am

It's perfect ! :D

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Sun Jul 23, 2006 9:51 pm

There is a problem in this script. Sometimes it doesn't take the last file. It must take the last writed in hard disk and dont the last modified.
May you rewrite , pls ? :D
Thank you for your time

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jul 24, 2006 1:03 am

Change f1.DateLastModified to f1.DateCreated:

Code: Select all

VBSTART

Function LastCreated(folderspec,filespec)
   Dim fso, f, f1, fc, s, lastfiledate, latestfile
   lastfiledate = 0
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set fc = f.Files
   For Each f1 in fc
      if InStr(f1.Name,filespec) > 0 then
	     If f1.DateCreated > lastfiledate then
      	 	lastfiledate = f1.DateCreated
		 	latestfile = f1.Name
  		 end if
	  end if
   Next
   LastCreated = latestfile
End Function

VBEND

VBEval>LastCreated("c:\some folder","Updated Report - 'Car Sales' -"),URCS
MessageModal>URCS
VBEval>LastCreated("c:\some folder","Last Sales - 'Truck Sales' -"),LSTS
MessageModal>LSTS
VBEval>LastCreated("c:\some folder","Last Acquisitions - 'Motorbike' -"),LAMB
MessageModal>LAMB
Here I've also changed the function name to something more relevant.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Mon Jul 24, 2006 4:46 am

Thank you very much again :D

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Mon Jul 24, 2006 7:10 am

This script still dont work ...sometimes it takes the file with last date and sometimes it takes an old file... :?

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Mon Jul 24, 2006 7:29 am

I found the problem : some files have the same created date..then it take the first of these files. the script need a second condition with last file modified

wandila
Newbie
Posts: 9
Joined: Thu Jul 20, 2006 10:57 pm

Post by wandila » Mon Jul 24, 2006 7:39 am

The correct is:
VBSTART
Function LastCreated(folderspec,filespec)
Dim fso, f, f1, fc, s, lastfiledate, latestfile
lastfiledate = 0
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
if InStr(f1.Name,filespec) > 0 then
If f1.DateCreated > lastfiledate then
If f1.DateLastModified >= lastfiledate then
lastfiledate = f1.DateLastModified
latestfile = f1.Name

End If
end if
end if
Next
LastCreated = latestfile
End Function

VBEND
VBEval>LastCreated("%PATH%","FILE%"),RESULT

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts