1

I'm writing a batch file to read AML_handshake.txt date and compare the date with yesterday's date. If it matches and the last line=EOF then execute my java program. So far I have got bellow.

@ECHO OFF 
SET CURRENTDATE=%DATE% 
SET LOGFILE_DATE=%DATE:~4,2%.%DATE:~7,2%.%DATE:~10,4% 
SET LOGFILE_TIME=%TIME:~0,2%.%TIME:~3,2% 
SET LOGFILE="C:\Extractor\log\AML_Data_Auto-%LOGFILE_DATE%-%LOGFILE_TIME%.log" 
call :Logit >> %LOGFILE% 
exit /b

:Logit 
set "firstLine" 
for /f "tokens=*" %%A in (C:\Extractor\AML_handshake.txt) do ( 
        If not defined firstLine set "firstLine=%%A" 
        set Lastline=%%A 
) 
REM *** FIRST CHECK *** 
if "EOF" NEQ "%Lastline%" goto fail 

REM *** SECOND CHECK *** 
IF "%CURRENTDATE%" NEQ "%firstLine%" goto fail 
exit /b 

goto prog 
:prog 
echo Incremental Data Extraction - Started 
sqlplus -s ***** @call_proc.sql 
echo Incremental Data Extraction - Ended 
echo Generating AML Files - Started 
start "C:\Extractor" TableExtractor.exe 
echo Generating AML Files - Ended 
echo Process Completed 

:fail 
echo Initial Check Failed, Process Terminated 

To do a test run, I have modified the txt file date to current date and executed the batch file, but in log file I always see "Initial Check Failed, Process Terminated".

Bellow is the text file.

01-JUN-2016
PORT_DATA|560538
NDB_AML_AA|43063
NDB_AML_LD|12878
NDB_AML_REPO|496
NDB_AML_TRAN|84596
NDB_AML_JOINT_AC|219873
NDB_AML_CUS_REL_PRTY|43
NDB_AML_BICCODE|108292
CUSTOMER_MASTER|684124
CATEGORY.MASTER|3288
DEPT.MASTER|2527
COUNTRY.MASTER|251
CUSTOMER.STATUS.MASTER|26
INDUSTRY.MASTER|65
JOB.TITLE.MASTER|22
COMPANY.MASTER|121
TRANSACTION.MASTER|3133
RELATION.MASTER|56
NDB_AML_TBILL_TBOND|2845
EOF

Two things I want to know.

1) How to compare the text file date with date-1(yesterday's date), 2) Why is my batch file not running even though I edit the text file date?

DeE DEe
  • 343
  • 1
  • 6
  • 15
  • 1
    [Debugging your batch files](http://www.robvanderwoude.com/battech_debugging.php) – DavidPostill Jun 09 '16 at 11:47
  • What is the date format returned by `%DATE%` on your system? I guess it does not contain the month in letter like `JUN`, right? So why do you expect `IF "%CURRENTDATE%" NEQ "%firstLine%"` to ever match? – aschipfl Jun 09 '16 at 12:25

1 Answers1

2

There are a few errors in your batch file, including some hard-to-detect ones like setting a variable to a value with some extra white space at the end, which caused your condition to compare, for instance "EOF" with "EOF ". So always make sure to remove that extra white space at the end of any SET var=value line.

In addition, you need to use delayed expansion since you are setting a variable value inside the FOR loop. Here's a revised version of the batch that should do what you're looking for, and with a little more information being logged.

@ECHO OFF 
setlocal EnableDelayedExpansion
SET CURRENTDATE=%DATE%
SET LOGFILE_DATE=%DATE:~4,2%.%DATE:~7,2%.%DATE:~10,4%
SET LOGFILE_TIME=%TIME:~0,2%.%TIME:~3,2%
SET LOGFILE=AML_Data_Auto-%LOGFILE_DATE%-%LOGFILE_TIME%.log

:Logit 
set firstLine=
set lastLine=
for /f "delims=" %%A in (handshake.txt) do ( 
  if "!firstLine!"=="" set firstLine=%%A
  set lastLine=%%A
) 
set err=
if "EOF" NEQ "!Lastline!" (
  set err=LastLine should not be equal to "!Lastline!"
  goto fail 
) else (
  if "%CURRENTDATE%" NEQ "!firstLine!" (
    set err=FirstLine is "!firstLine!" but today's date is "%CURRENTDATE%"
    goto fail 
  )
)
echo Incremental Data Extraction - Started >> %LOGFILE%
sqlplus -s ***** @call_proc.sql >> %LOGFILE%
echo Incremental Data Extraction - Ended >> %LOGFILE%
echo Generating AML Files - Started >> %LOGFILE%
start "C:\Extractor" TableExtractor.exe >> %LOGFILE%
echo Generating AML Files - Ended >> %LOGFILE%
echo Process Completed >> %LOGFILE%
goto :eof 

:fail 
echo Initial Check Failed, Process Terminated with error: %err% >> %LOGFILE%
exit /b

And as far as comparing the text file date with yesterday's date is concerned, the issue has already been answered.

Community
  • 1
  • 1
Filipus
  • 520
  • 4
  • 12