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 :
1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada adafaafaf
1212314214152156182651265172651782658172561651SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada adafaafaf
1312442141431421415215618265126517265178265817SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOFailSNumbersadada adafaafaf
41251515142141521561826512651726517826581725616SAMPLETEXTTOCHECKNumbersadada adafaafaf
4125151514214152156182651265172651782658172561SAMPLETEXTTOCHECKNumbersadada adafaafaf
So the line with SAMPLETEXTTOFail should fail and this is working correctly. But I cannot get my code to check the last line.
SETLOCAL DisableDelayedExpansion
REM hard coded for now - this is the value i want to check against
set "NewName=SAMPLETEXTTOCHECKN"
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ D:sample.TXT"`) do (
SETLOCAL EnableDelayedExpansion
Rem get first three characters for testing End is present on last row
set "ThreeChar=%Line:~0,3%"
Rem get first character to check if a number as data rows to check start with a number
set "FirstChar=%Line:~0,1%"
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)
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
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.
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>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 :
<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.
</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.