Click here to Skip to main content
15,749,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A file exists named "vb19/02/2015" which has the static name "vb" and a dynamic date that is today's date. I want to check that a particular file has the string "vb" in it and the today's date. I want to use VB script.
Updated 19-Feb-15 0:28am
Richard MacCutchan 19-Feb-15 7:08am    
And what is the problem? Does VBScript have a feature that can list all files with a particular pattern that you could search for?

1 solution

The following script should work
'Work out what the filename should be
Dim dt
Dim fn
fn = "vb" & Lpad(day(dt), "0", 2) & "/" & Lpad(month(dt), "0", 2) & "/" & year(dt)

'search for the file
Set fso = CreateObject("Scripting.FileSystemObject")
foldername = "c:\temp"   'change this as required

If fso.FileExists(fso.BuildPath(foldername, fn)) Then
	WScript.Echo fn & " exists in " & foldername
	WScript.Echo fn & " not found"
End If

'Lpad function from
'bug fixed from Lpad = PadString + MyValue
Function Lpad (MyValue, MyPadCharacter, MyPaddedLength)
	Dim x
	Dim PadLength
	Padlength = MyPaddedLength - Len(MyValue)
	Dim PadString
	For x = 1 To Padlength
		PadString = PadString & MyPadCharacter
	Lpad = PadString & MyValue
End Function

Points to note:
- I had to remove the "/" to test this as I'm working in a windows environment and that character is not legal in filenames

- I've used the day,month, year VBScript functions to force the date (in the file name) to always be the format I expect (rather than using date formatting which could differ depending on culture)

- I've padded the day and month with leading zeroes for consistency of naming - the requirement was implied by your use of /02/

- The function from M$ (See reference link in the code) didn't work so I changed the + to a & (string concatenator)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900