0

In my batch I process a csv file, upon hitting an error in processing the playerSync.exe file the batch will loop and restart processing the csv file after the line that hit the problem. The trouble is that the next call to findstr will fail to find the error message even though it's there. The tail.bat file shows it but the call to findstr fails to see it and so the else message "some other error" gets printed and eof is reached. Or is the goto loop line just getting skipped and the processing falling through to goto eof somehow? The wait.bat, copycsv.bat seem to run right without problem so I doubt the trouble is in there. So my question is why can't I find the same string with repeated calls to findstr in my batch file ? UPDATE - NEW QUESTION: why is the goto command ignored in my batch file?

@echo. >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo %date% - %time% >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo. >> dataupdatelog.txt
@echo off
REM RENAME PlayerSyncLog.txt "PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,4%.TXT"

rem call download.bat >> dataupdatelog.txt
SET CSVFILE=fromwebsite.csv
REM call PlayerSync.exe -SYNC %CSVFILE%
:loop
time /T
findstr /c:"Import Successful!" "PlayerSyncLog.txt" >nul 2>&1 && (
    tail.bat
    goto success    
) || (

    findstr /c:"Error found in row" "PlayerSyncLog.txt" >nul 2>&1 && (
        echo "found exception  error"

        echo "copycsv"
        copycsv.bat
        time /T 
        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%
        time /T 
        echo "wait 240"
        wait.bat 240
        time /T
        echo "GOTO loop"
    GOTO loop
    echo "after GOTO loop"
) || (
    echo "some other error"
    GOTO success
))  

:success

rem call backitup.bat >> dataupdatelog.txt
rem call upload.bat >> dataupdatelog.txt
rem call uploadlogs.bat >> dataupdatelog.txt

UPDATED CODE: added the /I to the findstr command and also added the line echo "after goto loop" to see if the goto was ignored and it was, the "after goto loop" gets echoed, eg output from a run is:

C:\Documents and Settings\Administrator\Desktop\website xfer>mybatch.bat
"loop"
02:52 PM
"check for error"
"found exception  error"
(1 row affected)
"copycsv"
02:54 PM
"call playersync after error found"
02:54 PM
"wait 240"
02:58 PM
"goto loop"
"after goto loop"
C:\Documents and Settings\Administrator\Desktop\website xfer>

So, new question, why is the goto command ignored?

UPDATE 2: the new scripts seems to be working :

@echo. >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo %date% - %time% >> dataupdatelog.txt
@echo ============================================ >> dataupdatelog.txt
@echo. >> dataupdatelog.txt
@echo off
rem RENAME PlayerSyncLog.txt "PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,4%.TXT"

rem call download.bat >> dataupdatelog.txt
SET CSVFILE=fromwebsite.csv
rem call PlayerSync.exe -SYNC %CSVFILE%
rem echo "wait 240"
rem call wait.bat 240

GOTO BEGIN
:LOOP
echo "loop"
time /T 


call  copycsv.bat

time /T 

RENAME PlayerSyncLog.txt "PlayerSyncLog_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%%time:~3,2%%time:~6,4%.TXT"

echo "call playersync after error found"
call PlayerSync.exe -SYNC %CSVFILE%
time /T 
echo "wait 240"
call wait.bat 240
time /T

rem END LOOP    


:BEGIN


rem tail.bat
findstr /I /c:"Import Successful!" "PlayerSyncLog.txt" >nul 2>&1 && (
    echo "success"
    SET SUCCESS=0
) || (
    echo "check for error"    
    findstr /I /c:"Error found in row" "PlayerSyncLog.txt" >nul 2>&1 && (
        echo "found exception  error"
        SET SUCCESS=1

    ) || (
        echo "some other error"
        SET SUCCESS=2
    )
)   

echo "SUCCESS==" %SUCCESS%
if %SUCCESS% == 1 ( GOTO LOOP ) else ( GOTO END )


:END

rem call backitup.bat >> dataupdatelog.txt
rem call upload.bat >> dataupdatelog.txt
rem call uploadlogs.bat >> dataupdatelog.txt
The Newbie Qs
  • 483
  • 8
  • 22
  • 1
    It doesn't make sense that `"after goto loop"` is being echoed at all. When `wait.bat 240` is run, execution is transferred to the `wait.bat` file because you didn't use `call`, like this: `call wait.bat 240`. It doesn't transfer control back to this batch-file when `wait.bat` is done unless you use `call`. – James L. Dec 23 '13 at 19:25
  • Isn’t the interpreter looking for the label “loop” just in the region bordered with parentheses? I haven’t used `goto` in such case yet and I think I shouldn’t… – Melebius Dec 23 '13 at 19:41
  • @Melebius: no, `goto` doesn`t care about parentheses. It just breakes out of the block and jumps. – Stephan Dec 23 '13 at 19:47
  • Thanks all, I'm working a on a work around based on this thread http://stackoverflow.com/questions/8481558/windows-batch-goto-within-if-block-behaves-very-strangely will update soon – The Newbie Qs Dec 23 '13 at 20:08
  • and James, while testing a new version of this script I found that my calls to my other bat files ran but then killed my parent script when they finished so I guess that's what you meant by needing the call to "call .." – The Newbie Qs Dec 23 '13 at 20:11
  • see update 2, that script is running now and seems to work. – The Newbie Qs Dec 23 '13 at 20:41
  • after running for a while, and looping numberous times, the script crashed with the error message: "The syntax of the command is incorrect." could a variable like CSVFILE just lose its value if the script runs awhile? should I "setlocal enabledelayedexpansion" ? – The Newbie Qs Dec 23 '13 at 21:12
  • A variable should remain until the cmd prompt is closed, or it is changed. If a script is self-modifying then odd things can happen, like that error message. – foxidrive Dec 24 '13 at 07:15

0 Answers0