create sequence number.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

create sequence number.

#1 Post by denday » 16 Dec 2011 02:44

hello,

I denday from Indonesia. I need your help.
I have a list of variables that contain more than 8000 lines of automatic variables created by other programs.

This list will be used by another program but must add an additional variable.

Additional variables are added at the end of the list and follow the previous sequence number.

Lets say the variable list contains 8250 lines and I have to add three additional variables again. How I can continue with the sequence number using the command

Code: Select all

echo "XXXX, variable_name, #,"> variable_list.csv


expectations of output in the file:
8251, variable_name1, #,
8252, variable_name2, #,
8253, variable_name3, #,

thanks a lot for your help.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: create sequence number.

#2 Post by orange_batch » 16 Dec 2011 12:24

If there are any lines such as comments, they can be dealt with, but following your example:

Code: Select all

@echo off&setlocal enabledelayedexpansion

:: Use only one of the following:

:: To grab the first value of the last line in variable_list.csv...
for /f "usebackq delims=," %%a in ("variable_list.csv") do set "counter=%%a"

:: Or to count the number of lines...
for /f "usebackq delims=" %%a in ("variable_list.csv") do set /a counter+=1



:: Use only one of the following:

:: Write data using a loop...

for %%x in ("variable_name1,#," "variable_name2,#," "variable_name3,#,") do (
for /f "delims=, tokens=1*" %%y in (%%x) do (
set /a counter+=1
echo(!"!!counter!, %%y, %%z,!"!>>"variable_list.csv"
))

:: Or simply...

set /a counter+=1
echo(!"!%counter%, variable_name1, #,!"!>>"variable_list.csv"
set /a counter+=1
echo(!"!%counter%, variable_name2, #,!"!>>"variable_list.csv"
set /a counter+=1
echo(!"!%counter%, variable_name3, #,!"!>>"variable_list.csv"

If you don't want to use delayed expansion, you can remove the setlocal... and use this in the loop instead:

Code: Select all

set /a counter+=1
call set "output=%%counter%%, %%y, %%z"
call call echo(%%%%"%%%%%%output%%%%%%"%%%%>>"variable_list.csv"

For the individual lines it would be...

Code: Select all

set /a counter+=1
call echo(%%"%%%counter%, variable_name1, #,%%"%%>>"variable_list.csv"

(I'm not too familiar with this !"! quotation technique without delayed expansion.)

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

Re: create sequence number.

#3 Post by denday » 18 Dec 2011 20:23

Thank you very much for the reply. will try this now..

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

Re: create sequence number.

#4 Post by denday » 19 Dec 2011 01:16

it's works..
any idea how to hide the results of the command "set /a counter +=1" being displayed?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: create sequence number.

#5 Post by orange_batch » 19 Dec 2011 04:18

It shouldn't display using @echo off, but set /a counter+=1 >nul would do.

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

Re: create sequence number.

#6 Post by denday » 19 Dec 2011 08:29

Code: Select all

@echo off
does not work for me, but

Code: Select all

set /a counter+=1 >nul
works.
thank you orange_batch, you are truly an expert. :D

Post Reply