how to echo to screen in a FOR loop?

Microsoft Microsoft windows xp pro full...
February 12, 2010 at 23:38:13
Specs: Windows XP, 2.4 3GB
Let's say I was using the following FOR loop:

@echo off
setlocal EnableDelayedExpansion
FOR /F %%z IN (list.txt) DO (
set line=!date! !time! %%C
psexec \\%%z "C:\WINDOWS\system32\calc.exe" >nul 2>&1 || set line=!line! Error
echo !line!
) >>results.txt

How would I insert an echo in the loop... that is displayed on the command line window? I want something like: ECHO launching program on computer %%z

My problem is that because I have my text file I'm writing to, it's putting any ECHO command I give it in the text file. I want everything else in the text file.. except for this. I just want this line presented to the user everytime the program loops through.

Ideas? Thanks!


See More: how to echo to screen in a FOR loop?

Report •


#1
February 12, 2010 at 23:52:16
this might work, but i have not tried it:

FOR /F %%z IN (list.txt) DO (
set line=!date! !time! %%C
psexec \\%%z "C:\WINDOWS\system32\calc.exe" >nul 2>&1 || set line=!line! Error
call :bb
) >>results.txt
goto :eof

:bb
echo !line!


Report •

#2
February 13, 2010 at 01:27:56
nbrane's method should work but there are more convenient
options.

Perhaps sending the echo output to either "con" or "stderr" is a
good solution. Using con would ensure that it always gets
sent to the console regardless, while using stderr gives the
option of redirecting the output at startup.

FOR /F %%z IN (list.txt) DO (
set line=!date! !time! %%C
psexec \\%%z "C:\WINDOWS\system32\calc.exe" >nul 2>&1 || set line=!line! Error

rem sending to stderr
1>&2 ECHO launching program on computer %%z

rem sending to "con"
>con ECHO launching program on computer %%z

) >>results.txt


If you use something like this to start the batch the you can
redirect the echo sent to stderr, but not the echo sent to "con".

2>errs.txt mybatch.bat


Report •

#3
February 13, 2010 at 07:01:12
Most commands in the FOR loop do not have output, so why outputting the whole loop ?

@echo off
setlocal EnableDelayedExpansion
FOR /F %%z IN (list.txt) DO (
set line=!date! !time! %%C
psexec \\%%z "C:\WINDOWS\system32\calc.exe" >nul 2>&1 || set line=!line! Error
echo !line! >> results.txt
ECHO launching program on computer %%z >> results.txt
)


Report •

Related Solutions

#4
February 13, 2010 at 10:44:19
I'm outputting the results because the list will be a list of computers that I'll be installing an .msi to. I want to know which ones it copied to.. and which ones it installed to sucessfully.
Thanks for the tips. I'll work on it more tomorrow and see if I can come up with a working solution from these.

By the way... Your solution didn't work nbrane. It just writes to the text file twice. Thanks though!


Report •

#5
February 14, 2010 at 05:23:25
My question was not really like that, I mean, if you have 6 commans in a FOR loop, and one of them produces console output, why would you forward all ouput of the complete FOR loop, but rather redirect only that one command ... especially if you have issues differntiating output between two commands inside the loop. The "con" thing is great, but it'll also mean there is no output to any file, for that command.

If you look for such a solution (output to both screen AND to a file), you may need the (non-Windows native) command called TEE


Report •

#6
February 14, 2010 at 20:21:33
ahh.. well that explains some things. Thanks for the lesson. I'm new to batch file writing so much to learn. I've decided it's just easier to write everything to the text file and open it. I was only trying to write to console so that I could look over and see how long the process still had. I'm deploying an install to about 50 workstations and the copy size will be about 25MB each. I'm a software vendor at this company, so the real IS guys won't give me access to SMS or Group Policy to deploy... so I figure this will be a good workaround... 1 loop to copy to all the workstations with error file to show any problems.. and then another loop to execute the msi (with .ini silent install instructions).
Thanks for the advice guys.

Report •


Ask Question