Page 1 of 1

create sequence number.

Posted: 16 Dec 2011 02:44
by denday
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.

Re: create sequence number.

Posted: 16 Dec 2011 12:24
by orange_batch
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.)

Re: create sequence number.

Posted: 18 Dec 2011 20:23
by denday
Thank you very much for the reply. will try this now..

Re: create sequence number.

Posted: 19 Dec 2011 01:16
by denday
it's works..
any idea how to hide the results of the command "set /a counter +=1" being displayed?

Re: create sequence number.

Posted: 19 Dec 2011 04:18
by orange_batch
It shouldn't display using @echo off, but set /a counter+=1 >nul would do.

Re: create sequence number.

Posted: 19 Dec 2011 08:29
by denday

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