0

I have a bat file which simply generates a date

for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set CurrYear=%%c

for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set CurrMoth=%%a

for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set CurrDay=%%b


set bDate =01/%CurrMoth%/%CurrYear%

set eDate=%CurrDay%/%CurrMoth%/%CurrYear%

set /a dayminus = %CurrDay% - 1

echo %dayminus%

pause

The answer I get for echo %dayminus% is 6 instead of 06 & that's my problem.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Irfan Memon
  • 3
  • 1
  • 2

4 Answers4

0

(Interestingly, most people want to know how to go the other way.) The reason this is happening is because batch considers numbers that begin with a 0 to be octal and then simplifies them to drop the 0 when printing as an integer. You can get around this by treating the variable like a string instead.

set /a dayminus=%CurrDay%-1
if %dayminus% lss 10 (
    set dayminus=0%dayminus%
)
echo %dayminus%
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • thanks but yes you are correct, now i seek an answer on how to take care when the day is 1st of every month. – Irfan Memon Aug 07 '15 at 04:02
  • @IrfanMemon - Feel free to ask another question after picking an answer (click the check mark next to the answer you want to mark as accepted), but the solution will likely involve a dozen `if` statements. – SomethingDark Aug 07 '15 at 04:08
0

If your intent is to simply ensure a number is left-zero-padded to two places, you can just use:

if %dayminus% lss 10 set dayminus=0%dayminus%

However, you'll soon run into another problem, specifically what you do when you run this on the first of the month. But that's probably a matter for a separate question (or you could just look at this one).

If you're open to using other tools (which ship with Windows, just like cmd.exe does), then VBScript is possibly the easiest option. You can just create a one-liner yesterdom.vbs:

wscript.echo day(date()-1)

and then call it from your script:

for /f %%a in ('cscript //nologo yesterdom.vbs') do set dayminus=%%a

If you don't want the hassle of maintaining the one-liner, you can create and destroy it from within your script, such as with:

@echo off
echo wscript.echo day(date()-1) >yesterdom.vbs
for /f %%a in ('cscript //nologo yesterdom.vbs') do set dayminus=%%a
del /q yesterdom.vbs >nul: 2>nul:
echo %dayminus%
Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • thanks but yes you are correct, now i seek an answer on how to take care when the day is 1st of every month. – Irfan Memon Aug 07 '15 at 04:02
  • @IrfanMemon, follow the link I gave, then you can get it from yesterday's date. That's if you're strictly limited to `cmd.exe`. If you're allowed to use other standard Windows tools, I've added some VBScript which will make it much easier. – paxdiablo Aug 07 '15 at 04:56
0

Here is a quick workaround that I have prepared for you in a sample program:

@echo off
set /a test = 4
IF %test% lss 10 (
@echo 0%test%
)
@pause

This will always get you a two digit number since anything below 10 will have the added 0 in front of it when it's being written to the screen.

Brad
  • 359
  • 5
  • 21
0

This is a pure Batch file solution that get the date of yesterday; it also works for the current date minus any number of days less than a month:

@echo off
setlocal EnableDelayedExpansion

rem Create array of days per month; both month and day are 100-based
set i=100
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
   set /A i+=1
   set "daysPerMonth[!i!]=1%%a"
)

rem Get today's date parts (from "Dow MM/DD/YYYY" format) - 1 day
for /F "tokens=2-4 delims=/ " %%a in ('date /T') do (
   set /A MM=1%%a, DD=1%%b-1, YYYY=%%c, YYYYmod4=YYYY %% 4
)
if %YYYYmod4% equ 0 set "daysPerMonth[102]=129"

rem Adjust date if previous month
if %DD% equ 100 (
   set /A MM-=1
   if !MM! equ 100 (
      set /A MM=112, YYYY-=1
   )
   set /A DD=daysPerMonth[!MM!]
)

set "dateMinus=%DD:~1%/%MM:~1%/%YYYY%
echo %dateMinus%
Aacini
  • 65,180
  • 12
  • 72
  • 108