Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to do a command that takes yesterday date and builds it into a file path like this:

copy "Something 2010-12-06\*.zip" "d:\Copy\"

where 2010-12-6 is yesterday's date in format yyyy-mm-dd.

Based on link in answer, I found this solution, which is close to what I want, but not quite:

For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set cdate=%%c-%%b-%%a)
echo "Backup Set*\Backup Files %cdate% *\*.zip" 

This give me today's date, How can I get yesterday's date?

share|improve this question
This only works if the regional settings display the date as: dd/mm/yyyy – Hydaral Jul 12 '11 at 1:06
up vote 1 down vote accepted

This is really messy at the moment, but it does work. I'll try naming things properly and tidying it up later tonight, unless anybody else wants to edit my answer.

@echo off
call :GetDate year month day
echo/Today is: %year%-%month%-%day%
goto :part2

:GetDate yy mm dd
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
  for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
    set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :eof

call :DateToDays %year% %month% %day% days
echo/It has been %days% days since 1970-01-01
goto :part3

:DateToDays %yy% %mm% %dd% days

set yy=%1&set mm=%2&set dd=%3
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set %4=%j%&goto :eof

set /a yesterday=%days%-1

call :DaysToDate %yesterday% yy mm dd
echo/Yesterday was %yy%-%mm%-%dd%
goto :copy

:DaysToDate %days% yy mm dd
set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto :eof

copy "Something %yy%-%mm%-%dd%\*.zip" "d:\Copy\"


Source of the script snippets I used.

share|improve this answer
Well, it's messy, but it works... – awe Jun 19 '12 at 11:10

This post deals with getting the date in whatever format you like into a variable:

subtraction and variable usage is here:

that should about do it...

share|improve this answer
How can I get yesterday's date? – awe Dec 7 '10 at 11:47
today's date - 1 – RobotHumans Dec 7 '10 at 11:47
the /a is important i think...directly from the second link: set /a counter=%counter%+1 would lead me towards set /a counter = %a%-1 and reassigning cdate. they talk about it in the arithmetic section – RobotHumans Dec 7 '10 at 11:51
That works if I should just decrement the day component, but what if today is the first in the month? – awe Dec 7 '10 at 11:56
needs to be some if then logic....i would look at a calendar app to get an idea of how others have determined what months have how many days. – RobotHumans Dec 7 '10 at 11:58

My date format looks like this:

E:\>date /t
Wed 10/17/2012

This works for my format:

E:\>echo %date:~10,4%-%date:~4,2%-%date:~7,2%
share|improve this answer
The issue is not formatting the date value. It is getting yesterday's date. – awe Feb 8 '13 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .