Vbscript 来确定文件夹中的文件是否包含某些字符并检查其修改日期



我无法让它工作。我不断收到以下错误"期望然后第 23 行"该错误位于第一个"如果 File.Name 包含"部分的地方。

我知道这可能是我错过的相当简单的东西,但我的生活看不到它。任何帮助都非常感谢。

reportDate = date
dtMonth = Month(reportDate)
If dtMonth < 10 Then
    dtMonth = "0" & dtMonth
End If
dtDay = Day(reportDate)
If dtDay < 10 Then
    dtDay = "0" & dtMonth
End If
dtYear = Year(reportDate)
saveFormatDate = dtYear & dtMonth & dtDay
Set fso = CreateObject("Scripting.FileSystemObject")
Set recentFile = Nothing
Set goodFile = 0
Set badFile = 0
Set goodFileName = "To_Vendor_" & saveFormatDate
For Each file in fso.GetFolder("C:Data_Files").Files
    If file.Name contains(goodFileName) Then
        recentDate = File.DateLastModified
        If recentDate > DateAdd("H",-6,Now) Then
            goodFile = goodFile + 1         
        Else
            badFile = badFile + 1           
        End If
    Else
        ' Do nothing
    End If
Next
If badFile > 0 Then
    wscript.echo "No files created within the last 6 months"
Else
    If goodFile > 0 Then
        'execute batch file eventually to initiate SFTP process
        'not adding this until working
        wscript.echo "Files are going to be sent."
    Else
        wscript.echo "No good files to be sent."
    End if
End If
wscript.quit

我看到一些问题...

  1. 你可能不是这个意思:

    dtDay = "0" & dtMonth
    

    我假设你的意思是这个?

    dtDay = "0" & dtDay
    

    顺便说一句,这里有一行来填充这样的东西:

    dtDay = Right("0" & Day(reportDate), 2)
    
  2. 您不应该将"Set"与一般变量一起使用。将其从以下行中删除:

    Set recentFile = Nothing ' This isn't being used at all
    Set goodFile = 0
    Set badFile = 0
    Set goodFileName = "To_Vendor_" & saveFormatDate
    
  3. 更改此设置:

    If file.Name contains(goodFileName) Then
    

    自:

    If InStr(file.Name, goodFileName, vbTextCompare) > 0 Then
    

最新更新