Solved

date batch file

Posted on 2011-03-10
7
602 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:sambec22
[X]
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
7 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 35095681
see

http://www.dostips.com/DtTipsDateTime.php that is a starting point julien days -1
0
 
LVL 2

Expert Comment

by:khaaz
ID: 35095783
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%


:loop
   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

0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 35095970
using powershell

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Steve Knight
ID: 35096403
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:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

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%


Steve
0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35099346
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

:GetPriorDay
  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
  )

:GetPriorDay90
  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

~bp
0
 

Expert Comment

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

-byd2k
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 36577529
@byd2k

Great, glad it was helpful.

~bp
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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