Solved

date batch file

Posted on 2011-03-10
7
557 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
7 Comments
 
LVL 30

Expert Comment

by:IanTh
Comment Utility
see

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

Expert Comment

by:khaaz
Comment Utility
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 18

Expert Comment

by:Raheman M. Abdul
Comment Utility
using powershell

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

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Thanks for posting this online.  I used this code in a file naming convention that required today's date -1.

-byd2k
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
@byd2k

Great, glad it was helpful.

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the features I've come to appreciate about Windows 7 and Windows Server 2008 R2 is the ability to pin applications to the task bar. As useful a feature as I've found this, it does have some quirks.  For example, have you ever tried pinning an…
Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now