|
Answer» hi,
im working on a small batch file in which i get date using date /t and then use FOR to parse out the day, date, month and year and set variables respectively. I WANT to subtract 1 from the %date% variable. That way i get yesterday's date.
Could anyone please help
Thanks no batch for you, but VBSCRIPT: Code: [Select]WScript.Echo DateAdd("d",-1,Now) save it as subtractDate.vbs or whatever .vbs, then in batch Code: [Select]for /F "usebackq delims==" %%I IN (`cscript /nologo subtractDate.vbs`) DO (set VAR=%%I @echo %VAR% ) Why when usebackq is enabled is "delims==" instead of "delims=".
Sorry for hijacking, I'll post a batch file to do this, if still required?Ghostdog, will your script work if the day is 1?yes, if day is 1, dateadd will plus 1 day from the date now. just change -1 to 1.Ghostdog, I MEANT if the day in the month was '1'. Would it return '0' or 28/30/31?
Regardless, seeing I made it to experiment:
Ok, this TOOK me a while, I don't know if it works but try it. If Ghostdog's works on the first day of a month or the first month of a year then use that. Otherwise, try this.
NOTE: Where I live the date is DD/MM/YYYY. You may be different. To fix this simply swap the A and B in the FOR body, only if 'date /t' returns MM/DD/YYYY.
Unfortunately I haven't made it work on leap years if run on the first of March. The value will be 28, even though yesterday would have been the 29th. I will do that later, if necessary.
Code: [Select]@echo off
setlocal enabledelayedexpansion
for /F "usebackq tokens=2,3,4 delims=/ " %%A in (`date /t`) do ( set d=%%A set m=%%B set y=%%C goto day )
:day if not %d%==1 ( set /a d-=1 goto end ) else ( goto month )
:month if not %m%==1 ( set /a m-=1 if !m!==1 set /a d=31 if !m!==2 set /a d=28 if !m!==3 set /a d=31 if !m!==4 set /a d=30 if !m!==5 set /a d=31 if !m!==6 set /a d=30 if !m!==7 set /a d=31 if !m!==8 set /a d=31 if !m!==9 set /a d=30 if !m!==10 set /a d=31 if !m!==11 set /a d=30 if !m!==12 set /a d=31 goto end ) else ( goto year )
:year set /a y-=1 set /a m=12 set /a d=31 goto end
:end echo %d% %m% %y% pause If any experts could double check that, it would be appreciated.
Edit: I seriously hadn't actually tried that out, but I just did it and changed the variables around a bit and it appears to work. Also, forgot the %'s in month had to be !'s.Quote from: DeltaSlaya on July 24, 2007, 03:16:13 AM Ghostdog, I meant if the day in the month was '1'. Would it return '0' or 28/30/31?
dateadd takes care of that for you. You can just try by CHANGING your date in the computer and experiment with it. lol. That renders my script useless, it works though. If you have installed Logparser (and who hasnt!) try this 1 liner
logparser -o:NAT -q:on "Select Top 1 TO_STRING(SUB(SYSTEM_TIMESTAMP(), TIMESTAMP('01-02', 'MM-dd')), 'yyMMdd') as d1 Into 'yesterday.txt' From System" I haven't, mine still works though. Regardless of all the one-liner scripts vs my pure batch thing, can someone tell me if I used setlocal enabledelayedexpansion correctly, it's the first time I've used it. From what I understand it helps if you are trying to set something inside a IF body that is the same as the IF comparison?Thank you guys for your help. Quote from: DeltaSlaya on July 24, 2007, 05:20:13 AMcan someone tell me if I used setlocal enabledelayedexpansion correctly Looks fine to me.
|