ruhi
ruhi

Reputation: 1

batch script to check the size of file and send mail if greater

I have been working on a batch script which checks for the size of a file and send an email in case the size is greater than 0 byte:

set V_TMP_FILE="D:\pmserver\Tgtfiles\XLI_stage\f_cdc_flag1.out" 
set minbytesize=0  
if exist %V_TMP_FILE% (
FOR %%A IN ('%V_TMP_FILE%') DO set size=%%~zA
if %size% GTR %minbytesize% (
    %V_EMAIL_PATH%\postie -host:internalmail.usa.xl -to:"%V_TO_EMAIL_ID%" -from:%COMPUTERNAME%@xlgroup.com -s:"%V_SRC_TABLE_NM% %V_SUBJ%" -msg:"%V_MSG%" 
) else (
    echo %V_TMP_FILE% is EMPTY
)

I am getting the below error -

Standard output and error:

h file. 0 was unexpected at this time.

D:\Informatica91\server\bin>if  GTR 0 (

Kindly help me solve this issue , its really taking me time to dig in the issue.

Upvotes: 0

Views: 5474

Answers (2)

BDM
BDM

Reputation: 3900

How many times have we had to answer this...

Ok, check out this answer that I wrote maybe a week ago. It explains everything...

Upvotes: 0

Magoo
Magoo

Reputation: 79983

Batch parses an entire command, even if it's spread across multiple lines, before executing it. As part of that procedure, it substitutes ANY %var% variable for the value VAR has at the time it was PARSED - that is, BEFORE it is executed.

SIZE was NOT set before the IF... was parsed, so its value is replaced by (nothing) and hence the command is interpreted as IF (nothing) GTR 0

Further, the single-quotes around the filename would change the filename dince ' is a valid filename character.

Number of cures: easiest seems to be

if exist %V_TMP_FILE% (
FOR %%A IN (%V_TMP_FILE%) DO (
if %%~zA GTR %minbytesize% (
    %V_EMAIL_PATH%\postie -host:internalmail.usa.xl -to:"%V_TO_EMAIL_ID%" -from:%COMPUTERNAME%@xlgroup.com -s:"%V_SRC_TABLE_NM% %V_SUBJ%" -msg:"%V_MSG%" 
) else (
    echo %V_TMP_FILE% is EMPTY
)

Upvotes: 1

Related Questions