opnspaces
Sep 03, 2019Navigator II
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
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