0

I made a simple batch file in windows. it works reporting to find the URL including error. but all URL echo only success.

wget command stdout seems like empty. redirection pipe is not work. what's wrong??

@echo off
for /f "delims=" %%i in (TEST.txt) do call :request %%i
set /p in=Finish!

:request
echo | set /p= %1 >> result.txt
wget.exe -T 3 --tries=2 %1 | findstr /I "error"
if %errorlevel% == 0 (
    echo error >> result.txt
) else (
    echo success >> result.txt
)

My problem is solved. Modified code from a good answer

@echo off
set logFile="log.txt"
set resultFile="result.txt"

for /f "delims=" %%i in (TEST.txt) do call :request %%i 
set /p in=Finish!

:request
echo|set /p= %1 >> %resultFile%
wget.exe -P download -T 3 --tries=2 %1  2>&1 | tee -a %logFile% | findstr /I "error" > nul
if %errorlevel% == 0 (
    echo error >> result.txt
) else (
    echo success >> result.txt
)
김현우
  • 23
  • 6
  • what is the extenstion of you batch file? `.bat` or `.cmd` ? – Gerhard Apr 12 '18 at 08:24
  • It's .bat. Is it related? – 김현우 Apr 12 '18 at 08:52
  • oh!! thanks! I didn't think about difference .bat between .cmd file. reference - https://stackoverflow.com/questions/148968/windows-batch-files-bat-vs-cmd – 김현우 Apr 12 '18 at 08:58
  • yes, so errorlevel will be reset with cmd :) Is it working now? – Gerhard Apr 12 '18 at 09:06
  • is it working now as `.cmd` ? – Gerhard Apr 12 '18 at 14:08
  • @GerhardBarnard - FINDSTR is an external command, so it always sets ERRORLEVEL to 0 or 1. The `.bat` vs `.cmd` issue only arises with some internal commands. – dbenham Apr 12 '18 at 20:22
  • @dbenham. I am aware of that, but tested this issue with `.cmd` and did not replicate OP issue, hence the comment. Not sure if it helped him though so was waiting on a response. – Gerhard Apr 12 '18 at 20:26
  • My gnu wget writes the diagnostic info to stderr, so redirection would be needed. See my answer. – dbenham Apr 12 '18 at 20:50
  • This works well after i change it to ".cmd". I use wget downloaded in the gnu utility, but in the case of findstr, I could use it without any extra work. I don't understand that findstr is an external command. – 김현우 Apr 13 '18 at 01:06
  • oh.. Is your statement related to the tee command in my modified code? – 김현우 Apr 13 '18 at 01:09

2 Answers2

1

The wget diagnostic information that you are trying to read is written to stderr, not stdout. So you simply need to redirect stderr to stdout before your pipe.

Assuming all your wget arguments are correct, I would write the code as follows:

@echo off
>result.txt (for /f "delims=" %%i in (TEST.txt) do (
  wget.exe -T 3 --tries=2 %%I 2>&1|findstr /i error >nul&&echo %%i ERROR||echo %%i SUCCESS
))
echo Finished!
dbenham
  • 119,153
  • 25
  • 226
  • 353
  • EDIT - Fixed the code logic - my original answer had it reversed, and the critical redirection was missing – dbenham Apr 12 '18 at 20:50
0

I assume, you want to echo only to result.txt instead to terminal/console.

if %errorlevel% == 0 (
    echo error >> result.txt > /dev/null 2>&1
) else (
    echo success >> result.txt > /dev/null 2>&1
)

These changes not allowed to display on console/terminal

ntshetty
  • 1,082
  • 8
  • 20