romellem
romellem

Reputation: 6503

Trouble with FOR /F loops in Windows Batch Files

So I am trying to create a batch file that will do different things depending on the output of another program. Basically, the first program outputs some information on the encryption status of the hard drive. Here is the output when a drive is not encrypted

S:\>beinvvol.exe xlc
------------------------------------------------------------------------
BEInvVol, Utimaco Safeware AG - A member of the Sophos Group
Decommissioning Tool for fully SGN-encrypted Volumes
------------------------------------------------------------------------

&Info: SGN filter driver off
&Info: Target log-file "S:\BEInvVol.log"

------------------------------------------------------------------------
Listing info for the target volume(s) -command xlc
Date: 2011-07-26 13:31:01

[Volume C:]
Drive Type: DRIVE_FIXED
[Boot Sector]
Media Type: Plain -not SGN
Media Label:
Media Serial Number: {8C19-DCCD}
File System: NTFS
Size: 232.88 GB

And here is what you get when the drive is encrypted

S:\>beinvvol.exe xle
------------------------------------------------------------------------
BEInvVol, Utimaco Safeware AG - A member of the Sophos Group
Decommissioning Tool for fully SGN-encrypted Volumes
------------------------------------------------------------------------

&Info: SGN filter driver off
&Info: Target log-file "S:\KeyDestroy\BEInvVol.log"

------------------------------------------------------------------------
Listing info for the target volume(s) -command xle
Date: 2011-07-26 12:41:48

[Volume E:]
Drive Type: DRIVE_FIXED
[Boot Sector]
Media Type: SGN -fully encrypted
SGN Media GUID: {83490018-50C3-B4E3-829E-ACE3B23C86D3}
File System: NTFS
Size: 298.09 GB
[KSA Original]
Sector Offset ..... 0x0000000000065ED8 (417496)
Byte Offset ....... 0x000000000CBDB000 (213757952)
Size in Sectors ... 0x15 (21)
Size in Bytes ..... 0x2A00 (10752)
[KSA Backup]
Sector Offset ..... 0x000000002542D660 (625137248)
Byte Offset ....... 0x0000004A85ACC000 (320070270976)
Size in Sectors ... 0x15 (21)
Size in Bytes ..... 0x2A00 (10752)

The "xlc" and "xle" arguments are for choosing the drive. So, I see that in the un-encrypted drive, for "Media Type:" I have "Plain -not SGN" and for an encrypted driver I have "SGN -fully encrypted"

Now, I'm trying to write a FOR /F loop that when it sees "Plain -not SGN", it will go to a different part of the file. Here is my batch script so far:

@ECHO OFF
::Determine if drive is encrypted
FOR /F "tokens=3,4,5 delims= " %%A IN ('S:\beinvvol.exe xlc') DO IF /I "%%A%%B%%C"=="Plain-notSGN" GOTO NOTENCRYPTED

:ENCRYPTED
ECHO Yes
GOTO END

:NOTENCRYPTED
ECHO No

:END

Now, my thought process is, take the 3rd, 4th, and 5th tokens in the list (for the line that I'm interested in, 1st token will be "Media", 2nd is "Type:", and the 3rd, 4th, and 5th will either be "Plain" "-not" and "SGN", or "SGN" "-fully" "encrypted"), then see if those strings will equal "Plain-notSGN" (just concatenating all those strings). However, this does not work. Any suggestions?

Upvotes: 1

Views: 572

Answers (3)

Anthony Miller
Anthony Miller

Reputation: 15930

::Test the file passed in via command line
FOR /F "usebackq tokens=4" %%G IN (`S:\beinvvol.exe xlc ^| FIND "-not"`) DO (
  IF "%%G"=="-not" GOTO NOT_ENCRYPTED
)

:ENCRYPTED
ECHO Yes
GOTO END

:NOT_ENCRYPTED
ECHO No

:END

Upvotes: 1

Andriy M
Andriy M

Reputation: 77737

It is a really nice idea by @Mechaflash to involve a utility like FIND. But to use it together with the FOR loop is a bit overkill in this situation, I think.

Here's how you could use FIND without the loop:

beinvvol.exe xlc | FIND /I "Plain -not SGN" >NUL && (ECHO No) || (ECHO Yes)

The /I switch simply tells FIND to ignore the case while searching for the key phrase, the >NUL bit discards the output of FIND. The two operators, && and || control the flow of execution depending on the result of the last executed command, which is FIND in this case. The command after && is executed in case of the logical result of 'Success', and the one after || in case of logical 'Fail'.

Of course, you can use GOTO commands instead of ECHO.

beinvvol.exe xlc | FIND /I "Plain -not SGN" >NUL && (GOTO NotEncr) || (GOTO Encr)

:NotEncr
…

:Encr
…

Also you can use only && or only ||:

beinvvol.exe xlc | FIND /I "Plain -not SGN" >NUL && GOTO NotEncr
:: The following commands are executed in case of an encrypted drive
…

:NotEncr
…

Upvotes: 1

Joshua Drake
Joshua Drake

Reputation: 2746

Try:

@Echo off
::Test the file passed in via command line
FOR /F "tokens=3,4,5 skip=15" %%G IN (%1) DO IF Plain-notSGN == %%G%%H%%I GOTO NOT_ENCRYPTED

:ENCRYPTED
ECHO Yes
GOTO END

:NOT_ENCRYPTED
ECHO No

:END

You may need to first pipe or redirect the results of your utility into text files, but this does appear to successfully differentiate between the two results you posted.

Upvotes: 0

Related Questions