Solved

date batch file

Posted on 2011-03-10
7
586 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 53

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 53

Expert Comment

by:Bill Prew
ID: 36577529
@byd2k

Great, glad it was helpful.

~bp
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
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.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

790 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