dealing with octal variables in batch script

后端 未结 1 1186
面向向阳花
面向向阳花 2020-12-21 15:01

Hi I am running a batch script to get a current date and perform numerical operations on it.

I get the date by using the following command and then do operations on

相关标签:
1条回答
  • 2020-12-21 15:26

    you can remove the zero with a little trick:

    set /a dd1=(1%dd: =0%-100)-1
    

    This adds the string "1" to "08" (which is also still a string), resulting in "108". Then subtract "100" (/a treating them as numbers), which results in "8". If in your locale the date has no leading zero but a space instead, %%d: =0% replaces it with a zero

    If you need the result with leading zero, just add it again:

    set dd1=0%dd1%
    set dd1=%dd1:~-2%
    

    This adds the string "0" in front of the string "7" (result from before), resulting in "07" and takes the last two digits from it "07" (in case, the result from before is "24", -> add "0" = "024" -> last two = "24")

    edited to work also in locales, where the date has a space instead of a leading zero. (thanks to Lưu Vĩnh Phúc for spotting it)

    0 讨论(0)
提交回复
热议问题