Manual vs. automated execution problems

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sweener
Posts: 18
Joined: 06 Jan 2010 13:00
Location: Indianapolis, IN

Manual vs. automated execution problems

#1 Post by sweener » 29 Jan 2010 12:59

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Manual vs. automated execution problems

#2 Post by !k » 29 Jan 2010 14:16

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Manual vs. automated execution problems

#3 Post by avery_larry » 29 Jan 2010 16:49

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
)

Post Reply