Forum Discussion

opnspaces's avatar
opnspaces
Navigator II
Sep 03, 2019

Help with a batch file not concatinating it's logs

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
  • 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?
  • 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
  • Or you could go to the Windows forum and ask about what tires to get.
  • 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.
  • 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.
  • I would write it to take the results of the net start command and add that to the results of the log (what ever you name it) then delete prior log. Or maybe you could keep prior log.
  • Most likely either an open neutral, insufficent GVWR, failure to use a WDH, or the computer came with China Bomb tires. It might help if you switch to single ply RV toilet paper and leave a couple of gallons of water in the hard drive to clean out the storage while traveling down the road. If you can get those suggestions concatenated, you should be fine.
  • Can't imagine why there are no responses yet from the RV crowd.:)