PC Review


Reply
Thread Tools Rate Thread

Date variable in a command file

 
 
JDR
Guest
Posts: n/a
 
      16th Feb 2009
I have a cmd line that reads

set vardate=%date%

I need to assign the vardate a value of yesterday's date.

Is there a way to assign the current date -1?

Thanks
 
Reply With Quote
 
 
 
 
billious
Guest
Posts: n/a
 
      16th Feb 2009

"JDR" <(E-Mail Removed)> wrote in message
news:22021343-BB94-4D89-86F8-(E-Mail Removed)...
>I have a cmd line that reads
>
> set vardate=%date%
>
> I need to assign the vardate a value of yesterday's date.
>
> Is there a way to assign the current date -1?
>
> Thanks


Try Timo Salmi's FAQ in alt.msdos.batch.nt; last posted today - item 6.


 
Reply With Quote
 
JDR
Guest
Posts: n/a
 
      16th Feb 2009
I'm going to be stupid but where is alt.msdos.batch.nt?

Thanks

"billious" <(E-Mail Removed)> wrote in message
news:499902d4$0$679$(E-Mail Removed)...
>
> "JDR" <(E-Mail Removed)> wrote in message
> news:22021343-BB94-4D89-86F8-(E-Mail Removed)...
>>I have a cmd line that reads
>>
>> set vardate=%date%
>>
>> I need to assign the vardate a value of yesterday's date.
>>
>> Is there a way to assign the current date -1?
>>
>> Thanks

>
> Try Timo Salmi's FAQ in alt.msdos.batch.nt; last posted today - item 6.
>

 
Reply With Quote
 
billious
Guest
Posts: n/a
 
      16th Feb 2009

"JDR" <(E-Mail Removed)> wrote in message
newsBAC9926-AB4B-4A28-9BAC-(E-Mail Removed)...
> "billious" <(E-Mail Removed)> wrote in message
> news:499902d4$0$679$(E-Mail Removed)...
>>
>> "JDR" <(E-Mail Removed)> wrote in message
>> news:22021343-BB94-4D89-86F8-(E-Mail Removed)...
>>>I have a cmd line that reads
>>>
>>> set vardate=%date%
>>>
>>> I need to assign the vardate a value of yesterday's date.
>>>
>>> Is there a way to assign the current date -1?
>>>
>>> Thanks

>>
>> Try Timo Salmi's FAQ in alt.msdos.batch.nt; last posted today - item 6.
>>


> I'm going to be stupid but where is alt.msdos.batch.nt?
>
> Thanks
>


It's another news group - in the same way as you've posted this message to
microsoft.public.win2000.cmdprompt.admin, try alt.msdos.batch.nt - or
alt.msdos.batch - which carries the same thread (some news servers don't
carry ...nt; most would carry ...batch)

Or Google for "alt.msdos.batch.nt" or "timo salmi batch faq"


 
Reply With Quote
 
foxidrive
Guest
Posts: n/a
 
      16th Feb 2009
On Mon, 16 Feb 2009 00:01:32 -0600, "JDR" <(E-Mail Removed)> wrote:

>I have a cmd line that reads
>
>set vardate=%date%
>
>I need to assign the vardate a value of yesterday's date.
>
>Is there a way to assign the current date -1?


:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
echo to get todays date use
echo call "%~n0" today 0
echo.
echo to get yesterdays date use
echo call "%~n0" today -1
echo.
echo to get the date 25 days ago:
echo call "%~n0" today -25
echo.
echo to get the date 1250 days in the future
echo call "%~n0" today +1250
goto :EOF)

set date1=%1
set qty=%2
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" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)

 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      16th Feb 2009
billious <> wrote:
> "JDR" <(E-Mail Removed)> wrote in message
>> I have a cmd line that reads
>> set vardate=%date%
>> I need to assign the vardate a value of yesterday's date.
>> Is there a way to assign the current date -1?


> Try Timo Salmi's FAQ in alt.msdos.batch.nt; last posted today - item 6.


Below is a copy of the posting in case the OP lacks access. The direct
link to the said item #6 is

6} How does one get yesterday's date?
http://www.netikka.net/tsneti/info/tscmd006.htm

<snip>
Dear new alt.msdos.batch and alt.msdos.batch.nt readers,

Before posing a question you might first wish to take a look at the
frequently asked questions in these newsgroups.

An online version of Useful NT/2000/XP batch file tricks and tips
http://www.netikka.net/tsneti/info/tscmd.htm

A FAQ collection of NT/2000/XP batch file tricks and tips
http://garbo.uwasa.fi/pub/pc/link/tscmd.zip

A FAQ collection of MSDOS/Win9x/ME etc batch files tricks and tips
http://garbo.uwasa.fi/pub/pc/link/tsbat.zip

For further reading: Windows scripting and programming links
http://lipas.uwasa.fi/~ts/http/http2.html#cmdscript

Also: MS-DOS+Win../95/98/Me batch programming links
http://lipas.uwasa.fi/~ts/http/http2.html#batch

The main scope of these batch file related newsgroups are
alt.msdos.batch :for MS-DOS+Win../95/98/Me batch scripts
alt.msdos.batch.nt :for NT/2000/XP/.. batch scripts

Please define your OS when posing a batch/script question.
</snip>

All the best, Timo

--
Prof. Timo Salmi private.php?do=newpm&u= ftp & http://garbo.uwasa.fi/
Home page: http://www.uwasa.fi/laskentatoimi/he...nta/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Timo's FAQ materials at http://lipas.uwasa.fi/~ts/http/tsfaq.html
 
Reply With Quote
 
Tom Lavedas
Guest
Posts: n/a
 
      16th Feb 2009
On Feb 16, 2:44*am, foxidrive <got...@woohoo.invalid> wrote:
> On Mon, 16 Feb 2009 00:01:32 -0600, "JDR" <jdru...@hotmail.com> wrote:
> >I have a cmd line that reads

>
> >set vardate=%date%

>
> >I need to assign the vardate a value of yesterday's date.

>
> >Is there a way to assign the current date -1?

>
> :: Date foward & backward
> @echo off
> :: from code by Phil Robyn
> setlocal
> if [%1]==[] (
> * echo to get todays date use
> * echo call "%~n0" today 0
> * echo.
> * echo to get yesterdays date use
> * echo call "%~n0" today -1
> * echo.
> * echo to get the date 25 days ago:
> * echo call "%~n0" today -25
> * echo.
> * echo to get the date 1250 days in the future
> * echo call "%~n0" today +1250
> * goto :EOF)
>
> set date1=%1
> set qty=%2
> 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" WScript.Echo year(s)^&_
> echo>>"%temp%\%~n0.vbs" * * * * right(100+month(s),2)^&_
> echo>>"%temp%\%~n0.vbs" * * * * right(100+day(s),2)
> for /f %%a in (
> * 'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
> del "%temp%\%~n0.vbs"
> endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
> echo %%day%% is set to "%day%" (without the quotes)


It seems to me to be easier to use a batch/WSH hybrid here, as in ...

::RunWSH.CMD
@echo off
if '%1==' goto :EOF
if /i '%1=='date %_then_% (
%0 "year(now)&right(chr(48)&month(now),2)&right(chr(48)&day(now),
2)%2"
)
echo wsh.echo "res="^&eval("%~1")> %temp%\tmp.vbs
for /f "delims=" %%a in ('cscript //nologo %temp%\tmp.vbs') do set %
%a
del %temp%\tmp.vbs
% For demonstration % echo Res = %Res%

This takes up to two arguments. The first can be an expression for
evaluation, or the word DATE (case insensitive) and the second
[optional] argument can be a continuation of the evaluation or a
number to add (+/-n) to the DATE. The expressions can be enclosed
with double quotes, if needed to handle included delimiters, like
commas or spaces. However, the procedure as written cannot handle
string (text) expressions.

The procedure stores its return value in the environment variable
RES. The last line is intended as an illustration, only. It is best
to remove it or comment it out for production use.

In use, the current question can be answered in several ways,
depending on the desired format of the result ...

C:\>RunWSH date -1
Res = 20090215

or

C:\>RunWSH date-1
Res = 2/15/2009

or

C:\>RunWSH now-1
Res = 2/15/2009 8:14:09 AM

I suspect the first version is the most helpful.

The procedure is not limited to date manipulation, however. For
example, the issue of handling decimal math ...

C:\>RunWSH 2/3
Res = 0.666666666666667

etc. Anything that can be expressed as a valid VBS mathematical
expression. Values stored in environment variables can be used, as
with any batch procedure. Knowledge of VBS IS needed to use
complicated expressions or intrinsic functions, but not to do
straightforward math. Such math expressions are almost universally as
you might expect, simply the numbers with the operators between.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
 
Reply With Quote
 
JDR - Hotmail
Guest
Posts: n/a
 
      16th Feb 2009
Excellent!

Thanks for every ones input!

"Tom Lavedas" <(E-Mail Removed)> wrote in message
news:961fe201-118f-48fe-8b02-(E-Mail Removed)...
> On Feb 16, 2:44 am, foxidrive <got...@woohoo.invalid> wrote:
>> On Mon, 16 Feb 2009 00:01:32 -0600, "JDR" <jdru...@hotmail.com> wrote:
>> >I have a cmd line that reads

>>
>> >set vardate=%date%

>>
>> >I need to assign the vardate a value of yesterday's date.

>>
>> >Is there a way to assign the current date -1?

>>
>> :: Date foward & backward
>> @echo off
>> :: from code by Phil Robyn
>> setlocal
>> if [%1]==[] (
>> echo to get todays date use
>> echo call "%~n0" today 0
>> echo.
>> echo to get yesterdays date use
>> echo call "%~n0" today -1
>> echo.
>> echo to get the date 25 days ago:
>> echo call "%~n0" today -25
>> echo.
>> echo to get the date 1250 days in the future
>> echo call "%~n0" today +1250
>> goto :EOF)
>>
>> set date1=%1
>> set qty=%2
>> 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" WScript.Echo year(s)^&_
>> echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
>> echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
>> for /f %%a in (
>> 'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
>> del "%temp%\%~n0.vbs"
>> endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
>> echo %%day%% is set to "%day%" (without the quotes)

>
> It seems to me to be easier to use a batch/WSH hybrid here, as in ...
>
> ::RunWSH.CMD
> @echo off
> if '%1==' goto :EOF
> if /i '%1=='date %_then_% (
> %0 "year(now)&right(chr(48)&month(now),2)&right(chr(48)&day(now),
> 2)%2"
> )
> echo wsh.echo "res="^&eval("%~1")> %temp%\tmp.vbs
> for /f "delims=" %%a in ('cscript //nologo %temp%\tmp.vbs') do set %
> %a
> del %temp%\tmp.vbs
> % For demonstration % echo Res = %Res%
>
> This takes up to two arguments. The first can be an expression for
> evaluation, or the word DATE (case insensitive) and the second
> [optional] argument can be a continuation of the evaluation or a
> number to add (+/-n) to the DATE. The expressions can be enclosed
> with double quotes, if needed to handle included delimiters, like
> commas or spaces. However, the procedure as written cannot handle
> string (text) expressions.
>
> The procedure stores its return value in the environment variable
> RES. The last line is intended as an illustration, only. It is best
> to remove it or comment it out for production use.
>
> In use, the current question can be answered in several ways,
> depending on the desired format of the result ...
>
> C:\>RunWSH date -1
> Res = 20090215
>
> or
>
> C:\>RunWSH date-1
> Res = 2/15/2009
>
> or
>
> C:\>RunWSH now-1
> Res = 2/15/2009 8:14:09 AM
>
> I suspect the first version is the most helpful.
>
> The procedure is not limited to date manipulation, however. For
> example, the issue of handling decimal math ...
>
> C:\>RunWSH 2/3
> Res = 0.666666666666667
>
> etc. Anything that can be expressed as a valid VBS mathematical
> expression. Values stored in environment variables can be used, as
> with any batch procedure. Knowledge of VBS IS needed to use
> complicated expressions or intrinsic functions, but not to do
> straightforward math. Such math expressions are almost universally as
> you might expect, simply the numbers with the operators between.
>
> Tom Lavedas
> ***********
> http://there.is.no.more/tglbatch/


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open file with variable date name based on current date Ciprian Microsoft Excel Programming 1 7th Jan 2010 02:31 PM
Saving file with date variable Steel Monkey Microsoft Excel Programming 1 16th Aug 2006 02:52 PM
Usage of environment variable in a batch file as command line argument kk Microsoft Windows 2000 CMD Promt 2 28th Jan 2006 06:33 AM
Passing a date variable to import file margo430 Microsoft Access Macros 4 29th Dec 2005 07:10 PM
Command line - File access date VDF Microsoft Windows 2000 CMD Promt 4 14th Jul 2004 12:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:48 AM.