?
Solved

How do I append running result logs to text file in a batch

Posted on 2014-02-17
4
Medium Priority
?
745 Views
Last Modified: 2014-02-25
I use the batch below. with the command pspasswd I am changing the local administrator password of the several machines which listed in "MachineSource.txt".
To Access to the machine admin user is "authorized_user" and password is "authorized_user_pass".
I want to have Error log. But the end part of the command, you se >2 d:\Errorlog.txt" saving only the last transaction's log.
How do I append every transaction's or only error transactions's log that pspasswd command does.


The Batch is:

@echo off
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I

goto :EOF

:begin
set srv=%1
:get info

pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 2> "D:\ERRORLOG.TXT"
0
Comment
Question by:certuran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 1000 total points
ID: 39864932
You need to use ">>" to not overwrite an existing file.
Only Errorlog to file:
@echo off
set ErrorLog=D:\ERRORLOG.TXT
if exist "%ErrorLog%" del "%ErrorLog%"
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I
goto :EOF

:begin
set srv=%1
:get info
pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 2>>"%ErrorLog%"

Open in new window

Standardlog and Errorlog separately:
@echo off
set StandardLog=D:\STANDARDLOG.TXT
set ErrorLog=D:\ERRORLOG.TXT
if exist "%StandardLog%" del "%StandardLog%"
if exist "%ErrorLog%" del "%ErrorLog%"
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I
goto :EOF

:begin
set srv=%1
:get info
pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 1>>"%StandardLog%" 2>>"%ErrorLog%"

Open in new window

And combined in one file:
@echo off
set CombinedLog=D:\COMBINEDLOG.TXT
if exist "%CombinedLog%" del "%CombinedLog%"
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I
goto :EOF

:begin
set srv=%1
:get info
pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 1>>"%CombinedLog%" 2>&1

Open in new window

0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39865162
I often do these slightly differently so wanted to share a different style.  I wrap the chunk of code that I want to capture output from in a containing set of parens, and then after the last paren redirect the STDOUT and STDERR streams as desired (showing them merged below). This makes the code a little easier to maintain I find, and removes the need for specifying the output redirection on each command when there are multiples in the block of code.

This approach also saves having to DEL the output file manually if you want to start it fresh each execution.

I also wanted to show a technique where you can place the program execution right in the FOR loop, saves code and will run slightly faster.

@echo off

set MachineFile=MachineSource.txt
set LogFile=D:\ERRORLOG.TXT

(
  for /F "usebackq delims==" %%I in ("%MachineFile%") do (
    pspasswd \\%%I -u auhorized_user -p authorized_user_pass administrator abc123
  )
) >"%LogFile%" 2>&1

Open in new window

~bp
0
 

Expert Comment

by:SmartRxCloud
ID: 39865307
@echo off
for /F "usebackq delims==" %%I in ("MachineSource.txt") do Call :begin %%I

goto :EOF

:begin
set srv=%1
:get info

pspasswd \\%srv% -u auhorized_user -p authorized_user_pass administrator abc123 2[b]>> [/b]"D:\ERRORLOG.TXT"

Open in new window



more info refer this link


http://www.robvanderwoude.com/redirection.php
0
 

Author Closing Comment

by:certuran
ID: 39885194
It worked proper to just what I wanted to do. And much, you have given alternatives those all usefull. Thank you very much.
0

Featured Post

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question