Solved

Batch file Help

Posted on 2011-02-16
16
316 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 68

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 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now