1.

Solve : Anyone know how to change str to no.?

Answer»

OS: XP Professional
Prog: MS DOS Batch

Would NEED to convert a strings cropped from a file name to become a number,

Example:

Set A=0022.txt
Set /A Num=%A:~0,4%
Echo %Num%

Screen Output: 18

Will not work as it will display 18 instead of 22.

I have TRIED the below lousy method to convert it:

Set /A End4=%Num:~3,1%
Echo %End4%
Set /A End3=%Num:~2,1%
Echo %End3%
Set /A End2=%Num:~1,1%
Echo %End2%
Set /A End1=%Num:~0,1%
Echo %End1%

Set /A End4=End4*1
Set /A End3=End3*10
Set /A End2=End2*100
Set /A End1=End1*1000

Set /A Num=End4+End3+End2+End1
Set /A Num=Num+1

Echo %Num%

If %Num% LSS 10 (
Echo Yes 10
Set Num=000%Num:~-1%
Echo %Num%
Pause

And the output will still 3 instead of 03 which i need to use it to rename a file.

Any help?

You only need the /a switch when doing arithmetic:

Code: [SELECT]Set A=0022.txt
Set Num=%A:~0,4%
Echo %Num%

Good luck. 8-)Hi...Thanks for your reminder.

I have tried the below, but how can i increase the counter by 1?
The below method will not work as it give a wrong arithmetic RESULTS:
18 instead of 23... Hope you can suggest a way for it...

@Echo Off

Set A=0022.txt
Set Num=%A:~0,4%
Echo %Num%

Set /A Num=%Num%+1
Echo %Num%

PauseThe leading zeros are tricking the interpreter into using octal notation. (octal 22 = decimal 18) This snippet will solve your immediate problem. For a more generic solution you need to develop a loop to strip off any high order zeros.

Code: [Select]@echo off
Set A=0022.txt
Set Num=%A:~0,4%
set num=%num:0=%
Echo %num%
Set /A Num="Num+1"
Echo %Num%
Pause

Good luck. 8-)Hi, thanks a lot for your solutions.
Else i will be searching like mad for the reason.

If you do have any web link good for these details information do let me know ok?
I have been searching through quite abit but those are either i don't quite understand or not for MS XP dos.
Honest to say i pick up most of it through browsing through this forum for peoples question and answer.
I will post more code in future for people to CRITICIZE and learn as well. Will be making more good stuffs...

Thanks.



Discussion

No Comment Found