how to do this for the last line without comma?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

how to do this for the last line without comma?

#1 Post by goodywp » 09 Apr 2018 15:08

I have a task as below
The code below shall give me all lines with comma, I added , after !line:~,8!

Code: Select all

for /f "delims=" %%i in (debug_list.txt) do (
set line=%%i 
echo !line:~,8!,>>debug_extra.txt
)
the expected result is as below:
  • 50001101,
    50007201,
    50013501,
    50014301,
    50013601,
And the debug_list.txt looks like
  • 500011011000.S3S
    500072010400.S3S
    500135010100.S3S
    500143010002.S3S
    500136010100.S3S
The desired output debug_extra.txt should looks like
  • 50001101,
    50007201,
    50013501,
    50014301,
    50013601
Only last line without comma, all the other lines should have comma for the need of format...
How not to add the comma for the last line...
Thanks

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: how to do this for the last line without comma?

#2 Post by Squashman » 09 Apr 2018 15:34

You put the current line into a variable and don't write it out until the next line comes in. After the FOR command you write out the last line without the comma.

Code: Select all

set "line="  
for /f "delims=" %%i in (debug_list.txt) do (
	IF DEFINED line echo !line:~,8!,>>debug_extra.txt
	set "line=%%i" 
)
echo %line:~,8%>>debug_extra.txt

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

Re: how to do this for the last line without comma?

#3 Post by goodywp » 10 Apr 2018 07:45

Squashman wrote:
09 Apr 2018 15:34
You put the current line into a variable and don't write it out until the next line comes in. After the FOR command you write out the last line without the comma.

Code: Select all

set "line="  
for /f "delims=" %%i in (debug_list.txt) do (
	IF DEFINED line echo !line:~,8!,>>debug_extra.txt
	set "line=%%i" 
)
echo %line:~,8%>>debug_extra.txt
Thanks soooooo much!!

Post Reply