I have to restart the batch processing of a csv file several times to recover from certain errors. I don't want to reprocess the lines from the csv already successfully processed since that could waste lot of time. When a crash happens an error message gets printed out into my log file that looks in part like this:
.... Error found in row 3611. Exception ...
so I need to read the log file, find that row number and then restart my process at that line or maybe even right after it if I can't recover from the error. I have some code I can run to try to recover from the error. I'll need to copy my csv file from that line down and then rename the files so the new file has the same name as the original but I'd like to keep the original file too maybe with a date/time string appended to the filename. So, my questions: How do I find that row number programmatically and then use it to copy the file from that line down, ie if the number is 3611 then I want to skip the first 3610 line when I copy the file.
I need a batch script that will run on winxp without any extras installed, no unix utils, no powershell just basic batch.
Thanks
UPDATE: here is what my batch file looks like:
@echo. >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo %date% - %time% >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo. >> dataupdatelog.txt
RENAME PlayerSyncLog.txt PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2%.TXT
rem call download.bat >> dataupdatelog.txt
SET CSVFILE=smallfromwebsite.csv
call PlayerSync.exe -SYNC %CSVFILE%
rem i need a delay.bat here to allow the log file to get written before i try to parse it
:loop
findstr /c:"Import Successful!" "PlayerSyncLog.txt" >nul 2>&1 && (
rem tail.bat
GOTO FOUND
) || (
rem only loop for errors of type PK_MemberNumHistory
findstr /c:"PK_MemberNumHistory" "PlayerSyncLog.txt" >nul 2>&1 && (
fix.bat
RENAME PlayerSyncLog.txt PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2%.TXT
call PlayerSync.exe -SYNC %CSVFILE%
rem need a delay.bat here
GOTO loop
)
)
:FOUND
rem call backitup.bat >> dataupdatelog.txt
rem call upload.bat >> dataupdatelog.txt
rem call uploadlogs.bat >> dataupdatelog.txt
and here is what a line in my csv file looks like:
2004031,Robby,Brown,65 Lonely St.,Peterborough,,a2d3f4,,,,01/01/1952,01/01/1900,06/18/2013,,2/31/1969,4445556677,[email protected],,
and here is what the last lines in my log file look like after a crash:
12/17/2013 12:52:07: 19994017 updated successfully.
12/17/2013 12:52:07: 19999919 updated successfully.
12/17/2013 11:51:12: Violation of PRIMARY KEY constraint 'PK_MemberNumHistory'. Cannot insert duplicate key in object 'Players'.
The statement has been terminated.. Error found in row 12345. Exception Stack Trace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnec... etc
so in this case I want to find the string "Error found in row 12345" in my log, read the number part 12345 and then copy my csv from line 12346 (ie trim the first 12345 lines from my csv file) and then start processing again and just loop until the whole csv file is processed.
UPDATE 2: new scripts, main.bat:
@echo. >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo %date% - %time% >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo. >> dataupdatelog.txt
RENAME PlayerSyncLog.txt PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,2%.TXT
rem call download.bat >> dataupdatelog.txt
SET CSVFILE=fromwebsite.csv
call PlayerSync.exe -SYNC %CSVFILE%
:loop
findstr /c:"Import Successful!" "PlayerSyncLog.txt" >nul 2>&1 && (
tail.bat
goto success
) || (
rem only loop for errors of type PK_MemberNumHistory
findstr /c:"PK_MemberNumHistory" "PlayerSyncLog.txt" >nul 2>&1 && (
Fix.bat
copycsv.bat
RENAME PlayerSyncLog.txt PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,4%.TXT
call PlayerSync.exe -SYNC %CSVFILE%
goto loop
)
findstr /c:"PK_PlayerInfo" "PlayerSyncLog.txt" >nul 2>&1 && (
Fix.bat
copycsv.bat
RENAME PlayerSyncLog.txt PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,4%.TXT
call PlayerSync.exe -SYNC %CSVFILE%
goto loop
) || (
echo "some other error"
goto eof
)
)
:success
call backitup.bat >> dataupdatelog.txt
call upload.bat >> dataupdatelog.txt
call uploadlogs.bat >> dataupdatelog.txt
and the csv rewriter copycsv.bat:
@echo off
for /F "tokens=10 delims= " %%a in ('type PlayerSyncLog.txt ^| find /i "error found"') do set $row=%%a
set /a $row="%$row:.=%"
for /f "skip=%$row:.=% delims=" %%a in (fromwebsite.csv) do echo %%a>>newfile.csv
RENAME fromwebsite.csv "fromwebsite_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,4%.TXT"
RENAME newfile.csv fromwebsite.cs
so far so good it works except that after the call to copycsv.bat the next call to PlayerSync seems to be skipped and I the goto is ignored and I hit the echo "some other error" line and then the goto eof works. I think whats happening is the subsequent call to PlayerSync fails (also hits another error ) but delays writing to the log for a sec after it returns so the attempt to find an error in the log fails as it hasn't been written yet. How can I build in a delay of a few seconds? Thanks