0

The Scenario:

I am trying to reformat an output of an application (Wpkg, command wpkg.js /query:a). I am limited to standard commandline tools.

The command output consists of repetitive blocks of software package title and it's parameters, ie.:

Mozilla Firefox
    ID:                mozilla-firefox
    Revision:          23.0.1.1
    Reboot:            false
    Execute:           -
    Priority:          100
    Status:            Installed


Mozilla Thunderbird
    ID:                mozilla-thunderbird
    Revision:          17.0.8.1
    Reboot:            false
    Execute:           -
    Priority:          100
    Status:            Installed

This makes a long output that is hard to read. I also do not need all the information.

Therefore I am trying, for each block, to collect interesting information in one variable which I would then output in one line, getting result like this:

Mozilla Firefox             mozilla-firefox          23.0.1.1     Installed
Mozilla Thunderbird         mozilla-thunderbird      17.0.8.1     Installed

The Problem:

To achieve that, I began first to parse the output using following code:

rem Split all lines into two tokens

@for /f "tokens=1,2" %%G IN ('%WPKG_CMD% /query:a') DO @(

rem With first token, check what to do:
rem I can get known strings (Store, Ignore)
rem or I can get package name (NewLine)

@set WpkgListAll_ToDo=NewLine

@if [%%G]==[ID:]        set WpkgListAll_ToDo=Store
@if [%%G]==[Revision:]  set WpkgListAll_ToDo=Store
@if [%%G]==[Reboot:]    set WpkgListAll_ToDo=Ignore
@if [%%G]==[Execute:]   set WpkgListAll_ToDo=Ignore
@if [%%G]==[Priority:]  set WpkgListAll_ToDo=Ignore
@if [%%G]==[Status:]    set WpkgListAll_ToDo=Store

rem Echo for debug purpose

@echo [%%G][%WpkgListAll_ToDo%]

rem Next follows some unimportant code

)

From that code above I would totally expect a following debug output:

[Mozilla][NewLine]
[Revision:][Store]
[Reboot:][Ignore]
[Execute:][Ignore]
[Priority:][Ignore]
[Status:][Store]
[Mozilla][NewLine]
[ID:][Store]
[Revision:][Store]
[Reboot:][Ignore]
[Execute:][Ignore]
[Priority:][Ignore]
[Status:][Store]

What I get though is...:

[Mozilla][]
[Revision:][]
[Reboot:][]
[Execute:][]
[Priority:][]
[Status:][]
[Mozilla][]
[ID:][]
[Revision:][]
[Reboot:][]
[Execute:][]
[Priority:][]
[Status:][]

...and I've been trying hard to understand why, but I failed.

The Question:

Can anyone explain me why the output fails me and what should I do to get it right?

1
  • Edited tags - This is a Windows batch file, not MS-DOS. MS-DOS does not support the FOR /F syntax. Commented Aug 22, 2013 at 20:15

1 Answer 1

3

You have to use delayed variable expansion. First, add setlocal enabledelayedexpansion line at the beginning and endlocal at the end of the script. Then change the echo statement to:

@echo [%%G][!WpkgListAll_ToDo!]`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.