generate the output file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
denday
Posts: 10
Joined: 16 Dec 2011 02:19

generate the output file

#1 Post by denday » 10 Jan 2012 02:53

hello,

I need help again.
I have a list of variables as follows:

Code: Select all

1,10-A-1DI
2,20-A-1DI
3,10-B-1DI
4,20-B-1DI
5,10-C-1DI
6,20-C-1DI
7,NULL
8,10-A-2DI
9,20-A-2DI
10,10-B-2DI
11,20-B-2DI
12,10-C-2DI
13,20-C-2DI

I hope the result('result.csv') is like this: :roll:

Code: Select all

Address,Variable,OK,NOK,GROUP,END
201,10-A-1DI,10 A Group 1DI Ok,10 A Group 1DI Nok,DI,#
202,20-A-1DI,20 A Group 1DI Ok,20 A Group 1DI Nok,DI,#
203,10-B-1DI,10 B Group 1DI Ok,10 B Group 1DI Nok,DI,#
204,20-B-1DI,20 B Group 1DI Ok,20 B Group 1DI Nok,DI,#
205,10-C-1DI,10 C Group 1DI Ok,10 C Group 1DI Nok,DI,#
206,20-C-1DI,20 C Group 1DI Ok,20 C Group 1DI Nok,DI,#
207,NULL,,,,#
208,10-A-2DI,10 A Group 2DI Ok,10 A Group 2DI Nok,DI,#
209,20-A-2DI,20 A Group 2DI Ok,20 A Group 2DI Nok,DI,#
210,10-B-2DI,10 B Group 2DI Ok,10 B Group 2DI Nok,DI,#
211,20-B-2DI,20 B Group 2DI Ok,20 B Group 2DI Nok,DI,#
212,10-C-2DI,10 C Group 2DI Ok,10 C Group 2DI Nok,DI,#
213,20-C-2DI,20 C Group 2DI Ok,20 C Group 2DI Nok,DI,#
214,,,,,#
215,,,,,#
216,,,,,#

sequence number is 8. so the number should always be a multiple of 8. if there are 13 variables then rounded to 16. if there are 17 variables will be rounded to 24. :wink:

thanks a lot for your help. :D

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: generate the output file

#2 Post by dbenham » 10 Jan 2012 07:16

Not sure what you mean by a list of variables. As presented it looks like you have a file.

Assuming the leading number from each line (variable?) is always contiguous starting from 1, then:

Code: Select all

@echo off
setlocal enableDelayedExpansion
set inFile="test.txt"
set outFile="result.csv"
set cnt=0
set n=200
(
  for /f "usebackq tokens=1-4 delims=,-" %%A in (%inFile%) do (
    set /a "n+=1, cnt+=1"
    if "%%C" == "" (echo(!n!,%%B,,,,#) else (
      set "end=%%D"
      set "end=!end:~-2!
      echo(!n!,%%B-%%C-%%D,%%B %%C Group %%D Ok,%%B-%%C-%%D,%%B %%C Group %%D Nok,!end!,#
    )
  )
  set /a "cnt=cnt%%8"
  for /l %%N in (!cnt! 1 7) do (
    set /a "n+=1"
    echo(!n!,,,,,#
  )
)>%outFile%


Dave Benham

denday
Posts: 10
Joined: 16 Dec 2011 02:19

Re: generate the output file

#3 Post by denday » 10 Jan 2012 22:01

thank you Dave for the answer. :D

this list is a digital input, when the value is '1 'it will be OK and when the value is '0' it will be NOK.

I tried the program and succeed, but then there are problems when there is a variable that consists of only 1 or 2 parts and more than 3 parts. :cry:

suppose I have a list of variables as follows:

Code: Select all

1,10-A-X-1DI
2,20-B-Y-2DI
3,
4,10-A-1DI
5,20-B-2DI
6,
7,10-1DI
8,20-2DI
9,TEMP
10,NULL
11,START
12,STOP

results I would expect something like this:

Code: Select all

201,10-A-X-1DI,10 A X Group 1DI OK,10 A X Group 1DI NOK,DI,#
202,20-B-Y-2DI,20 B Y Group 2DI OK,20 B Y Group 2DI NOK,DI,#
203,,,,,#
204,10-A-1DI,10 A Group 1DI OK,10 A Group 1DI NOK,DI,#
205,20-B-2DI,20 B Group 2DI OK,20 B Group 2DI NOK,DI,#
206,,,,,#
207,10-1DI,10 Group 1DI OK,10 Group 1DI NOK,DI,#
208,20-2DI,20 Group 2DI OK,20 Group 2DI NOK,DI,#
209,TEMP,,,,#
210,NULL,,,,#
211,START,Program START OK,Program START NOK,DI,#
212,STOP,Program STOP OK,Program STOP NOK,DI,#
213,,,,,#
214,,,,,#
215,,,,,#
216,,,,,#

I tried to modify your program but did not succeed :roll:
I need help again.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: generate the output file

#4 Post by dbenham » 10 Jan 2012 23:39

Why don't you show what you have tried? I hope you didn't give up after just one attempt.

Also, you never state your requirements. Just showing a limited set of input and the expected output is not enough. You need to better define what types of input can be expected, and what are the rules for transformation. And provide the complete requirements up front.

Dave Benham

denday
Posts: 10
Joined: 16 Dec 2011 02:19

Re: generate the output file

#5 Post by denday » 11 Jan 2012 01:37

batch program that I need is to take the word of each segment and write it back into the csv file format as you have guessed.

The following program I have tried and succeeded, which at first I had problems with parentheses. :roll:

Code: Select all

@echo off
setlocal enableDelayedExpansion
set inFile="test.txt"
set outFile="result.csv"
set cnt=0
set n=200
(
  echo(address,name,ok,nok,group,end
  for /f "usebackq tokens=1-5 delims=,-" %%A in (%inFile%) do (
    set /a "n+=1, cnt+=1"

:: handle 4 segment variables
if "%%E" == "1DI" if "%%D" == "X" (echo(!n!,%%B-%%C-%%D-%%E,%%B %%C %%D Group %%E OK,%%B %%C %%D Group %%E NOK,DI,#) else (echo(!n!,%%B-%%C-%%D-%%E,,,,#)
if "%%E" == "2DI" if "%%D" == "Y" (echo(!n!,%%B-%%C-%%D-%%E,%%B %%C %%D Group %%E OK,%%B %%C %%D Group %%E NOK,DI,#) else (echo(!n!,%%B-%%C-%%D-%%E,,,,#)

:: handle 3 segment variables
if "%%D" == "1DI" if "%%C" == "A" (echo(!n!,%%B-%%C-%%D,%%B %%C Group %%D OK,%%B %%C Group %%D NOK,DI,#) else (echo(!n!,%%B-%%C-%%D,,,,#)
if "%%D" == "2DI" if "%%C" == "B" (echo(!n!,%%B-%%C-%%D,%%B %%C Group %%D OK,%%B %%C Group %%D NOK,DI,#) else (echo(!n!,%%B-%%C-%%D,,,,#)

:: handle 2 segment variables
if "%%C" == "1DI" if "%%D" == "" (echo(!n!,%%B-%%C,%%B Group %%C OK,%%B Group %%C NOK,DI,#) else (echo(!n!,%%B-%%C,,,,#)
if "%%C" == "2DI" if "%%D" == "" (echo(!n!,%%B-%%C,%%B Group %%C OK,%%B Group %%C NOK,DI,#) else (echo(!n!,%%B-%%C,,,,#)

:: handle START & STOP variables
if "%%B" == "START" if "%%C" == "" (echo(!n!,%%B,Program START OK,Program START NOK,DI,#) else (echo(!n!,%%B,,,,#)
if "%%B" == "STOP" if "%%C" == "" (echo(!n!,%%B,Program STOP OK,Program STOP NOK,DI,#) else (echo(!n!,%%B,,,,#)

:: handle other variables
if not "%%B" == "START" if not "%%B" == "STOP" if "%%C" == "" (echo(!n!,%%B,,,,#)

  )
  set /a "cnt=cnt%%8"
  for /l %%N in (!cnt! 1 7) do (
    set /a "n+=1"
    echo(!n!,,,,,#
  )
)>%outFile%

thank you Dave for the sample program.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: generate the output file

#6 Post by dbenham » 11 Jan 2012 06:55

Good job. I'm glad you got everything workinig :D

One helpful tip -

Your use of :: as a comment within a parenthesized block () is not advisable. Your code is fine, but there are situations where :: within () will give a syntax error. There are rules as to when it can and can't be used, but it is probably better just to avoid :: within (). In all other situations not within (), :: is a great choice.

The official reliable option to use within () is REM.

There is another option that EdDyreen initiated:

Code: Select all

%= This is a comment =%

It should work in any situation as long as the comment itself does not contain %.

It works because the parser attempts to expand the "comment" as a variable. But there can never be a valid expansion for anything with two =. The expansion of an undefined variable within a batch context results in an empty string.


Dave Benham

denday
Posts: 10
Joined: 16 Dec 2011 02:19

Re: generate the output file

#7 Post by denday » 11 Jan 2012 20:40

thank you Dave for your helpful tip

please do not give up helping me. :D

have a nice day.
-denDay

Post Reply