XP Date batch for previous day

Posted on 2008-10-03
Last Modified: 2012-05-05
I currently have a batch file which gets the date and subtracts one day for file creation and directory creation but ran into the problem with the 1st day of month (1 - 1 = 0 of course) and its truncating the 01 of the day so instead of 03 for today's date it puts 3. Can someone help me with something that may work better, currently I'm using a set to grab portions of the date.
Question by:nasteal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 22639814
do you want to subtract 1 day from the given input date or you want to find yesterdays date from the current system date.

Author Comment

ID: 22640040
Well I want to create a file with the previous day's date.

Accepted Solution

devil_himself earned 500 total points
ID: 22640183
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations


Author Comment

ID: 22642875
What I have now is:

set YYMM=%date:~12,4%%date:~4,2%
set DD=%date:~7,2%
set /a PDD = %DD% - 1

Which gives me the problem described above.

Author Comment

ID: 22642986
That code will work however it sets


I am unfamiliar with tokens in the for, but I need YYMMDD and not sure how to strip off the 20 portion, I tried at the last line of code "if %yy% LSS 100 set yy=20%yy%", however removing it the date still shows 2008. Thanks for pointing out that bit of code, I just need to figure out the portion to remove the "20"
@echo off 
call :get_date 
:: Strip leading zeros from possible octals and decrement the day 
set /a mm=1%mm%-100, dd=1%dd%-101 
if %dd% NEQ 0 goto :add_zeros 
:: Today is the 1st of the month - decrement the month 
:: and set leap year check (ignoring centuries) 
set /a mm-=1,ly=yy%%4 
:: If today is 1 Jan, set date to 31st Dec 
if %mm% EQU 0 (set /a dd=31, mm=12, yy-=1) else ( 
  rem Calculate days in last month (by Frank Westlake) 
  set /a "dd=5546>>mm&1,dd+=30" 
  rem Special case for February 
  if %mm% EQU 2 if %ly% EQU 0 (set dd=29) else (set dd=28) 
if %dd% LSS 10 set dd=0%dd% 
if %mm% LSS 10 set mm=0%mm% 
echo yesterday was %yy%%mm%%dd% 
goto :eof 
:: ------------------------------------------------------------------ 
:: ------------------------------------------------------------------ 
:: Generic date parser 
:: Sets %dd% (01-31), %mm% (01-12) & %yy% (4 digit) 
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4) 
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo:^|date') do ( 
  for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do ( 
    set %%a=%%d 
    set %%b=%%e 
    set %%c=%%f 
    set toks= 
if %yy% LSS 100 set yy=20%yy% 
goto :eof 

Open in new window


Assisted Solution

devil_himself earned 500 total points
ID: 22643002
change line 20

echo yesterday was %yy%%mm%%dd%


echo yesterday was %yy:~2%%mm%%dd%

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question