# Create Batch File to Check for New File and Send Email



## Joe4 (Oct 14, 2014)

I am trying to create a batch file, either via DOS Batch file (.bat) or VB Script (.vbs) that checks a specified folder to see if any new files were created today (current date).  If not, it will send out an email (I already have the VBS script for the email portion, so I would just call that).  This script will run daily from a scheduler.

I have worked some with .bat and .vbs files, but have never done anything like this (searching a specified folder to see if any new files were created on that day).  Any tips?


----------



## Jerry Sullivan (Oct 14, 2014)

Joe, Here's some VB Script you can try...


```
Option Explicit  

Function HasFileUpdatedToday(sFilePath) 
'--Checks in folder sFilePath for file last modified today
'   If found, returns True, else False

 Dim oFSO, oFile
    
 Set oFSO = CreateObject("Scripting.FileSystemObject") 
 For Each oFile in oFSO.GetFolder(sFilePath).Files  
    If oFile.DateLastModified>=Date Then         
       HasFileUpdatedToday=True
       Exit For   
    End If
 Next 

End Function

'--Example calling code
Const sPATH = "C:\Test"
Dim bResult 
bResult= HasFileUpdatedToday(sPATH)
WScript.Echo(bResult)
```


----------



## Joe4 (Oct 15, 2014)

Jerry,

Thanks for the reply.  I have never used Function with VB Script before.  So I assume that I would have another script which calls this function.  Do I just need to place the function in the same folder as the script that calls it?


----------



## Jerry Sullivan (Oct 15, 2014)

Joe, You could just combine the code I suggested with the code you already have that sends email messages in one .vbs file.

Something like....


```
Option Explicit  

Const sPATH = "C:\Test"

If HasFileUpdatedToday(sPATH) Then
    WScript.Echo("A file has been updated today.") 
Else
    WScript.Echo("Sending notification...")
[COLOR="#0000CD"]    '--your code to send email notification
    '.....
    '.....
    '.....[/COLOR]
End If

'-------end of main procedure
'-------called procedure(s) below

Function HasFileUpdatedToday(sFilePath) 
'--Checks in folder sFilePath for file last modified today
'   If found, returns True, else False

 Dim oFSO, oFile
    
 Set oFSO = CreateObject("Scripting.FileSystemObject") 
 For Each oFile in oFSO.GetFolder(sFilePath).Files  
    If oFile.DateLastModified>=Date Then         
       HasFileUpdatedToday=True
       Exit For   
    End If
 Next 

End Function
```


----------



## Joe4 (Oct 15, 2014)

Jerry,

That works well.  I am still having one issue.  My script to sent the email is another VBS script, and I am having as hard time figuring out how to run this scripts from inside of the other script.  I have been poking around on the internet, but haven't seemed to have found the right method yet.


----------



## Joe4 (Oct 15, 2014)

I figured it out.  This block works:

```
Dim objShell
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "Email1.vbs" 
    Set objShell = Nothing
```
I actually found and tried this early on, but it wasn't working (none of the methods I tried were!).  Then I found it was because I had left the "1" off of "Email1.vbs".  Done in by my own stupid typo!!!

Anyway, thanks again for the help.  Things are working great now!


----------



## Jerry Sullivan (Oct 15, 2014)

Glad you sorted that out Joe. Yes, the most obvious typos are the hardest ones to spot.


----------

