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
Find oldest file in directory & run script
Moderators: Dorian (MJT support), JRL
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
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?
Thanks in advance
Regards
k
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"
Regards
k
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%"