Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch file Help

Posted on 2011-02-16
16
Medium Priority
?
358 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

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.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

824 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