I am creating a SQL Server integration package that requires some VBScript. Basically, every few days there is a file that is uploaded to a folder on my computer. This file always has a different name, but it always contains the same string.
For example, the file for yesterday could have been “John J July 15.xlsx” and the file for tomorrow might be “John Jones Jul 17 2012.xlsx”. They all contain “John” and they always will. I always want to set my variable to equal the filename of the file that contains “John” AND has a DateCreated attribute equal to today.
Is there something wrong with my code? I can’t get it to work. Is the “Today.Date” also returning a time? I only want it to return a date. Does the Name attribute return the extension also?
Public Sub Main()
Dim f, fl, fs As Object
Dim filedate As Date = Microsoft.VisualBasic.Today.Date
Dim firstFileName As String
fs = CreateObject("Scripting.FileSystemObject")
fl = fs.GetFolder("E:\myFolder")
For Each f In fl.Files
If f.DateCreated = filedate And f.Name = "John*" Then
firstFileName = f.Name
Dts.Variables("ExcelSource").Value = "E:\myFolder\" & firstFileName
End If
Next
End Sub
I don’t believe the wildcard character in your IF statement above will work as you intend here. It will compare the filename with the literal “*” character, and match nothing. I might suggest something like:
*EDIT: The DateCreated property does resolve to the second, so the comparison you have above won’t work, either. Here’s a very rough and untested illustration of one way you could modify to test for the proper date (omitting the seconds resolution).*