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

Search through folder for specific file name

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi, I have a script which uses the date function to create a variable which is basically the name of a logfile with a time stamp - i.e logfile name = error-2010-06-19_03.log

I've written a script that looks for this logfile see below:

errorvar = "error-"
logvar = ".log"
ext1 = "_01"
ext2 = "_02"
ext3 = "_03"
ext3 = "_04"
dt=date
wscript.echo dt 'how it is displayed does not matter
yyyy=year(dt)
mm=month(dt)
dd=day(dt)
wscript.echo yyyy & vbcrlf & mm & vbcrlf & dd

mm=right("00" & mm,2)
dd=right("00" & dd,2)
'wscript.echo errorvar & yyyy & "-" & mm & "-" & dd & ext1 & logvar
datevar = errorvar & yyyy & "-" & mm & "-" & dd & ext1 & logvar


'On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")


ShowSubFolders objFSO.GetFolder("C:\scripts")


Sub ShowSubFolders(Folder)

For Each file In Folder.Files
Set objCurFile = objFSO.GetFile(file)
'look for logfile instring....
If Instr(objCurFile.Name, datevar) Then wscript.echo objcurfile.name
Next

End Sub

The problem I have is that in the logfile there is a counter which changes when the file reasches 1mb so there might be 3 or 4 logfiles but with different counter like

error-2010-06-19_03.log
error-2010-06-19_02.log
error-2010-06-19_01.log

I will need to find either teh date modified or the hightest number in of the counters.

Question would I need an array or is there a function that would do this...

any help much appreciated

 
when you find an instance of the file name you are looking for then fo something like

intTemp = CInt(Right(strFile.Name, 2))
If intTemp > intMax Then
intMax = intTemp
End If

 
Hi,

Managed to get it working using an if instr else:

ext1 = "_01"
ext2 = "_02"
ext3 = "_03"
ext4 = "_04"
ext5 = "_05"
ext6 = "_06"

errorvar = "error-"
logvar = ".log"

dt=date
wscript.echo dt 'how it is displayed does not matter
yyyy=year(dt)
mm=month(dt)
dd=day(dt)
wscript.echo yyyy & vbcrlf & mm & vbcrlf & dd

mm=right("00" & mm,2)
dd=right("00" & dd,2)
'wscript.echo






Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Scripts"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
if instr(objfile, ext6) then
extension = ext6
'wscript.echo objfile
else
if instr(objfile, ext5) then
extension = ext5
'wscript.echo objfile
else
if instr(objfile, ext4) then
extension = ext4
'wscript.echo objfile
else
if instr(objfile, ext3) then
extension = ext3
'wscript.echo objfile
else
if instr(objfile, ext2) then
extension = ext2
'wscript.echo objfile
else
if instr(objfile, ext1) then
extension = ext1
'wscript.echo objfile


end if
end if
end if
end if
end if
end if
next

wscript.echo extension

Thanks for you help...
 
personally i dislike the look of what is within your

For Each objFile In colFiles
...this bit
Next

i know i am being picky, perhaps it is my inner desire to be a teacher which nags at me to say...when you write code and look at it and think, 'hmm, thats ugly' it should make you think again.

you might as well say?

For Each objFile In colFiles
extension = Right(objFile.Name, 3)
Next
 
I have recently had to do some pattern matching in file names and have found regular expressions to be a great help.

Code:
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/yab2dx62.aspx[/URL]
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/ms974570.aspx[/URL]
set re = new regexp
'find an underscore followed by 2 or more digits
'\d = any digit, {2,} = match 2 or more
re.Pattern ="(_\d{2,})"
re.IgnoreCase = true
re.Global = true

dim strInput
dim strOutput
dim Matches

strInput = "error-2010-06-19_03.log"
'error-2010-06-19_03.log
'error-2010-06-19_02.log
'error-2010-06-19_01.log


wscript.echo(strInput)
if re.Test(strInput) then
	Set Matches = re.Execute(strInput)
	for each Match in Matches
		'strOutput = strOutput & "Match found at position "
		'strOutput = strOutput & Match.FirstIndex & ". Match Value is '"
		'strOutput = strOutput & Match.Value & "'." & vbCRLF
		'wscript.echo(strInput & vbcrlf & strOutput)
		wscript.echo("extension = " & Match.Value)
	next
else
	wscript.echo("extension not found")
end if

With this you won't need to update your code should you grow beyond extension _06.
 
Hi,

The problem I have is that I don't know which file I will need to search through. My 'Ugly' code seems to be functional enough to be able to somehow locate in order from _05 to _01 in a file. This is why I had to use it.

I was looking at datelast modified but couldn't differenciate between oldest and newest...

Thanks for all your help guys..

james
 
The following code will show you how to find the newest file.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

'make sure target folder exists
If objFSO.FolderExists("<your path here>") Then
	Set objStartFolder = objFSO.GetFolder("<your path here>")
	Set colFiles = objStartFolder.Files
	dim newest
	
	newest = null
	
	For Each objFile in colFiles
		if isnull(newest) then
			set newest = objFile
		elseif objFile.DateLastModified > newest.DateLastModified then
			set newest = objFile
		end if
	next
	
	if isnull(newest) then
		wscript.echo("folder is empty")
	else
		wscript.echo("the newest file is: " & newest)
	end if
End if
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top