Parenthetical Redirection

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
atfon
Posts: 46
Joined: 06 Oct 2017 07:33

Parenthetical Redirection

#1 Post by atfon » 21 Jul 2021 13:01

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

Squashman
Expert
Posts: 4293
Joined: 23 Dec 2011 13:59

Re: Parenthetical Redirection

#2 Post by Squashman » 21 Jul 2021 13:13

Use this as a best practice.

Code: Select all

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

atfon
Posts: 46
Joined: 06 Oct 2017 07:33

Re: Parenthetical Redirection

#3 Post by atfon » 21 Jul 2021 13:23

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?

aGerman
Expert
Posts: 4198
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Parenthetical Redirection

#4 Post by aGerman » 21 Jul 2021 13:46

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

atfon
Posts: 46
Joined: 06 Oct 2017 07:33

Re: Parenthetical Redirection

#5 Post by atfon » 21 Jul 2021 13:55

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.

atfon
Posts: 46
Joined: 06 Oct 2017 07:33

Re: Parenthetical Redirection

#6 Post by atfon » 22 Jul 2021 06:40

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?

Squashman
Expert
Posts: 4293
Joined: 23 Dec 2011 13:59

Re: Parenthetical Redirection

#7 Post by Squashman » 22 Jul 2021 08:43

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
)

atfon
Posts: 46
Joined: 06 Oct 2017 07:33

Re: Parenthetical Redirection

#8 Post by atfon » 22 Jul 2021 09:12

: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.

Post Reply