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!

VBscript Help!

Status
Not open for further replies.

Hammer187

IS-IT--Management
Feb 11, 2013
7
US
I have a script that looks for a certain file in a folder and opens that file. For example, if the user inputs a date, 02/08/2013, it should return that file with that date in it. For some reason, it returns any file with 282013 in it. I have it coded that way because the program that creates the files, drops off the leading zeroes. Here is the breakdown of what is happening, user inputs 02/08/2013, the script grabs 1282013 (because it has 282013 in it) The logical answer is to not take out the leading zeroes, it works like a charm like that, but the program doesnt create the files with the leading zeroes. Here is some of the code:


Dim matches(1000)
Dim match_count
Dim str_matches

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get the search date
search_date = InputBox("Please enter the date to search" & vbcrlf & "You must use the format MM/DD/YYYY" & vbcrlf & "ex. 03/04/2009")

if search_date = "" then
wscript.quit
end if

date_pieces = split(search_date, "/")

' build the filename from the date pieces
date_valid = 0


Dim month
month = date_pieces(0)

fc_month = Left(month, 1)
if fc_month = 0 then
month = Right(month, 1)
end if


Dim day
day = date_pieces(1)

fc_day = Left(day, 1)
if fc_day = 0 then
day = Right(day, 1)
end if

Dim year
year = Right(date_pieces(2), 2)
date_string = month & day & "20" & year

Set dateReg = new regexp
dateReg.Pattern = date_string & "ivr.log"

Set logfolder = objFSO.GetFolder("K:\Kronos\PHONE\Log")
Set logfiles = logfolder.Files

' iterate through each file in the log folder
' as soon as we find a match, use that file
for each logfile in logfiles
'wscript.echo "Testing " & logfile & " against " & dateReg.Pattern

if dateReg.Test(logfile) then
filename = logfile

wscript.echo "Using log file: " & logfile
exit for
end if
next

if objFSO.fileexists(filename) then
Set objFile = objFSO.OpenTextFile(filename, 1)
else
wscript.echo "Can't find log file for " & search_date
wscript.quit
end if





The rest of the program doesn't pertain to my problem. I know it is something to do with the For Loop but I can't seem to figure it out.
 
So if the user enters 02/12/2013 and another user enters 21/02/2013, they both end up in 2122013?

It would be better if you reversed the dates to yyyymmdd. That way, you don't have leading zeros, the above problem goes away, and best of all, when you look at the directory, the files can be sorted in date order.
 
Try changing your pattern to:

Code:
dateReg.Pattern = "^" & date_string & "ivr\.log"

The caret ensures a match starts with date_string. In your example, it would match 282013ivr.log, but not 1282013ivr.log. Also, you will want to escape the dot character by preceding it with a backslash. In regex, the dot is a wildcard character meaning "any character"; to search for a literal dot, you need to escape it.
 
I am unable to change the way the filenames are, therefore I cannot change the way the user enters the date. I put the carat in front of the dateReg.Pattern, but that didn't help. I am still getting 1282013.
 
If I put the carat in front of the pattern, it says that it can't find the file. If I put a . in front of the pattern, it returns 1282013.
 
What about this ?
dateReg.Pattern = "[^1-9]" & date_string & "ivr\.log"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Are you meaning to put that slash in "ivr.log"?
 
dateReg.Pattern = "[^1-9]" & date_string & "ivr.log"

This works. Does the brackets force the check of the first number after the . ?
 
If you already know the name of the file, why are you traversing the folder to find it when you can instead just ask for it (no regex).

Code:
strFilename =  date_string & "ivr.log"
strFilepath = "K:\Kronos\PHONE\Log\" & strFilename
if (objFSO.FileExists(strFilepath)) then
   set objStream = objFSO.OpenTextFile(strFilepath, 1)
else
   wscript.echo "Can't find file " & strFilepath
end if


-Geates

 
Thank you so much. I am not well-versed in VBscript. How do I mark this as solved?
 
Code:
[COLOR=blue]Dim[/color] matches(1000)  
[COLOR=blue]Dim[/color] match_count  
[COLOR=blue]Dim[/color] str_matches  

[COLOR=blue]Set[/color] objFSO [COLOR=blue]=[/color] CreateObject("Scripting.FileSystemObject")  

[COLOR=green]' Get the search date[/color]
search_date [COLOR=blue]=[/color] InputBox("Please enter the [COLOR=blue]date to[/color] search" [COLOR=blue]&[/color] vbcrlf [COLOR=blue]&[/color] "You must use the format MM/DD/YYYY" [COLOR=blue]&[/color] vbcrlf [COLOR=blue]&[/color] "ex. 03/04/2009")  

[COLOR=blue]if[/color] search_date [COLOR=blue]=[/color] "" [COLOR=blue]then[/color]  
wscript.quit  
end [COLOR=blue]if[/color]  

date_pieces [COLOR=blue]=[/color] split(search_date, "/")  

[COLOR=green]' build the filename from the date pieces[/color]
date_valid [COLOR=blue]=[/color] 0  


[COLOR=blue]Dim[/color] month  
month [COLOR=blue]=[/color] date_pieces(0)  

fc_month [COLOR=blue]=[/color] Left(month, 1)  
[COLOR=blue]if[/color] fc_month [COLOR=blue]=[/color] 0 [COLOR=blue]then[/color]  
month [COLOR=blue]=[/color] Right(month, 1)  
end [COLOR=blue]if[/color]  


[COLOR=blue]Dim[/color] day  
day [COLOR=blue]=[/color] date_pieces(1)  

fc_day [COLOR=blue]=[/color] Left(day, 1)  
[COLOR=blue]if[/color] fc_day [COLOR=blue]=[/color] 0 [COLOR=blue]then[/color]  
day [COLOR=blue]=[/color] Right(day, 1)  
end [COLOR=blue]if[/color]  

[COLOR=blue]Dim[/color] year  
year [COLOR=blue]=[/color] Right(date_pieces(2), 2)  
date_string [COLOR=blue]=[/color] month [COLOR=blue]&[/color] day [COLOR=blue]&[/color] "20" [COLOR=blue]&[/color] year  

[COLOR=blue]Set[/color] dateReg [COLOR=blue]= new[/color] regexp  
dateReg.Pattern [COLOR=blue]=[/color] [highlight #EDD400]"^" [COLOR=blue]&[/color] date_string [COLOR=blue]&[/color] "ivr\.log"
[/highlight]
[COLOR=green]Set logfolder = objFSO.GetFolder("K:\Kronos\PHONE\Log")[/color]
[COLOR=blue]Set[/color] logfiles [COLOR=blue]=[/color] logfolder.Files  

[COLOR=blue]if[/color] dateReg.Test(myTest) [COLOR=blue]then[/color]  
msgbox("found myTest")  
end [COLOR=blue]if[/color]  


[COLOR=green]' iterate through each file in the log folder[/color]
[COLOR=green]' as soon as we find a match, use that file[/color]
[COLOR=blue]for each[/color] logfile [COLOR=blue]in[/color] logfiles  
wscript.echo "Testing " [COLOR=blue]&[/color] logfile.name [COLOR=blue]&[/color] " against " [COLOR=blue]&[/color] dateReg.Pattern  

	if [highlight #EDD400]dateReg.Test(logfile.name)[/highlight] [COLOR=blue]then[/color]  
		msgbox("file found")  
		filename [COLOR=blue]=[/color] logfile  
		wscript.echo "Using log file: " [COLOR=blue]&[/color] logfile  
		exit [COLOR=blue]for[/color]  
	end [COLOR=blue]if[/color]  
[COLOR=blue]next[/color]  

[COLOR=blue]if[/color] objFSO.fileexists(filename) [COLOR=blue]then[/color]  
[COLOR=blue]Set[/color] objFile [COLOR=blue]=[/color] objFSO.OpenTextFile(filename, 1)  
[COLOR=blue]else[/color]  
wscript.echo "Can  [COLOR=green]'t find log file for " & search_date[/color]
wscript.quit  
end [COLOR=blue]if[/color]
 
I don't know the name of the file, i just know the date that it was created on. It ammends the date to the rest of the filename. It uses a counter to increment each time it creates a file so the filename looks something like 41378.dateivr.log. So it changes everytime.
 
Hammer187 said:
It ammends the date to the rest of the filename. It uses a counter to increment each time it creates a file so the filename looks something like 41378.dateivr.log. So it changes everytime.

In that case, ignore my suggestion of using the caret to anchor the match. But you do want the backslash before the dot, otherwise the dot is considered a wildcard (it has special meaning in regular expression syntax). PHV's pattern is more appropriate in your situation.

There's no way to "mark the thread as solved", but to indicate a useful answer you can click the "Thank <member> and star this post" link in the post(s) that helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top