Solved

Batch file Help

Posted on 2011-02-16
16
353 Views
Last Modified: 2012-05-11
Hi All

I am trying to create a batch file. I have setup a batch file to run some commands and output itself to log file. It works great however in the log file it displays error messages but doesnt show what there linked to (Remark)

For example

I could set the following batch file.

REM Test
"A batch command runs here"

in the log file it doesnt show REM Test. So if I get alot of errors I cant tell which is which.

Any ideas about this?

HMuser
0
Comment
Question by:hmuser
[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
  • 4
  • 3
  • +3
16 Comments
 
LVL 5

Expert Comment

by:danubian
ID: 34905406
You could redirect with echo, to log file.
echi REM Test > c:\mylogfile.log
0
 
LVL 5

Expert Comment

by:danubian
ID: 34905411
err...

echo REM Test > c:\mylogfile.log
0
 
LVL 10

Expert Comment

by:jfromanski
ID: 34905412
REMs are for comments. If you want to have them in logs add something like

ECHO REM Test

for each REM used.
0
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
LVL 35

Expert Comment

by:Ernie Beek
ID: 34905416
Remarks are just that, a remark within a batchfile. They don't generate any output which can be redirected to a log file.
You could add an echo to it: echo Test That should show up in the log.
0
 
LVL 35

Expert Comment

by:Ernie Beek
ID: 34905421
Woops, those guys are fast ;)
0
 
LVL 5

Expert Comment

by:h4mi
ID: 34905425
You do not specify how the log file is generated and written to, but an example of how to achieve this is:

echo.
echo Starting command 1>>logfile.txt
(command1)
echo.
echo Starting command 2>>logfile.txt
(command2)

etc...

Open in new window


Edit the comments and commands as fit. The "echo." lines are just blank lines for readability in the logfile.
0
 
LVL 5

Expert Comment

by:h4mi
ID: 34905432
If you use only one right arrow (>) the command erases everything in the logfile. Use double right arrows (>>) to add to the file instead.
0
 
LVL 5

Expert Comment

by:h4mi
ID: 34905448
Obviously the blank lines should also be followed by >>logfile.txt. My bad...

echo. >>logfile.txt

Open in new window

0
 
LVL 1

Author Comment

by:hmuser
ID: 34905637
Cant get it to work! I'm not brill on batch files.

Basically what I have at the moment is a batch file with loads of REMs (Each site) and within each REM a command to delete files on remote machines. The command executes perfectly and log file out puts fine but it will just state the error not which site its linked to so I dont know which ones failed.

I have another file which is shown below

call "Promotions_Removal.bat" 2>"c:\TEMP\Log Files\removallog.txt"

pause

This calls my command batch file in and outputs it to the file show.

Where am I going wrong?

HMUSER
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 34905680
Another way for debugging is to NOT use @echo off at the beginning, and redirect ALL output to a log file, like with

ThisIsMyBatch.cmd >> logfile.txt 2>&1

That will redirect standard output (which contains the commands and any output of commands) and standard error (all error messages).   2>&1 has the special meaning of "use the same file as with standard output".
0
 
LVL 5

Expert Comment

by:danubian
ID: 34905751
I'd intercept the site/machine name on the batch and echo this name on the log.

something like:

set cSite = NewSiteNameHere

and/or

echo %NewSiteNameHere% >> logfile.txt
(commands)
0
 
LVL 1

Author Comment

by:hmuser
ID: 34906503
Ok tell me where im going wrong.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ECHO REM ##site name## 1>>"c:\TEMP\Log Files\removallog.txt"

del"\\0.0.0.0\site name\TEMP\HeadmastersPromotions\Picture.bmp" 1>>"c:\TEMP\Log Files\removallog.txt"

del"\\0.0.0.1\site name\TEMP\HeadmastersPromotions\Picture.bmp" 1>>"c:\TEMP\Log Files\removallog.txt"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

But can't pipe out it will either only pipe out the REM. Rest wont appear!
0
 
LVL 5

Expert Comment

by:danubian
ID: 34906810
If you try to batch delete some files, as I know 'del' has no errorlevel (it returns 0 no matter).

So, what event do you expect to return the del line, to write on the log file ?
0
 
LVL 1

Author Comment

by:hmuser
ID: 34906932
I managed to solve it.

I kept my 2nd batch file

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call "Promotions_Removal.bat" 2>"c:\TEMP\Log Files\removallog.txt"

pause

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

and ammended it so it now looks like this...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call "Promotions_Removal.bat" >"c:\TEMP\Log Files\removallog.txt"2>&1

pause

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Be nice if I could still see the cmd text while it was running, but the main thing is that its working and piping everything to a text file.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 34908135
The default behaviour of batch files is to echo all commands. You might have that changed? Whatsoever, you can force output by putting
@echo on
as first command in each batch file. The at prevents the echo command from being echoed.
0
 
LVL 5

Accepted Solution

by:
h4mi earned 250 total points
ID: 34908283
If you add >file.txt it will echo everything in there instead of in the prompt. No output will be shown. The standard way of removing ugly outputs from commands is >nul which sends all output to nothing.

A way to get the info back is adding a line to make the batch file look like this:
call "Promotions_Removal.bat" >"c:\TEMP\Log Files\removallog.txt"2>&1
type c:\TEMP\Log Files\removallog.txt
pause

Open in new window

It will view the log file after promotions_removal.bat is finished.

If you give us the contents of promotions_removal.bat we could help you edit it to your satisfaction.


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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