Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch file Help

Posted on 2011-02-16
16
Medium Priority
?
355 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:Headmasters
[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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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:Headmasters
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 71

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:Headmasters
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:Headmasters
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 71

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 1000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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