Change in date input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Change in date input

#1 Post by mor.bas » 03 Nov 2013 05:31

Hi,

I have a script the collect the date and do processing on this directory

FOR /F " tokens=1,2,3,4 delims=/,-,. " %%i IN ('date /T') DO set currentdate=%%k%%j

I want to add 1 to the day (of today) and then doing

xcopy /e /o /h /k "G:\Clear\%currentdate%\*.*" "H:\*.*"

current date varible will be current date +1

Thanks...

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Change in date input

#2 Post by foxidrive » 03 Nov 2013 05:33

Do you want a batch that uses a VBS script? You can pull out the bits to calculate the day +1 or use it as is.

Code: Select all

:: Date forward & backward
@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs"         d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& (
set "YY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "daynum=%result:~-1%"
)
if %daynum% EQU 1 set "weekday=Sunday"
if %daynum% EQU 2 set "weekday=Monday"
if %daynum% EQU 3 set "weekday=Tuesday"
if %daynum% EQU 4 set "weekday=Wednesday"
if %daynum% EQU 5 set "weekday=Thursday"
if %daynum% EQU 6 set "weekday=Friday"
if %daynum% EQU 7 set "weekday=Saturday"

set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%
echo The weekday turns out to be: %weekday%
pause

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Change in date input

#3 Post by mor.bas » 03 Nov 2013 06:37

Hi,
Thanks
But I only want the 2 lines to adding to my script to add 1 to the current day and get the new variblr to use after...

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Change in date input

#4 Post by foxidrive » 03 Nov 2013 06:47

You have to calculate day, month, and year changes. Allow for leap years, and also calculate the days in the month.

2 lines in plain batch isn't really going to do it.

aGerman
Expert
Posts: 3869
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change in date input

#5 Post by aGerman » 03 Nov 2013 08:45

That question came up frequently before. Just search the forum and you'll find threads like that.

Perhaps some information to understand why that can't be solved within two lines of batch code. Basically the command processor knows only one data type. That is a "null terminated string of characters". The meaning of that complicated expression is "text". The reason is that the black console window is a text-based window. Also the manner how data is saved in the computer memory is text in case of variables and their values. Using some commands (SET /A, IF, FOR /F, EXIT in particular) a numeric string is implicitely casted into an integer value.
That's all. Other data types like floating point numbers, date/time values, arrays, class objects etc. are not supported. Knowing that you can imagine what you have to do for date calculations:
- Split the date string into separate values for year, month and day. These are integer numbers that the command processor can handle.
- Use algorithms to calculate the number of days counted from a certain date on.
- Add the number of days you want.
- Calculate it back to year, month and day.

Those algorithms you'll find in the function refeference (:date2jdate and :jdate2date).

There are some more difficulties you have to fight though.
- Often a zero is prepended to month and day that lets the command processor parse them as octal numbers. You have to get rid of it.
- Both the order of year, month and day and the separator between them are language dependent. You have to figure it out before you split the date string.

Regards
aGerman

Aacini
Expert
Posts: 1631
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Change in date input

#6 Post by Aacini » 03 Nov 2013 12:31

First of all, you have not show us the format of 'date /T' in your computer, but I assumed that is Dow MM/DD/YYYY. Well, perhaps not two additional lines, but close:

Code: Select all

setlocal EnableDelayedExpansion

FOR /F " tokens=1,2,3,4 delims=/,-,. " %%i IN ('date /T') DO set /A dd=1%%k+1, mm=1%%j, mmPos=(1%%j%%100-1)*2, leap=%%l%%4
if %leap% neq 0 (set dpm=3128) else set dpm=3129
set dpm=%dpm%31303130313130313031
if %dd% gtr 1!dpm:~%mmPos%,2! set /A dd=101, mm+=1 & if !mm! gtr 112 set mm=101
set currentdate=%dd:~-2%%mm:~-2%
xcopy /e /o /h /k "G:\Clear\%currentdate%\*.*" "H:\*.*"


Note that if the format of 'date /T' in your computer is different, a small modification is needed. If you want to run this Batch file in several computers with possible different date formats, then you must follow the advice of aGerman above...

Antonio

einstein1969
Expert
Posts: 774
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Change in date input

#7 Post by einstein1969 » 03 Nov 2013 15:10

try:

Code: Select all

 for /f "delims=" %%a in ('mshta vbscript:Execute("createobject(""scripting.filesystemobject"").GetStandardStream(1).write(date+1):close"^)') do echo(%%a



einstein1969

aGerman
Expert
Posts: 3869
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change in date input

#8 Post by aGerman » 03 Nov 2013 15:42

Nice find, einstein1969!
I never used the GetStandardStream method in VBScript but in connection with mshta it may come in handy also for other tasks.

Regards
aGerman

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Change in date input

#9 Post by foxidrive » 03 Nov 2013 21:19

Here are some powershell examples.

Code: Select all

@echo off
for /f "usebackq" %%x in (`powershell "(Get-Date).AddDays(-8).ToString('dd-MMM-yyyy')"`) do set "d8=%%x"
echo 8 days ago was %d8%
pause

@echo Off
:: change -1 to 0 for todays date.
for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%m%%d"') do set "d8=%%a"
echo yesterday was %d8%
pause

@echo Off
For /F "delims=" %%a In ('PowerShell -Command "&{((Get-Date).AddDays(+1)).ToString('yyyyMMdd')}"') Do Set "d8=%%a"
Echo tomorrow is %d8%
pause

Sponge Belly
Posts: 197
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Change in date input

#10 Post by Sponge Belly » 04 Nov 2013 16:22

Hi mor.bas! :-)

Use Carlos’s pure Batch getDate function to acquire the date in locale-independent form. Once you have that nailed down, you can use xcopy’s /d switch to validate dates n days ago/from now as detailed in Paul Tomasi’s Validating Dates article.

HTH! ;-)

- SB

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Change in date input

#11 Post by foxidrive » 04 Nov 2013 16:45

Sponge Belly wrote:Hi mor.bas! :-)

Use Carlos’s pure Batch getDate function to acquire the date in locale-independent form.


mor.bas wants a 2 line solution. :roll:

shayanjameel08
Posts: 10
Joined: 21 Nov 2013 01:07

Re: Change in date input

#12 Post by shayanjameel08 » 27 Nov 2013 23:57

I used to chnange in dos .....Changes the DOS command prompt. ... (the indicator that DOS is ready for input) ... \DOS> TIME=11:07:54.23 like that

Squashman
Expert
Posts: 4150
Joined: 23 Dec 2011 13:59

Re: Change in date input

#13 Post by Squashman » 28 Nov 2013 08:44

shayanjameel08 wrote:I used to chnange in dos .....Changes the DOS command prompt. ... (the indicator that DOS is ready for input) ... \DOS> TIME=11:07:54.23 like that

Not understanding how this helps the user with their problem.

Post Reply