Solved

XP Date batch for previous day

Posted on 2008-10-03
6
1,137 Views
Last Modified: 2012-05-05
I currently have a batch file which gets the date and subtracts one day for file creation and directory creation but ran into the problem with the 1st day of month (1 - 1 = 0 of course) and its truncating the 01 of the day so instead of 03 for today's date it puts 3. Can someone help me with something that may work better, currently I'm using a set to grab portions of the date.
0
Comment
Question by:nasteal
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:devil_himself
Comment Utility
do you want to subtract 1 day from the given input date or you want to find yesterdays date from the current system date.
0
 

Author Comment

by:nasteal
Comment Utility
Well I want to create a file with the previous day's date.
0
 
LVL 8

Accepted Solution

by:
devil_himself earned 500 total points
Comment Utility
0
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

 

Author Comment

by:nasteal
Comment Utility
What I have now is:

set YYMM=%date:~12,4%%date:~4,2%
set DD=%date:~7,2%
set /a PDD = %DD% - 1

Which gives me the problem described above.
0
 

Author Comment

by:nasteal
Comment Utility
That code will work however it sets

YYYYMMDD

I am unfamiliar with tokens in the for, but I need YYMMDD and not sure how to strip off the 20 portion, I tried at the last line of code "if %yy% LSS 100 set yy=20%yy%", however removing it the date still shows 2008. Thanks for pointing out that bit of code, I just need to figure out the portion to remove the "20"
@echo off 

setlocal 

call :get_date 

:: Strip leading zeros from possible octals and decrement the day 

set /a mm=1%mm%-100, dd=1%dd%-101 

if %dd% NEQ 0 goto :add_zeros 

:: Today is the 1st of the month - decrement the month 

:: and set leap year check (ignoring centuries) 

set /a mm-=1,ly=yy%%4 

:: If today is 1 Jan, set date to 31st Dec 

if %mm% EQU 0 (set /a dd=31, mm=12, yy-=1) else ( 

  rem Calculate days in last month (by Frank Westlake) 

  set /a "dd=5546>>mm&1,dd+=30" 

  rem Special case for February 

  if %mm% EQU 2 if %ly% EQU 0 (set dd=29) else (set dd=28) 

) 

:add_zeros 

if %dd% LSS 10 set dd=0%dd% 

if %mm% LSS 10 set mm=0%mm% 

echo yesterday was %yy%%mm%%dd% 

goto :eof 
 
 

:: ------------------------------------------------------------------ 

:Get_Date 

:: ------------------------------------------------------------------ 

:: Generic date parser 

:: Sets %dd% (01-31), %mm% (01-12) & %yy% (4 digit) 
 
 

if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4) 

for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo:^|date') do ( 

  for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do ( 

    set %%a=%%d 

    set %%b=%%e 

    set %%c=%%f 

    set toks= 

  ) 

) 

if %yy% LSS 100 set yy=20%yy% 

goto :eof 

Open in new window

0
 
LVL 8

Assisted Solution

by:devil_himself
devil_himself earned 500 total points
Comment Utility
change line 20

echo yesterday was %yy%%mm%%dd%

to

echo yesterday was %yy:~2%%mm%%dd%
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

7 Experts available now in Live!

Get 1:1 Help Now