• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 626
  • Last Modified:

date batch file

I need a batch file that will get the current system date and then subtract one day.  Create a folder with this date as it's name.  Then copy files from a specfic location (like C:\data) to this dated folder.  Can this be done with a batch file and can some one show me the code needed?
1 Solution

http://www.dostips.com/DtTipsDateTime.php that is a starting point julien days -1
this should do part of the job, you'll have to modify last line to create the folder and copy files

@echo off

set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%

   set /a d-=1

   if %d% lss 1 (
      set d=31
      set /a m-=1
      if %m% lss 1 (
         set m=12
         set /a y-=1

xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto loop

echo %d%/%m%/%y%
set foldername=%y%%m%%d%
md %foldername%

Open in new window

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
using powershell

$foldername = [DateTime]::Now.Date.AddDays(-1).ToString('yyyyMMdd')
md "$foldername"
copy c:\Data\*.* -Recurse "c:\$foldername"

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Steve KnightIT ConsultancyCommented:
My article here shows hwo to get yesterdays date with a single line of VBScript embedded in a batch file.  The example at the bottom there gives you basically what you want:


If usign %date% to get the date and time it WILL cause some problems when your date format changes due to running as a different user or PC etc.

echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday was %yesterday%
md C:\archive\%yesterday%
xcopy /s c:\data\*.* C:\archive\%yesterday%

Bill PrewCommented:
Here's a pure batch solution. adjust the paths in the copy section as needed.  This assumes your date format is MM/DD/YYYY on the machine running the script.

@echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion

REM Break apart MM/DD/YYYY from day name
for %%A in (%DATE%) do set Today=%%A

REM Get yesterday's date
call :GetPriorDay "%Today%" "Yesterday"

REM Process files
if not exist "C:\Temp\%Yesterday%" md "C:\Temp\%Yesterday%"
copy "c:\Data\*.*" "C:\Temp\%Yesterday%"
exit /b

  REM Build array of days in each month
  set DaysInMonth=31 28 31 30 31 30 31 31 30 31 30 31

  REM Returns the date of the prior day in MM/DD/YYYY format
  set DateIn=%~1

  REM Input format is MM/DD/YYYY, break apart MM, DD, and YYYY pieces
  for /f "tokens=1-3 delims=/ " %%A in ("%~1") do (
    set Month=%%A
    set Day=%%B
    set Year=%%C

  REM Handle leading seros on Month and Day to avoid octal interpretation
  if "%Month:~0,1%"=="0" set Month=%Month:~1,1%
  if "%Day:~0,1%"=="0" set Day=%Day:~1,1%

  REM If Day not 1, then all we have to do is subtract 1 from the Day
  if %Day% NEQ 1 (
    set /a Day-=1
    goto :GetPriorDay90

  REM Day was 1, so see if we need to change years
  if %Month% EQU 1 (
    set /a Year-=1
    set Month=13

  REM Decrease Month by 1
  set /a Month-=1

  REM Get days in this new (prior) month
  for /f "tokens=%Month%" %%A in ("%DaysInMonth%") do set Day=%%A

  REM If leap year and in month 2, add 1 to day
  if %Month% EQU 2 (
    set /a Leap=%Year% %% 4
    if !Leap! EQU 0 set /a Day+=1

  REM Put pieces back into MM/DD/YYYY format
  if %Month% LSS 10 set Month=0%Month%
  if %Day% LSS 10 set Day=0%Day%
  set %~2=%Year%%Month%%Day%
  exit /b

Open in new window

Thanks for posting this online.  I used this code in a file naming convention that required today's date -1.

Bill PrewCommented:

Great, glad it was helpful.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now