Solved

date batch file

Posted on 2011-03-10
7
594 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 54

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 54

Expert Comment

by:Bill Prew
ID: 36577529
@byd2k

Great, glad it was helpful.

~bp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

679 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