Custom script needed to check existence of files

murryc
murryc used Ask the Experts™
on
I need a custom script or batch written to check for the existence of two daily files that should exist for the next day. More specifically, we have a scheduler system that should be exporting a proprietary file for the next day. If this file does not get created then someone needs to be alerted prior to midnight when that file would need to be used. The scheduler is creating two files for each day so we would need to check for both. The format of the files is MEGA%MM%%DD% and BLAZE%MM%%DD%, where %MM% is the 2-digit month and %DD% is the 2-digit day of the month. Example of files: MEGA0913 and BLAZE0913. Neither file has a file extension so Windows just sees them as a type 'file'. I plan on using task scheduler to check for both files at 9pm of the day before. So on the 13th, the script would run at 9pm and be looking for MEGA0914 and BLAZE0914. Both files reside in the same folder. If either of the files is missing then the script needs to send an email, using BLAT or similar to alert an email address as to which file(s) are missing.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017

Commented:
Do you have an existing system that monitors your environment?
Some include the ability to monitor a file system for an event.

in your case though you are looking for a negative monitoring alert.

In the formulation of your question, you might fair better to post it as a gig?

commonly, in such a setup, I would have the process that generates the files generates the email on a successful conclusion of the process or if it runs into issues to generate an email.

Presumably, your situation is in the event the process is terminated outside the normal process, system rebooted, etc.

any reason why batch file not a powershell or a vbscript where it can be located in the folder and search the directory.....

includes more granular tools.
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
Hi murryc,
Your question is similar to the one solved in these EE threads:

https://www.experts-exchange.com/questions/29155232/Program-to-check-for-new-files.html
https://www.experts-exchange.com/questions/29155672/Setup-CDO-for-Office365.html

The solution there is an AutoHotkey script that sends an email via CDO or PowerShell (it uses an SMTP server of your choosing...Comcast, Gmail, Hotmail/Outlook, whatever). Let me know if that approach interests you and I'll look into what it would take to customize the script to meet your exact requirements. Regards, Joe
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
you might fair better to post it as a gig?
Hi Arnold,
I think you're absolutely right about that! Unfortunately, Experts Exchange killed Gigs a while ago, and also killed the HireMe button. This is definitely one of those questions requiring custom development that could go beyond what is reasonable for pro bono work. Although Gigs and HireMe are gone, EE now allows discussion of paid work via PM. Regards, Joe
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
There are some scripting solutions, and also third party products, that you might be able to use / adapt...



»bp
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
The following is PowerShell at heart, but "disguised" as batch file for easier scheduling, so save it as Whatever.cmd or Whatever.ps1.cmd.
It's in test mode and will not send an email; uncomment line 30 to send the email.
If you need/want further arguments for the mail, run
Get-Help Send-MailMessage -ShowWindow

Open in new window

You can just add them to the argument list in $mailArgs.
The rest should be pretty self-explanatory.
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 2) -join [environment]::NewLine)"
@exit /b %Errorlevel%

$mailArgs = @{
	From = 'filecheck@domain.com'
	To = 'Someone@domain.com'
	Subject = 'Missing files'
	SmtpServer = 'mail.domain.com'
}
$rootDir = 'D:\Temp'
$fileList = @(
	'MEGA{0}'
	'BLAZE{0}'
)
$body =  @"
The following files were not found:
{0}
"@
$timeStamp = Get-Date -Format 'MMdd'

$missingFiles = @()
$fileList | ForEach-Object {
	$fileName = $_ -f $timeStamp
	If (-not (Test-Path -Path "$($rootDir)\$($fileName)" -PathType Leaf)) {
		$missingFiles += $fileName
	}
}
If ($missingFiles) {
	"The following files were not found: $($missingFiles -join ', ')" | Write-Host -ForegroundColor Yellow
#	Send-MailMessage @mailArgs -Body ($body -f $($missingFiles -join "`r`n"))
} Else {
	"All required files were found: $($fileList -join ', ')" | Write-Host -ForegroundColor Green
}

Open in new window

Bill PrewTest your restores, not your backups...
Top Expert 2016
@murryc,

Are you all set with this now, or do you need more help?  If all set, could you please close it out now.  If you need help with the question close process take a look at:



»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial