Solved

Batch file Help

Posted on 2011-02-16
16
306 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

14 Experts available now in Live!

Get 1:1 Help Now