Find oldest file in directory & run script

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
hip2b2
Newbie
Posts: 9
Joined: Thu Nov 05, 2009 4:41 am

Find oldest file in directory & run script

Post by hip2b2 » Tue Jul 20, 2010 12:06 am

I am trying to find a solution to the following problem:

Of many files of many names and types in a folder find the oldest instance of a file with the string "SNAPSHOT" in the name.

Insert the file name in a command to run> program.exe OldestSNAPSHOT.exe

Thanks in advance

k

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jul 20, 2010 4:42 am

One way would be to use DOS

Code: Select all

Let>path=C:\yourpath
Let>filepart=SNAPSHOT
Let>rp_wait=1
Let>rp_windowmode=0

Run>cmd /c dir "%path%\*%filepart%*" /od /b > %temp_dir%%filepart%searchresult.tmp

ReadFile>%temp_dir%%filepart%searchresult.tmp,data
DeleteFile>%temp_dir%%filepart%searchresult.tmp
Separate>data,%CRLF%,line

If>line_count>1
  mdl>%path%\%line_1%
  run>program.exe %path%\%line_1%
Else
  mdl>A file with %filepart% as part of the name was not found in the %path% directory.
EndIf

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Wed Jul 21, 2010 12:44 am

I found/modified the following VBS script to finds the oldest file in a directory of a particular extension. The date stamp of the file is stored in a variable datMax. I need to execute the last line of the script with the datMax variable.

Problem is it doesn't work. What am I doing wrong or is this not possible?

Code: Select all

VBStart
Option Explicit
Const strDir 		= "D:\Virtual Machines\Web Spider Machines\VWR - Web Spider XP 32"
Const strSearchedExt	= "vmsn" 'extension without the dot

Dim fs, objDir, objFile
Dim datMax, objOldestFile
Dim strExt

'~~get fs-object
set fs = CreateObject ("scripting.filesystemobject")

'~~get folder
set objDir = fs.GetFolder(strDir)

'~~initialize with current date value
datMax = Now
	
'~~initialize with empty obj-reference
Set objOldestFile = Nothing	

'browse files for ext and date-max
for each objFile in objDir.Files
	'~~get extension
	strExt =  fs.GetExtensionName (objFile.Name)
	'~~test if extensions match
	if 0 = StrComp(strExt, strSearchedExt, vbTextCompare) Then
		'~~test if datemodified is more recent, i.e. bigger
		if objFile.DateLastModified < datMax Then
			'~~keep matches
			datMax = objFile.DateLastModified
			Set objOldestFile = objFile
		end if
	end if
next
VBEND

run>"C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" -T ws deleteSnapshot "D:\My Virtual Machines\Ubuntu\Ubuntu.vmx" "datMax"
Thanks in advance

Regards

k

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Wed Jul 21, 2010 11:30 am

Try this.

Code: Select all

VBStart
function get_max()
  'Const strDir="C:\"
  Const strDir="D:\Virtual Machines\Web Spider Machines\VWR - Web Spider XP 32"
  Const strSearchedExt="vmsn" 'extension without the dot
  'Const strSearchedExt="txt" 'extension without the dot

  Dim fs, objDir, objFile
  Dim datMax, objOldestFile
  Dim strExt

  '~~get fs-object
  set fs = CreateObject ("scripting.filesystemobject")

  '~~get folder
  set objDir = fs.GetFolder(strDir)

  '~~initialize with current date
  datMax =  now()

  '~~initialize with empty obj-reference
  'Set objOldestFile = Nothing

  'browse files for ext and date-max
  for each objFile in objDir.Files
  	'~~get extension
	strExt =  lcase(fs.GetExtensionName (objFile.Name))
	'~~test if extensions match
	if 0 = StrComp(strExt, strSearchedExt, vbTextCompare) Then
		'~~test if datemodified is more recent, i.e. bigger
		if (objFile.DateLastModified < datMax) Then
			'~~keep matches
			datMax = objFile.DateLastModified
			'Set objOldestFile = objFile
		end if
	end if
  next
  get_max=datMax
End Function
VBEND

VBEval>get_max(),datMax
MessageModal>datMax

run>"C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" -T ws deleteSnapshot "D:\My Virtual Machines\Ubuntu\Ubuntu.vmx" "%datMax%"


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