Convert string to number

I need help with the following:

I have the following script:

SETLOCAL DisableDelayedExpansion

for /f "delims=" %%a in (stores1.txt) do (

set /p stores=%%a )

      

store.txt has the following content:

0010
0011
0012
etc.

      

The solution I found is set /p stores=%%a

This output: 0010,0011,0012

should be passed as a parameter to sql.script

:

sqlplus user/password@%database% @sqlscript.sql !stores! !data!

      

stores must be a numeric value because the .sql script is asking for a numeric value

So the conclusion is that I want a numeric value, not a string. How can I do this "transformation"?

@echo off
setlocal enabledelayedexpansion
set stores=
PAUSE
for /f "delims=" %%a in (stores1.txt) do (
set /a stores=%%a
echo !stores!
)
pause

      

+3


source to share


2 answers


Basically, @Bali C is correct. In most cases, any value is a string value in cmd

unless the context requires or suggests that it is handled differently. For example, in an arithmetic expression, values ​​are automatically converted to / as numbers.

However, numbers in batch scripts can be handled in more than one way. In particular, depending on which character the number begins with, it can be viewed as

  • hexadecimal value (if prefixed 0x

    );

  • octal value (when it starts with 0

    );

  • decimal value (in all other cases).

So, if you just used, for example, 0010

in a context where a number was expected, the value will evaluate as 8

, because 0010

is the number in octal notation before cmd

, and 8

is its decimal equivalent. What would you get, in particular, if you just did this:

set /a stores=%%a

      



You can use the following simple trick to get rid of the leading 0s (and thus avoid the value being treated as octal):

set /a "stores=1%%a %% 10000"

      

Entering 1

at the beginning turns it into a 5-digit number and decimal notation number. Finding the remainder of division by 10000 only gives you the original number, but now with the leading 0 removed.

(Note: the actual modulo operator %

, not %%

, but the character must be doubled due to its special meaning besides the arithmetic context.)

+4


source


I don't think cmd treats numbers differently for strings unless you do arithmetic with them.

I'm not sure if this will make a difference, but you can assign a variable to a number with set /a

, so try using



set /a stores=%%a

      

0


source







All Articles