Create Batch File to Check for New File and Send Email

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
74,405
Office Version
  1. 365
Platform
  1. Windows
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?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Joe, Here's some VB Script you can try...

Code:
 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)
 
Upvote 0
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?
 
Upvote 0
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....

Code:
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
 
Upvote 0
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.
 
Upvote 0
I figured it out. This block works:
Code:
    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!
 
Upvote 0

Forum statistics

Threads
1,225,691
Messages
6,186,467
Members
453,358
Latest member
Boertjie321

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top