Page 1 of 1

Parenthetical Redirection

Posted: 21 Jul 2021 13:01
by atfon
I was trying to find out more about the rules of redirection to file through the use of parentheses. In some cases, I find this works fine, but in others the batch script crashes. I am attempting to collect some system information and write it to file. Here is an example of where this is failing:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=3 delims=: " %%a in ('wevtutil qe Security /q:"*[System[(EventID=4624)]] and *[EventData[Data[@Name='LogonType'] and (Data=2 or Data=11)]]" /c:1 /rd:true /f:text ^|findstr "Type:"') do for /f "tokens=*" %%b in ("%%a") do set logonType=%%b
if %logonType% EQU 11 set logonTypeInfo=Cached Interactive (Locally stored network credentials)
if %logonType% EQU 2 set logonTypeInfo=Interactive (A user logged on to this computer)
for /f "tokens=* delims=: " %%c in ('wevtutil qe Security /q:"*[System[(EventID=4624)]] and *[EventData[Data[@Name='LogonType'] and (Data=2 or Data=11)]]" /c:1 /rd:true /f:text ^|findstr "Date:"') do for /f "tokens=*" %%d in ("%%c") do set logonDate=%%d
if defined logonDate set logonDate=!logonDate:~6,-9!
set logonInfo=%logonDate:~5,2%/%logonDate:~8,2%/%logonDate:~0,4% at %logonDate:~11,8%
set exportPath="%userprofile%\info_%username%.txt"
(
echo Logon Info:
echo Last Logon: %logonInfo% [%logonTypeInfo%]
)> %exportPath%
If I remove the brackets after the echo, it prints the information to the console. If I remove the parentheses surrounding "Locally stored network credentials" and "A user logged on to this computer" it echos the information to file. I tried escaping the parentheses, but that did not work. Is there a way to keep the parentheses? If not, are there any other rules I should be aware of when re-directing to file with parentheses?

I know I can do this, but I'd like the code to look cleaner:

Code: Select all

echo Logon Info:  >%exportPath
echo Last Logon: %logonInfo% [%logonTypeInfo%] >> %exportPath

Re: Parenthetical Redirection

Posted: 21 Jul 2021 13:13
by Squashman
Use this as a best practice.

Code: Select all

set "logonTypeInfo=Cached Interactive (Locally stored network credentials^)"

Re: Parenthetical Redirection

Posted: 21 Jul 2021 13:23
by atfon
Thank you @Squashman. Quoting the Set command with the single parentheses escape did the trick. Are there any other potential pitfalls which should be kept in mind when re-directing to file through parentheses?

Re: Parenthetical Redirection

Posted: 21 Jul 2021 13:46
by aGerman
Recently I wrote some tips for best practice.
viewtopic.php?f=3&t=10137
So, you may enable delayed variable expansion right before you redirect the strings. (And maybe disable it after that, in case that more code is following.)
This has not necessarily something to do with the parethesized block. However, not even the closing parenthesis needs to be escaped this way.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "var1=foo(bar)"
set "var2="
set "var3=foo&bar&baz"
set "file=example.txt"
setlocal EnableDelayedExpansion
>"!file!" (
  echo(!var1!
  echo(!var2!
  echo(!var3!
)
Steffen

Re: Parenthetical Redirection

Posted: 21 Jul 2021 13:55
by atfon
Thanks Steffen (@aGerman). That's a big help. You guys on this forum are fantastic at what you do. Just reading through the posts has helped me a great deal.

Re: Parenthetical Redirection

Posted: 22 Jul 2021 06:40
by atfon
I have another small edge case to ask about. What if the variable contains a closing parentheses? I'll provide an example related to my previous code:

Code: Select all

for /f "tokens=*" %%i in ('powercfg /GetActiveScheme') do for /f "tokens=*" %%j in ("%%i") do set "pwrPlan=%%j"
(
echo Powerplan:
echo %pwrPlan%
echo(
)>%exportPath%
In my case, the variable contains this text: Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)

How can I get that to re-direct to file in a parethesized block?

Re: Parenthetical Redirection

Posted: 22 Jul 2021 08:43
by Squashman
You already had the answer in aGerman's post

Code: Select all

@echo off
for /f "tokens=*" %%i in ('powercfg /GetActiveScheme') do for /f "tokens=*" %%j in ("%%i") do set "pwrPlan=%%j"

(
setlocal enabledelayedexpansion
echo Powerplan:
echo !pwrPlan!
echo(
endlocal
)

Re: Parenthetical Redirection

Posted: 22 Jul 2021 09:12
by atfon
:oops: I'm embarrassed that I didn't even try delayed variable expansion with exclamation marks. That works just fine. I appreciate your assistance @Squashman.