Simple log function does not work with some characters (such as parentheses or variables)
Posted: 13 Mar 2019 01:59
Hello,
I am currently working on a script in which I would like to set up a log recording function.
But my function doesn't work well. Depending on what I send, some characters (such as parentheses or variables) crash my script.
I think this comes from having set a condition to check whether or not the recording is enabled. Because without it, it works, but I want to keep it.
Here is a part of my script with some examples of what works and what doesn't:
I also tried to retrieve the function settings with "$~1" and put the text between quotes, but it doesn't work any better.
Do you have any advice to give me to help me?
With kind regards.
I am currently working on a script in which I would like to set up a log recording function.
But my function doesn't work well. Depending on what I send, some characters (such as parentheses or variables) crash my script.
I think this comes from having set a condition to check whether or not the recording is enabled. Because without it, it works, but I want to keep it.
Here is a part of my script with some examples of what works and what doesn't:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "Log_record=yes"
set "Log_folder=D:\Logs\"
for /f "delims=" %%G in ('time /t') do set heure=%%G
echo %date%>len_date
for %%a in (len_date) do set /a len_date=%%~za -2
REM English OS, also European
if !len_date! gtr 10 (
set datetime=%date:~-4%-%date:~4,2%-%date:~7,2%_%heure:~0,2%-%time:~3,2%-%time:~6,2%
) else (
set datetime=%date:~-4%-%date:~3,2%-%date:~0,2%_%heure:~0,2%-%time:~3,2%-%time:~6,2%
)
set "Log_name=Log_%computername%_!datetime!.txt"
for /f %%A in ('whoami') do set "currend_user=%%A"
REM Work
call :Write_Log - [Error] - The script isn't running under an administrator account or there has been no increase privileges ^^^("Run as Administrator", from Windows Vista^^^).
REM Work
call :Write_Log - [Error] - The script wasn't executed under the account : "!currend_user!".
REM Don't work, error : ". était inattendu." in french, like : ". was unexpected."
call :Write_Log - [Info] - The script was executed under the account: "!currend_user!" ^^^("Run as Administrator", from Windows Vista^^^).
goto :END
:Write_Log
if "!Log_record!" == "yes" (
echo %date% !heure!%time:~5,3% %* >> "!Log_folder!!Log_name!"
)
goto :eof
:END
call :Write_Log - [Info] - End of script execution.
pause
I also tried to retrieve the function settings with "$~1" and put the text between quotes, but it doesn't work any better.
Do you have any advice to give me to help me?
With kind regards.