cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Help with a batch file not concatinating it's logs

opnspaces
Navigator II
Navigator II
This is not for work, this is something I'm playing around with at home using Windows Server 2019. My scripting knowledge is fairly basic and I usually have to thread bits and pieces of working scripts together to make a new working script.

My current problem or ask is I have a batch file that starts a service and logs the results. I can only get the batch to log by creating multiple logs every time it runs. I want to only have one log that just updates (concatenates) a single existing log with new results.

What I want to happen is every time the batch file runs it:

Looks to see if RestartInternalDatabaseServices.log exists

If no log exists create the log
inside the log record the current date and time.
Record the results of the net start command.
add a separator of ============ between log entries.

If a log does already exist
concatenate or append the current date and time and net start results to the existing log.
add a separator of ============ between log entries.

I know this has to be a fairly simple problem for someone on this forum. Is anybody willing to take a crack at fixing it for me? Below is the script that I'm using.


::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Create a log file containing todays date
::LogPath is the path to the folder where the log file will be saved
::LogFileName is a descriptive name that will be added after the date
::LogFileExt is the extension to be used
::First get the date into a variable. Then convert the '/' to '-' so
::the '/' won't be mistaken as a command line switch
::if the file exists, a blank line and a separator line of equal signs
:: will be written at the end of the file in case the script runs more than
:: once a day.
::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal
set LogPath=C:\RestartServices\
set LogFileExt=.log
set LogFileName=RestartInternalDatabaseServices-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%%LogFileExt%
::use set MyLogFile=%date:~4% instead to remove the day of the week
set MyLogFile=%LogPath%%MyLogFile%_%LogFileName%

::Note that the quotes are REQUIRED around %MyLogFile% in case it contains a space
If NOT Exist "%MyLogFile%" goto:noseparator
Echo.>>"%MyLogFile%"
Echo.===================>>"%MyLogFile%"
goto:1startbatch

:noseparator
echo.%Date% >>"%MyLogFile%"
echo.%Time% >>"%MyLogFile%"

:1startbatch
net start MSSQL$MICROSOFT##WID >> _RestartInternalDatabaseServices-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%%LogFileExt% 2>&1
.
2001 Suburban 4x4. 6.0L, 4.10 3/4 ton **** 2005 Jayco Jay Flight 27BH **** 1986 Coleman Columbia Popup
18 REPLIES 18

opnspaces
Navigator II
Navigator II
Wow, I'm glad I checked back in here as I thought the thread was basically dead but obviously it wasn't

Thanks to all the suggestions I just finished getting it working today with a combination of removing the date and time from the log name plus the suggestion from wnjj which got me looking at it from a different angle.

Oh and thanks for the powershell suggestion 1492. Now that I have it up and running I might just look into how to do it in powershell just for grins. Maybe it's time I come out of the stone age a little bit. ๐Ÿ™‚
.
2001 Suburban 4x4. 6.0L, 4.10 3/4 ton **** 2005 Jayco Jay Flight 27BH **** 1986 Coleman Columbia Popup

lane_hog
Explorer II
Explorer II
2oldman wrote:
You need a thick skin and sense of humor to survive asking almost any question on here.


Put otherwise, you need to learn how to be an adult around here.

Not everything in life will be child-proofed and bubble-wrapped, and unfortunately the participation-trophy generation never quite learned how to accept being called an idiot from time to time.
  • 2019 Grand Design 29TBS (had a Winnebago and 3x Jayco owner)
  • 2016 F-150 3.5L MaxTow (had Ram 2500 CTD, Dodge Durango)
  • 130W solar and 2005 Honda EU2000i twins that just won't quit

Ron3rd
Explorer III
Explorer III
go to the www.pcmech.com forum and go to the programming section. Lots of programmers on that site. I've been a member for over 20 years but mostly visit the hardware stuff forum.
2016 6.7 CTD 2500 BIG HORN MEGA CAB
2013 Forest River 3001W Windjammer
Equilizer Hitch
Honda EU2000

"I have this plan to live forever; so far my plan is working"

2oldman
Explorer II
Explorer II
thestoloffs wrote:
I wouldn't place so detailed a technical question in a forum where only a few members might be qualified to answer
x2. You need a thick skin and sense of humor to survive asking almost any question on here.
"If I'm wearing long pants, I'm too far north" - 2oldman

thestoloffs
Explorer
Explorer
2012Coleman wrote:
And shame on those who feel the need to knock a question that isn't in their wheelhouse and therefore need to scold - humor aside. You just asking a question in a place your comfortable doing so.


As the millennials now say, I'm sorry, not sorry for what you perceived as scolding. I intended it as a polite suggestion; the OP is free to disregard, because of his stated dislike of those type of boards I suggested.

I started my comment by saying that, as a retired IT professional (and therefore potentially in my wheelhouse), I wouldn't place so detailed a technical question in a forum where only a few members might be qualified to answer -- as opposed to forums where almost everyone has a fair amount of experience in the occupation.

When you fish, don't you cast where the school of fish is located, rather than a mile away? That's all I meant to suggest.

But, I'll let this battle drop, now.

wnjj
Explorer II
Explorer II
First, as others have pointed out your log file name is built using the date and therefore will not be reused.

Second, why not simplify it all by just always writing the date, then the command, then the separator? That way no branching is required.

set LogPath=C:\RestartServices\
set LogFileExt=.log
set LogFileName=RestartInternalDatabaseServices
set MyLogFile=%LogPath%%LogFileName%%LogFileExt%

echo %Date% >> "%MyLogFile%"
echo %Time% >> "%MyLogFile%"

net start MSSQL$MICROSOFT##WID >> "%MyLogFile%"

echo โ€œ===================โ€œ >>"%MyLogFile%"


Note that I didnโ€™t test this so may have spaces or quotes wrong.

2012Coleman
Explorer II
Explorer II
I'm not great at batch files either - but it seems like your using a timestamp in the name of the log file, then checking to see if the name exists when writing a log - time marches on and therefore, the log file name you created a minute ago using date time minutes, seconds, milliseconds? will never exist again. Try leaving out the timestamp in the name - the OS will tell you when it was created in the properties if that info matters.

Maybe I read your logic wrong - but the advice to go to Stackoverflow.com is solid. You will have to create an account, and the crowd there can be tough sometimes.

And shame on those who feel the need to knock a question that isn't in their wheelhouse and therefore need to scold - humor aside. You just asking a question in a place your comfortable doing so.
Experience without good judgment is worthless; good judgment without experience is still good judgment!

2018 RAM 3500 Big Horn CTD
2018 Grand Design Reflection 303RLS

NRALIFR
Explorer
Explorer
thestoloffs wrote:
WesternRVParkOwner has it right.

Don't be surprised if this doesn't get a lot of responses. This is an RV-oriented board and this question is better oriented towards boards like Windows Ten Forums, where all the participants are coding jocks. (I'm a retired coding jock myself and would never post such a question here.)

Good luck with your problem.


IMHO, the term โ€œjockโ€ should be reserved for those who participate in activities that require wearing one. Since thereโ€™s no athletic or strenuous physical aspect to coding, I canโ€™t see how a jock might help prevent any potential coding injuries.

Sorry, but thoughts like this cross my mind when I canโ€™t sleep. ๐Ÿ˜›

:):)
2001 Lance 1121 on a 2016 F450 โ€˜Scuse me while I whinge.
And for all you Scooby-Doo and Yosemite Sam typesโ€ฆโ€ฆโ€ฆ..Letโ€™s Go Brandon!!!

lane_hog
Explorer II
Explorer II
Check StackOverflow... 99 times out of 100 I find an answer there...
  • 2019 Grand Design 29TBS (had a Winnebago and 3x Jayco owner)
  • 2016 F-150 3.5L MaxTow (had Ram 2500 CTD, Dodge Durango)
  • 130W solar and 2005 Honda EU2000i twins that just won't quit

T18skyguy
Explorer
Explorer
Well, I've learned a new word; concatinating. I'm going to concatinate a few beers later ๐Ÿ™‚
Retired Anesthetist. LTP. Pilot with mechanic/inspection ratings. Between rigs right now.. Wife and daughter. Four cats which we must obey.

1492
Moderator
Moderator
Have to agree that you are not likely to find answers to coding questions on Technology Forum.

I've had the most luck with such questions on stackoverflow.com. Though curious if you're using Windows Server 2019, why not use PowerShell to generate your logs? I haven't touched batch files in years?

opnspaces
Navigator II
Navigator II
Thank you all. I was prepared to have a few of topic responses to this and if I didn't know most of you from this forum I would probably be a bit hurt (okay no not really hurt) :p. But I know who you are enough to know who is tongue in cheek and I see the humor in it. ๐Ÿ™‚

Here's a few responses to the thread.

True it's an RV forum. But it's comprised of thousands of people who have a multitude of jobs and experiences. If somebody else had posted this question I would definitely have taken some time to see if I could figure it out and help them. If I'm on here then guaranteed others like me are on here too so I posted it. I even posted it in a section that is somewhat relevant.




WesternPark, Yep I know your posts well and definitely see the humor and a bit of sarcasm in it. ๐Ÿ™‚

thestoloffs wrote:
Don't be surprised if this doesn't get a lot of responses. This is an RV-oriented board and this question is better oriented towards boards like Windows Ten Forums, where all the participants are coding jocks. (I'm a retired coding jock myself and would never post such a question here.)

Honestly I've never had much luck with coding forums. Everybody seems to be more concerned with trying to show their superior knowledge or trying to steer you to either a third party solution or argue with you about why some other language is better. True this is not exactly the best place to post, but I figured there might be somebody possibly recently retired with the knowledge in their head and a few minutes on their hands.



Thanks bgum and Rover_Bill, I'll poke around with it more and see if I can figure it out.

2oldman wrote:
Or you could go to the Windows forum and ask about what tires to get.

If I was a regular contributor to a windows forum I might just answer that question. :C
.
2001 Suburban 4x4. 6.0L, 4.10 3/4 ton **** 2005 Jayco Jay Flight 27BH **** 1986 Coleman Columbia Popup

2oldman
Explorer II
Explorer II
Or you could go to the Windows forum and ask about what tires to get.
"If I'm wearing long pants, I'm too far north" - 2oldman

Rover_Bill
Explorer II
Explorer II
It's been a few years since I retired from programming. You may want to remove the %time functions from the file name but keep the time stamp within the file. Have one log file name per day or per hour.
2015 GMC Canyon 3.6L V6 4X4 TowHaul SLE ExtCab Bronze
2016 Keystone Passport GT 2670BH
ReCurve R6 hitch, DirecLink brake controller
2005 Suzuki C50 2006 Suzuki S40