tags:

views:

207

answers:

3

Hello guys I know this kind of problem will be very easy for you but I am total beginner in batch mode.

The question is how bat file code should look like to generate a new txt file from specific one .

for example .

I have report txt

Displaying status for license file: 7788@Server01

License Server: server01

                    License    In Use   Free
                    -------    ------   ----
                    Design*      1       6
                               (user1@host1)   127 server01 7788  4402

                  Assembly*      0       4

                       Pro       0      15

                     AdvSE       2       3
                              (user2@host2)  AdvSE server01 7788  2706
                              (user3@host3)  AdvSE server01 7788  1503

        SingleSite_License       1       3
                              (user4@host4)      SingleSite_License server01 7788  2003

      Intra_CLIENT_License        1       4
                              (user2@host2)      Intra_CLIENT_License server01 7788  2003

                       CAD       1      32
                              ^(user2@host2)     CAD server01 7788   501
* = License Extensions - Available only on startup.
^ = Borrowed License.
Press any key to continue . . . 

What I want to have in new file from this one are only lines :

SingleSite_License 3

Intra_CLIENT_License 9

but both SingleSite_License and Intra_Client_License should be taken from 1st found string -other are not necessary.

report txt can be different and licenses can be showed in different order .

if it not possible - other solution can be just only Free value will be written in new txt file - f.e 3 and 9 . so the last string in line which contains specific words

thank you for any tips

A: 

Write a C application. Batch is unnecessarily kludgy and probably can't even do this kind of stream manipulation.

Delan Azabani
HIYes you are right = should be 4 instead of 9 - and first of all thank you for this solution - it works perfectly . now the only problem which left me is to delay the execute of this command . Because at first first script creates report.txt and next filters it as you wrote . I don't know how to combine them because filter searches for file which doesn't exist yet - it needs about 3-5 s and I have no idea how to delay it - I tried "PING -n 4 127.0.0.1>nul " but it doesn't pause .
Antediluvian
+1  A: 

Not sure where you got the figure of 9 for the Intra_CLIENT_License (souldn't it be 4?), but this script will print out the free licences for those two product codes:

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "tokens=1,3" %%a in ('type infile.txt') do (
    if "x%%a"=="xSingleSite_License" (
        echo %%a %%b
    )
    if "x%%a"=="xIntra_CLIENT_License" (
        echo %%a %%b
    )
)
endlocal

I basically cut and pasted your transcript into the infile.txt file and ran this script to get:

SingleSite_License 3
Intra_CLIENT_License 4

(you can replace type infile.txt with whatever command you need to generate that output).


Breakdown:

The setlocal just sets up cmd.exe to allow extensions and delayed expansion of environment variables. All my scripts start with this since it's so useful.

The for loop basically processes one line at a time, grabbing tokens 1 and 3 (a and b) from each.

Then it's a simple matter of checking a for the required licence values and, if it matches, outputting that along with b.

paxdiablo
Is that plain batch?
Jens Björnhager
@Jens, Yes, it was done on XP so should work on all supported versions of Windows. Don't try to backport it to Windows 3.11 :-) Batch has come a long way since the brain-dead days of MSDOS 2.11 - it's still no match for UNIX shells but it's not _too_ bad.
paxdiablo
Well, a +1 from me then :)
Jens Björnhager
HI Yes you are right = should be 4 instead of 9 - and first of all thank you for this solution - it works perfectly . now the only problem which left me is to delay the execute of this command . Because at first first script creates report.txt and next filters it as you wrote . I don't know how to combine them because filter searches for file which doesn't exist yet - it needs about 3-5 s and I have no idea how to delay it - I tried "PING -n 4 127.0.0.1>nul " but it doesn't pause
Antediluvian
@Ante, that ping command _should_ wait 3 seconds. Leave off the `>nul` bit and tell me what the output is.
paxdiablo
OK everything seems to ok now . THANK YOU very much for this tip !! best regards
Antediluvian
A: 

Download UnxUtils and use the Unix stream processors. Might be possible to do with Powershell too.

Jens Björnhager