how to output multiple results from loop clause

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 output multiple results from loop clause

#1 Post by goodywp » 25 Jul 2018 13:38

Hi

This could be easy for you but I am not. I had a file as below
  • Location : TELIUM3\TSA
    Location : TELIUM3\KIA
    Location : TELIUM3\TCPXCOM
    Location : TELIUM3\TCPXCORE
    Location : TELIUM3\TCPXHMI
    Location : TELIUM3\TCPXVEGA
    Location : TELIUM3\E2EE_App
    Location : TELIUM3\Web Platform Lock Application
    Location : TELIUM3\Web Platform Lock Parameter
    Location : TELIUM3\E2EE_App_Package
    Location : TELIUM3\E2EE_App_Package
I would like to output only string after \, so I use the below code but it only output the last one, how can I output all the results instead of the last one

Code: Select all

@ECHO OFF

if exist signfder.txt (del signfder.txt)

for /f "tokens=1* delims=\" %%a in (need1sign.txt) do set "base=%%b"

echo %base%>>signfder.txt

I only got the last one E2EE_App_Package in this signfder.txt

Thanks for your help...

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

Re: how to output multiple results from loop clause

#2 Post by aGerman » 25 Jul 2018 14:07

You overwrite variable base multiple times in the loop and of course only the last value is left in the variable after the loop ended. What's the reason why you think you need an additional variable? You already have the values in %%b.

Code: Select all

@echo off
>"signfder.txt" (for /f "usebackq tokens=1* delims=\" %%a in ("need1sign.txt") do echo %%b)
... should be all that you need.

Steffen

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

Re: how to output multiple results from loop clause

#3 Post by goodywp » 25 Jul 2018 14:31

aGerman wrote:
25 Jul 2018 14:07
You overwrite variable base multiple times in the loop and of course only the last value is left in the variable after the loop ended. What's the reason why you think you need an additional variable? You already have the values in %%b.

Code: Select all

@echo off
>"signfder.txt" (for /f "usebackq tokens=1* delims=\" %%a in ("need1sign.txt") do echo %%b)
... should be all that you need.

Steffen
Ya that make sense!!! and works....
BTW, in another case, I got the string I want as above, but the first character of the string is space, can this first space also be eliminated from above?

Thanks

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

Re: how to output multiple results from loop clause

#4 Post by aGerman » 25 Jul 2018 14:37

goodywp wrote:
25 Jul 2018 14:31
BTW, in another case, I got the string I want as above, but the first character of the string is space, can this first space also be eliminated from above?
Certainly it can. But without knowing how the lines look like and what part of them you need I can't give any good advice.

Steffen

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

Re: how to output multiple results from loop clause

#5 Post by goodywp » 25 Jul 2018 15:00

aGerman wrote:
25 Jul 2018 14:37
goodywp wrote:
25 Jul 2018 14:31
BTW, in another case, I got the string I want as above, but the first character of the string is space, can this first space also be eliminated from above?
Certainly it can. But without knowing how the lines look like and what part of them you need I can't give any good advice.

Steffen
The file looks like this:
  • Signed file name : 8295010610_TSA.P3A
    Signed file name : 8295000530_KIA.P3A
    Signed file name : 8296363001_CPXCOMM.P3A
    Signed file name : 8296343001_CPXCORE.P3A
    Signed file name : 8520863001_CPXHMI.P3A
    Signed file name : 8296353001_CPXVEGA.P3A
    Signed file name : 8295522012_E2EE.P3A
    Signed file name : 1240000053010200_KIA_LOCK.P3A
    Signed file name : 1240000054010100_KIALCK_PARAM.P3P
    Signed file name : 8295910100_E2ECFGDD.P3P
    Signed file name : 8520300100_E2EBIN.P3P
So I can use the same code as this to get the string after :

Code: Select all

>"signfile.txt" (for /f "usebackq tokens=1* delims=:" %%a in ("need2sign.txt") do echo %%b)
I got as expected but the first character is space as original file has for each

8295010610_TSA.P3A
8295000530_KIA.P3A
8296363001_CPXCOMM.P3A
8296343001_CPXCORE.P3A
8520863001_CPXHMI.P3A
8296353001_CPXVEGA.P3A
8295522012_E2EE.P3A
1240000053010200_KIA_LOCK.P3A
1240000054010100_KIALCK_PARAM.P3P
8295910100_E2ECFGDD.P3P
8520300100_E2EBIN.P3P

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

Re: how to output multiple results from loop clause

#6 Post by aGerman » 25 Jul 2018 15:05

Just use the space as delimiter (which is the default) and output everything after the fourth token (which is the colon).

Code: Select all

>"signfile.txt" (for /f "usebackq tokens=4*" %%a in ("need2sign.txt") do echo %%b)
Steffen

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

Re: how to output multiple results from loop clause

#7 Post by goodywp » 26 Jul 2018 07:40

aGerman wrote:
25 Jul 2018 15:05
Just use the space as delimiter (which is the default) and output everything after the fourth token (which is the colon).

Code: Select all

>"signfile.txt" (for /f "usebackq tokens=4*" %%a in ("need2sign.txt") do echo %%b)
Steffen
Amazing! and good to learn from you case by case.... :)

Post Reply