How to do the condition in this case...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

How to do the condition in this case...

#1 Post by goodywp » 13 Dec 2017 13:59

Hi,
I have a text file, either in one line,but the string are separated by space or each line have one string....
say T501-08665-0102 T501-08665-0101 T501-08681-0100 T501-08658-0100
or
T501-08665-0102
T501-08665-0101
T501-08681-0100
T501-08658-0100

Now how can I write a set command based upon the string middle portion and last portion
I would like to have this output as
set vargen=T501-08665-0102
set vargen1=T501-08665-0101
set varsl=T501-08681-0100
set varfk=T501-08658-0100

Basically I do not want mess up the set since the first line and second line belong to same category because they shared the same middle portion as 08665, it might have 3 lines or more having same middle portion,say
T501-08665-0102
T501-08665-0101
T501-08665-0100
in this case,the output would be
set vargen=T501-08665-0102
set vargen1=T501-08665-0101
set vargen2=T501-08665-0100

Is any better way to achieve this issue??
Thanks

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

Re: How to do the condition in this case...

#2 Post by aGerman » 13 Dec 2017 14:37

You used variable names like vargen, varsl and varfk. Are they dependent on the values? And if so, what would happen if we find a value with a middle part other than those that you wrote in your example?

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to do the condition in this case...

#3 Post by goodywp » 13 Dec 2017 15:36

aGerman wrote:
13 Dec 2017 14:37
You used variable names like vargen, varsl and varfk. Are they dependent on the values? And if so, what would happen if we find a value with a middle part other than those that you wrote in your example?

Steffen
Yes! basically the middle part are related to profile like I posted in other post "How to freely select one column based on another column data? ". However, there are not many variations, around 4 or 5 values which defined the middle part. Probably we can hard-coded the profile related middle portion?

Thanks

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

Re: How to do the condition in this case...

#4 Post by aGerman » 13 Dec 2017 17:46

Sorry but I also didn't understand the requirements in your other thread. Maybe you should wait for Antonio who obviously did.

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to do the condition in this case...

#5 Post by goodywp » 14 Dec 2017 09:24

aGerman wrote:
13 Dec 2017 17:46
Sorry but I also didn't understand the requirements in your other thread. Maybe you should wait for Antonio who obviously did.

Steffen
Thanks Steffen!
I have an alternative way. Actually I have a separate txt file which only hold vargen before I combined them together
say
T501-08665-0102
T501-08665-0101

How can I make set command based upon this txt file
if only one line
T501-08665-0102
then set vargen=T501-08665-0102

if two lines
T501-08665-0102
T501-08665-0101

then set vargen=T501-08665-0102
set vargen1=T501-08665-0101
........

How can we make it?

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

Re: How to do the condition in this case...

#6 Post by aGerman » 14 Dec 2017 19:28

Try that code

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "txtfile=vargen.txt"

set "n="
for /f %%i in ('type "%txtfile%"') do (
  set "vargen!n!=%%i"
  set /a "n+=1"
)

set vargen
pause
Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to do the condition in this case...

#7 Post by goodywp » 15 Dec 2017 12:03

aGerman wrote:
14 Dec 2017 19:28
Try that code

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "txtfile=vargen.txt"

set "n="
for /f %%i in ('type "%txtfile%"') do (
  set "vargen!n!=%%i"
  set /a "n+=1"
)

set vargen
pause
Steffen
Thanks Steffen!! it works!

How to output into a txt file?? :oops:

my desired output should looks like

set vargen=T501-08665-0102
set vargen1=T501-08665-0101

I tried as this only got

Code: Select all

@echo off &setlocal EnableDelayedExpansion

set "txtfile=gen_sch_pack_need.txt"
(
set "n="
for /f %%i in ('type "%txtfile%"') do (
  set "vargen!n!=%%i"
  set /a "n+=1"
)
)
echo set %vargen% >>vargen.txt
got
set T501-08665-0102 :oops:

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

Re: How to do the condition in this case...

#8 Post by aGerman » 15 Dec 2017 12:19

Using ECHO redirection as always :wink:

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "txtfile=gen_sch_pack_need.txt"
set "outfile=vargen.txt"

set "n="
>"%outfile%" (
  for /f %%i in ('type "%txtfile%"') do (
    echo set vargen!n!=%%i
    set /a "n+=1"
  )
)

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to do the condition in this case...

#9 Post by goodywp » 15 Dec 2017 12:47

aGerman wrote:
15 Dec 2017 12:19
Using ECHO redirection as always :wink:

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "txtfile=gen_sch_pack_need.txt"
set "outfile=vargen.txt"

set "n="
>"%outfile%" (
  for /f %%i in ('type "%txtfile%"') do (
    echo set vargen!n!=%%i
    set /a "n+=1"
  )
)
8)

Post Reply