Problem with batch file that is analysing data from a text file

I have some code that is reading a text file line by line and doing some analysis on it.

The loop checks if each line starts with a number and then if it starts with a number it needs to validate information at a certain position on that line.

This part if working.

However I also need to check that the last line has an entry “END” – if it has and END that is fine but if not it should provide an error message.

Data is as follows :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>HeaderLine1
HeaderLine2 00
Uline
1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada adafaafaf
1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada adafaafaf
1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOFailSNumbersadada adafaafaf
41251515142141521561826512651726517826581725616SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
EOF121412415152
EOF214125151514
UTL
END```
So the line with SAMPLETEXTTOFail should fail and this is working correctly. But I cannot get my code to check the last line.
Code is as follows :
```@echo off
SETLOCAL DisableDelayedExpansion
REM hard coded for now - this is the value i want to check against
set "NewName=SAMPLETEXTTOCHECKN"
echo %NewName%
pause
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ D:sample.TXT"`) do (
set "myVar=%%a"
call :processLine myVar
)
goto :eof
:processLine
SETLOCAL EnableDelayedExpansion
set "line=!%1!"
set "line=!line:*:=!"
echo(!line!
Rem get first three characters for testing End is present on last row
set "ThreeChar=%Line:~0,3%"
Echo %ThreeChar%
Rem get first character to check if a number as data rows to check start with a number
set "FirstChar=%Line:~0,1%"
Echo %FirstChar%
Rem check if first character is a number
SET "var="&for /f "delims=0123456789" %%i in ("%FirstChar%") do set var=%%i
if defined var (echo %FirstChar% NOT numeric) else (echo %FirstChar% numeric)
rem if numeric read in
SET "var="&for /f "delims=0123456789" %%i in ("%FirstChar%") do set var=%%i
rem if numeric then read in variable
if not defined var set "SchemeTest=%Line:~46,18%"
REM Test against expected value
echo %SchemeTest%
REM this line below not yet tested
REM if NOT "%SchemeTest%" == "%NewName%" echo ERROR: This file appears to hold unexpected data, please review. & goto ErrorExit
REM REMOVE ERROR EXIT FOR NOW
if not defined var if NOT "%SchemeTest%" == "%NewName%" echo ERROR: This file appears to hold data for more than one client, please review.
PAUSE
rem commented out as never seems to run
echo should be out of the loop here
rem last entry should be end
REM if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review. & goto ErrorExit
REM REMOVE ERROR EXIT FOR NOW
rem if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review.
ENDLOCAL
goto :eof
PAUSE
</code>
<code>HeaderLine1 HeaderLine2 00 Uline 1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada adafaafaf 1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada adafaafaf 4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf 1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada adafaafaf 1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada adafaafaf 4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf 4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf 4125151514214152156182651265172651782658172561SAMPLETEXTTOFailSNumbersadada adafaafaf 41251515142141521561826512651726517826581725616SAMPLETEXTTOCHECKNumbersadada adafaafaf 4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf EOF121412415152 EOF214125151514 UTL END``` So the line with SAMPLETEXTTOFail should fail and this is working correctly. But I cannot get my code to check the last line. Code is as follows : ```@echo off SETLOCAL DisableDelayedExpansion REM hard coded for now - this is the value i want to check against set "NewName=SAMPLETEXTTOCHECKN" echo %NewName% pause FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ D:sample.TXT"`) do ( set "myVar=%%a" call :processLine myVar ) goto :eof :processLine SETLOCAL EnableDelayedExpansion set "line=!%1!" set "line=!line:*:=!" echo(!line! Rem get first three characters for testing End is present on last row set "ThreeChar=%Line:~0,3%" Echo %ThreeChar% Rem get first character to check if a number as data rows to check start with a number set "FirstChar=%Line:~0,1%" Echo %FirstChar% Rem check if first character is a number SET "var="&for /f "delims=0123456789" %%i in ("%FirstChar%") do set var=%%i if defined var (echo %FirstChar% NOT numeric) else (echo %FirstChar% numeric) rem if numeric read in SET "var="&for /f "delims=0123456789" %%i in ("%FirstChar%") do set var=%%i rem if numeric then read in variable if not defined var set "SchemeTest=%Line:~46,18%" REM Test against expected value echo %SchemeTest% REM this line below not yet tested REM if NOT "%SchemeTest%" == "%NewName%" echo ERROR: This file appears to hold unexpected data, please review. & goto ErrorExit REM REMOVE ERROR EXIT FOR NOW if not defined var if NOT "%SchemeTest%" == "%NewName%" echo ERROR: This file appears to hold data for more than one client, please review. PAUSE rem commented out as never seems to run echo should be out of the loop here rem last entry should be end REM if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review. & goto ErrorExit REM REMOVE ERROR EXIT FOR NOW rem if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review. ENDLOCAL goto :eof PAUSE </code>
HeaderLine1                 
HeaderLine2                                 00                            
Uline                                        
1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
4125151514214152156182651265172651782658172561SAMPLETEXTTOFailSNumbersadada  adafaafaf  
41251515142141521561826512651726517826581725616SAMPLETEXTTOCHECKNumbersadada     adafaafaf  
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada  adafaafaf  
EOF121412415152                 
EOF214125151514             
UTL                                                   
END```


So the line with SAMPLETEXTTOFail should fail and this is working correctly. But I cannot get my code to check the last line.

Code is as follows :

```@echo off
SETLOCAL DisableDelayedExpansion

REM hard coded for now - this is the value i want to check against
set "NewName=SAMPLETEXTTOCHECKN"
echo %NewName%
pause

FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ D:sample.TXT"`) do (
    set "myVar=%%a"
    call :processLine myVar
)
goto :eof

:processLine
SETLOCAL EnableDelayedExpansion
set "line=!%1!"
set "line=!line:*:=!"
echo(!line!

Rem get first three characters for testing End is present on last row
set "ThreeChar=%Line:~0,3%"
Echo %ThreeChar%

Rem get first character to check if a number as data rows to check start with a number
set "FirstChar=%Line:~0,1%"
Echo %FirstChar%

Rem check if first character is a number
SET "var="&for /f "delims=0123456789" %%i in ("%FirstChar%") do set var=%%i
if defined var (echo %FirstChar% NOT numeric) else (echo %FirstChar% numeric)

rem if numeric read in
SET "var="&for /f "delims=0123456789" %%i in ("%FirstChar%") do set var=%%i
rem if numeric then read in variable
if not defined var set "SchemeTest=%Line:~46,18%"
REM Test against expected value
echo %SchemeTest%
REM this line below not yet tested
REM if NOT "%SchemeTest%" == "%NewName%"  echo ERROR: This file appears to hold unexpected data, please review. & goto ErrorExit
REM REMOVE ERROR EXIT FOR NOW
if not defined var if NOT "%SchemeTest%" == "%NewName%"  echo ERROR: This file appears to hold data for more than one client, please review.

PAUSE 


rem commented out as never seems to run
echo should be out of the loop here
rem last entry should be end
REM if NOT "%ThreeChar%" == "END"  echo ERROR: This file appears to have no End row, please review. & goto ErrorExit
REM REMOVE ERROR EXIT FOR NOW
rem if NOT "%ThreeChar%" == "END"  echo ERROR: This file appears to have no End row, please review. 

ENDLOCAL
goto :eof

PAUSE

I think the problem is where I am putting these lines :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>rem commented out as never seems to run
echo should be out of the loop here
rem last entry should be end
REM if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review. & goto ErrorExit
REM REMOVE ERROR EXIT FOR NOW
rem if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review.
</code>
<code>rem commented out as never seems to run echo should be out of the loop here rem last entry should be end REM if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review. & goto ErrorExit REM REMOVE ERROR EXIT FOR NOW rem if NOT "%ThreeChar%" == "END" echo ERROR: This file appears to have no End row, please review. </code>
rem commented out as never seems to run
echo should be out of the loop here
rem last entry should be end
REM if NOT "%ThreeChar%" == "END"  echo ERROR: This file appears to have no End row, please review. & goto ErrorExit
REM REMOVE ERROR EXIT FOR NOW
rem if NOT "%ThreeChar%" == "END"  echo ERROR: This file appears to have no End row, please review. 

If I put them after the ENDLOCAL goto :eof they don’t run. If i put them before the ENDLOCAL goto :eof they run for every line.

I don’t need to run this bit of code on every line i just need to run it on the last line that has been read in. I was thinking that if i did this after the loop has finished this would work as the variable %threechar% should still have the value of the final pass through the loop.

I know that EOF exits a subroutine and endlocal resets the variables. So this needs to happen after the loop and before end local. I have tried various combinations and placements of the code and EOF, ENDLOCAL but at the moment i can only get it to run every time or not at all.

My code has lots of echo’s and pauses in so I can see what is happening line by line. There are also various rem statements to help me with debugging.

Any help would be appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật