# Solved: Convert a string to a number with a batch file



## nidhal

Hi, 
I'm reading string from a CSV file and I want to convert this string to an Integer (all the strings in the file are numbers' format). I want to write this using batch file. 
this a sample of my code : 
for /F "tokens=1,2 delims=," %%A in (File1.csv) do ( 
for /F "tokens=1,2 delims=," %%X in (File2.csv) do ( 
if %%A == %%X ( 
set /a R=%%B + %%Y 
echo %%A,%%B,%%Y,%R%>>Results.csv ) 
) 
) 
My issue here is that R is all the time empty (I guess I can't add 2 strings). Is there any solution to convert a string to integer in Batch language to be able to do arithmetic operations?
I need help. 
Thanks a lot.


----------



## Squashman

Need to see what File1.csv and File2.csv look like. Could you post the first couple of lines from each file. And please surround your code in code TAGS so it looks like this.


Code:


for /F "tokens=1,2 delims=," %%A in (File1.csv) do ( 
for /F "tokens=1,2 delims=," %%X in (File2.csv) do ( 
if %%A == %%X ( 
set /a R=%%B + %%Y 
echo %%A,%%B,%%Y,%R%>>Results.csv ) 
) 
)


----------



## nidhal

File1.csv:


> Seed_Week/Class_View,13231
> Seed_Week/Class_Filter,9298
> Seed_Week/Class_PivotPanel,6052
> Seed_Week/Class_Level,418
> Seed_Location_View,10249


File2.csv:


> Seed_Week/Class_View,10295
> Seed_Week/Class_Filter,2860
> Seed_Week/Class_PivotPanel,2507
> Seed_Week/Class_Level,4138
> Seed_Location_View,7397


So I'm comparing the first column between both files, and I want to do some arithmetic operations for the second column.


----------



## Squashman

You need to use DelayedExpansion and then surround your result variable in exclamations.



Code:


@Echo Off & SetLocal EnableExtensions EnableDelayedExpansion
for /F "tokens=1,2 delims=," %%A in (File1.csv) do ( 
	for /F "tokens=1,2 delims=," %%X in (File2.csv) do ( 
		if %%A == %%X ( 
			set /a R=%%B + %%Y 
			echo %%A,%%B,%%Y,!R!>>Results.csv
		)
	) 
)


----------



## nidhal

Thanks a lot Squashman it's working fine.
I have one more question related to arithmetic operations, I want to do a division and the result must be a float (3,502 for example) but when I use "/" I got only integer : 


> if %%A == %%X (
> set /a S=%%B - %%Y
> set /a R=!S! / %%Y
> echo %%A,%%B,%%Y,!R!>>Results.csv
> )


Any idea on how to resolve it ?


----------



## Squashman

Batch files cannot natively do floating point math.


----------



## Squashman

You can kind of work around it if you call a Vbscript on the fly and have it do the math for you.
http://stackoverflow.com/questions/1503888/floating-point-division-in-a-dos-batch


----------



## nidhal

Thanks a lot it's really helpful.


----------

