2

I want to create a filewatcher in batch-file:

Watch for file in folderA (.zip) → unzip them to folderB (with same name) → and trigger a batch file → do the same for more incoming .zip files.

I checked related questions to this in StackOverflow but, I need some more help

:STARTPROCESS
CD /D %ROOT_DIR%
FOR /F "tokens=1-2 delims=." %%G in ('dir *.zip /b') do 
(
REM unzip file
%SEVENZIP_PATH%\7z.exe x "%ROOT_DIR%" -o%UNZIP_FOLDER% -y >> %LOG%
echo %%G -- unzip complete

REM run batch1
:BATCH1

REM check ERROR from batch1 log   --> i have a problem from here nothing below works

findstr /M "ERROR" %UNZIP_FOLDER%\%%G\Logs\*.log 
If %ERRORLEVEL%==0 echo Error Found
move /Y "%UNZIP_FOLDER%\%%G" "%Folder1% >> %LOG%
goto STARTPROCESS
else 
(
echo %%G Batch1 OK
goto BATCH2
)

REM run batch2
:BATCH2

REM check ERROR from batch2 log
findstr /M "Total Count : 0" %UNZIP_FOLDER%\%%G\Data\*_Output.log 
If %ERRORLEVEL%==0 echo %%G Batch2 OK
goto STARTPROCESS
else 
(
echo ERROR
move /Y "%UNZIP_FOLDER%\%%G\fileA.xml" "%UNZIP_FOLDER%\bin >> %LOG%
%SEVENZIP_PATH%\7z.exe a "%UNZIP_FOLDER%\%G%" -o%ZIP_FOLDER% -y >>%LOG%
)
)

timeout 60
goto STARTPROCESS
cmd /k
npocmaka
  • 55,367
  • 18
  • 148
  • 187

1 Answers1

3

I see two main problems with your code. The first is that you can't use labels inside of for loops. To get around this, you can move the :BATCH1 and :BATCH2 labels outside of the loop and into a subroutine (of course, %%G won't exist outside the loop so you need to pass it to the new subroutine as a parameter as well).

The second problem I see is that your parentheses are not in the right place (and possibly that your indentation is messed up, but that may have just been a copying error). The ( of a for loop needs to be on the same line as do and an else needs to be written as ) else (. You're also missing the initial ( in both of your if statements. Basically, batch locks you in to K&R style.

Ultimately, your code is going to look something like this:

:STARTPROCESS
CD /D %ROOT_DIR%
FOR /F "tokens=1-2 delims=." %%G in ('dir *.zip /b') do (
    REM unzip file
    %SEVENZIP_PATH%\7z.exe x "%ROOT_DIR%" -o%UNZIP_FOLDER% -y >> %LOG%
    echo %%G -- unzip complete

    REM run batch1
    call :BATCH1 "%%~G"
)

timeout 60
goto STARTPROCESS
cmd /k

:BATCH1
REM check ERROR from batch1 log

findstr /M "ERROR" %UNZIP_FOLDER%\%1\Logs\*.log 
If %ERRORLEVEL%==0 (
    echo Error Found
    move /Y "%UNZIP_FOLDER%\%%G" "%Folder1% >> %LOG%
    exit /b
) else (
    echo %%G Batch1 OK

    REM check ERROR from batch2 log
    findstr /M "Total Count : 0" %UNZIP_FOLDER%\%1\Data\*_Output.log 
    If %ERRORLEVEL%==0 (
        echo %%G Batch2 OK
        goto STARTPROCESS
    ) else (
        echo ERROR
        move /Y "%UNZIP_FOLDER%\%%G\fileA.xml" "%UNZIP_FOLDER%\bin >> %LOG%
        %SEVENZIP_PATH%\7z.exe a "%UNZIP_FOLDER%\%1" -o%ZIP_FOLDER% -y >>%LOG%
    )
)
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • it worked.! but found another problem: when I pass %%G from the main loop to the subroutine (it passes OK, but misses out the "\" ) and hence i get error. like this ` findstr /M "KO" %UNZIP_FOLDER%\%1\Logs\%1.log ` becomes ` d:\LocalData\\Desktop\unzip"testfile\Logs"testfile.log ` – PrimeSuspect Mar 22 '17 at 08:09
  • the missing part was to add double \\, I added \\ to the %1 and it worked as expected.!! – PrimeSuspect Mar 22 '17 at 10:24