用于读取文本文件

时间:2016-06-02 06:18:04

标签: file batch-file

我有一个批处理文件。

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

call :extractDate firstLineDate firstLine 
<file set /p line=
echo "%line%"
for /f "tokens=2 delims=|" %%# in ("%line%") do set "_date=%%#"
echo %_date%

call :convertToJulianDay julianFirstLine firstLineDate
REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524

set "today=%date%"
call :convertToJulianDay julianToday today
REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524

set /a diff=julianToday-julianFirstLine
REM *** SECOND CHECK ***
if %diff% NEQ 1 goto :fail
exit /b

:fail
echo File doesn't fit

而bellow是示例input.txt

AML.DATA|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

我想修改我的批处理文件以读取input.txt的第一行,如果date = sysdate-1而最后一行= EOF则继续执行prog。但是bat文件总是说:

The system cannot find the batch label specified - extractDate
The system cannot find the file specified.
The system cannot find the batch label specified - convertToJulianDay
The system cannot find the batch label specified - convertToJulianDay

1 个答案:

答案 0 :(得分:0)

阅读第一行很简单。

If not defined firstLine set firstLine=%%A

现在您需要提取日期 然后将其转换为朱利安日期 将系统日期转换为朱利安日期。
比较差异。

这就是全部

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

call :extractDate firstLineDate firstLine 
call :convertToJulianDay julianFirstLine firstLineDate
set "today=%date%"
call :convertToJulianDay julianToday today

set /a diff=julianToday-julianFirstLine
REM *** SECOND CHECK ***
if %diff% NEQ 1 goto :fail
exit /b

:fail
echo File doesn't fit