Page 1 of 1

Manual vs. automated execution problems

Posted: 29 Jan 2010 12:59
by sweener
I have an audit process that I developed and I made a tweak to it and now it doesn't work as expected when run as a scheduled task. I search for a string in a file and save the output in a temporary file. Here's what I see in the output when automated:

Code: Select all

---------- D:\PATH\FILE.TXT: 0

but when run manually:

Code: Select all

---------- D:\PATH\FILE.TXT: 1


Here is a segment of the code I have in place:

Code: Select all

IF EXIST "D:\FTP\%PROCNAME%\*.txt" FOR /F "tokens=*" %%a in ('dir "D:\FTP\%PROCNAME%\*.txt" /b') DO (
  FINDSTR /B /I /N ".put .get put get" "D:\FTP\%PROCNAME%\%%a" > NUL
  IF !errorlevel!==0 FIND /I /C "dir" D:\FTP\%PROCNAME%\%%a >> D:\altPath\dir.txt
)


I've messed around putting SETLOCAL ENABLEDELAYEDEXPANSION and/or SETLOCAL ENABLEEXTENSIONS at the top of this sub batch but it makes things worse.

Anyone have a simple answer? Thanks!
-Sweener

Re: Manual vs. automated execution problems

Posted: 29 Jan 2010 14:16
by !k
Try this

Code: Select all

FINDSTR /B /I /N ".put .get put get" "D:\FTP\%PROCNAME%\%%a" > NUL && FIND /I /C "dir" D:\FTP\%PROCNAME%\%%a >> D:\altPath\dir.txt

Re: Manual vs. automated execution problems

Posted: 29 Jan 2010 16:49
by avery_larry
I haven't really researched it, but I'm guessing the very nature of the errorlevel variable makes using it as you have it rather complicated. If you use

%errorlevel%

then it'll be the errorlevel value when the for loop is first called. If you use

!errorlevel!

then my guess is that it may return the value of errorlevel after the time that you want -- because it delays the expansion. I have started using the && and the || notation instead of referencing the errorlevel as !k showed. However, you can use the special errorlevel notation:

Code: Select all

IF EXIST "D:\FTP\%PROCNAME%\*.txt" FOR /F "tokens=*" %%a in ('dir "D:\FTP\%PROCNAME%\*.txt" /b') DO (
  FINDSTR /B /I /N ".put .get put get" "D:\FTP\%PROCNAME%\%%a" > NUL
  IF not errorlevel 1 FIND /I /C "dir" D:\FTP\%PROCNAME%\%%a >> D:\altPath\dir.txt
)