First off, here is the minimum number of carets needed to get your existing code to work:
Code: Select all
REM Line without delayed expansion
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 Line with delayed expansion
call :Write_Log - [Info] - The script was executed under the account: "!currend_user!" ("Run as Administrator", from Windows Vista^^^^).
So you may ask, "That makes no sense
What is going on
"
It is actually very predictable if you understand
how the batch parser works. If you follow that link, the relevant information is buried in phases 2 and 5 (and phase 6 has the potential to add to the complexity)
First off, a ( literal only needs to be escaped if it precedes a command. That is never the case in any of your examples, so it never needs to be escaped.
The ) literal only needs to be escaped when it appears inside a parenthesized block of code. This is not the case in your CALL, but it is the case inside your :Write_Log subroutine. So this means any ) literal within %* must be escaped as ^) for the ECHO statement's phase 2. The trick is how to get that correct syntax to the subroutine.
In the first CALL without delayed expansion, you only need to worry about Phase 2. You need ^^) so that phase 2 changes the value to ^), and then the subroutine's phase 2 reduces the value to the desired ).
In the second CALL with delayed expansion, phase 5 needs the ^ to be escaped as ^^. So you need to start with ^^^^). The CALL statement phase 2 reduces that to ^^), and then phase 5 reduces that to ^), and finally the subroutine phase 2 reduces that to the desired ).
The reason I mentioned phase 6 is because it plays games with ^ characters. It doubles all ^ as ^^, but then restarts an additional round of phase 2 which reduces it back to ^ as long as the character is not quoted. But if the character is quoted, then it remains doubled, and there is nothing you can do about it. It is impossible to pass a single quoted ^ literal through a CALL statement - it will always be doubled. Thankfully, that is not a concern in your code.
But your code can easily be simplified so that you need not escape any characters
Parentheses are mostly useful when you want to execute multiple commands within an IF or FOR statement. But your IF statement is only executing a single command. So if you rewrite the subroutine as:
Code: Select all
:Write_Log
if "%Log_record%" == "yes" echo %date% !heure!%time:~5,3% %* >> "!Log_folder!!Log_name!"
exit /b
then you can simply use:
Code: Select all
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).
call :Write_Log - [Info] - The script was executed under the account: "!currend_user!" ("Run as Administrator", from Windows Vista).
One other note, you are using delayed expansion without any purpose - there is no need for delayed expansion in your code.
Delayed expansion serves two purposes in batch code:
1) It allows you to work with poison characters without worrying about escaping because the expansion occurs after phase 2, where poison characters are detected. So if you were to do something like ECHO !VAR! then you can be sure that the value of VAR will be preserved, regardless of the content.
2) It allows you to access the value of a variable that was set within the same code block. An entire block of code is parsed through phases 1 and 2 in one step, so normal expansion will show the value that existed before the block was entered. Below is a simple demonstration:
Code: Select all
@echo off
setlocal enableDelayedExpansion
set "var=BEFORE"
(
set "var=AFTER"
echo Percent Expansion yields %var%
echo Delayed Expansion yields !var!
)
--OUTPUT--
Code: Select all
Percent Expansion yields BEFORE
Delayed Expansion yields AFTER
Neither of the above conditions exist in your code, so you can safely use normal percent expansion in all cases, and you don't need to enable delayed expansion.
Dave Benham