Batch script to validate filename&format

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Radha
Posts: 2
Joined: 09 Jul 2012 14:58

Batch script to validate filename&format

#1 Post by Radha » 09 Jul 2012 15:05

I am new to batch script .I need help for writing batch script which can be automated and execute every day on specific time.


My File name will be of below format:
e.g. ABC_CDEFGH_2601201012316_00001.csv

1.I should validate the name of the file and also its format.
2.Need to check if the file is empty and send alert by sendmail.
3.Check if there is any missing sequence number and send alert to the user.
Can anyone help me with this?
Thanks in advance.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch script to validate filename&format

#2 Post by abc0502 » 10 Jul 2012 07:37

what do you mean with validate,and when the file is empty is the size of it is zero or not and one more question is this file always with the same name , i mean could the name change any time or not

Radha
Posts: 2
Joined: 09 Jul 2012 14:58

Re: Batch script to validate filename&format

#3 Post by Radha » 10 Jul 2012 07:50

Thanks for the reply.

Filename will be the same,it will not change and the format is CSV file.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch script to validate filename&format

#4 Post by abc0502 » 10 Jul 2012 08:56

Hi, here is the batch files:
the first is to add a schadual task to run the file check.bat that will do the work you want it to do
The check.bat will send emial if one of these errors happen:
1- the file not exist
2- the file doesn't have the same name as ABC_CDEFGH_2601201012316_00001
3- the file is empty


Sch.bat
@echo off
cls
set "time=21:00"
set "date=S,SU,M,T,W,TH,F"
set "check=C:\check.bat"
At %time% /interactive /every:%date% "%check%"

Change the Text in Red To suite You.


Check.bat
@echo off
cls
setlocal
set "file=C:\ABC_CDEFGH_2601201012316_00001.csv"
set "email=%temp%\email_script.vbs"

:: Check to see if there is a previous e-mail to prevent Errors
IF Exist %email% Del /F /Q %email%

:: Check to see if the file exist or not
IF Not Exist %file% goto Error

:: Check to see if the file empty or not by comparint the file size to 0
For %%a in (%file%) do (
If %%~za==0 goto Error
)

:: Check to see if there is any missing sequance numbers "the name is differant"
For %%b in (%file%) do (
If %%~nb NEQ ABC_CDEFGH_2601201012316_00001 goto Error
)

:: This section is for sending e-mail if any conditions is not correct "file not exist, file is empty, or sequance numbers missing"
:Error
>%email% (
echo.Const fromEmail = "gmail_account_wher_the_email_will_be_sent_from"
echo.Const password = "gmail_password"
echo.Dim emailObj, emailConfig
echo.Set emailObj = CreateObject^("CDO.Message"^)
echo.emailObj.From = "same_email_from_where_you_will_send_email"
echo.emailObj.To = "the_reciver_email_any_email_service_could_be_any_email_service"
echo.emailObj.Subject = " Write Message Subject Here "
echo.emailObj.TextBody = " Write Message Here "
echo.Set emailConfig = emailObj.Configuration
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/smtpserver"^) = "smtp.gmail.com"
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/smtpserverport"^) = 465
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/sendusing"^) = 2
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"^) = 1
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/smtpusessl"^) = true
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/sendusername"^) = fromEmail
echo.emailConfig.Fields^("http://schemas.microsoft.com/cdo/configuration/sendpassword"^) = password
echo.emailConfig.Fields.Update
echo.emailObj.Send
echo.Set emailobj = nothing
echo.Set emailConfig = nothing )>%email%

:: Run the E-mail Script
%email%

And Also Change the text in red to suite you

Post Reply