1.

Solve : edit to yesterday's date?

Answer»

Hi there,
The following code copy some files from one location to another and put them in TODAYS named folder.
Instead of creating today's date, i want to make it yesterday's date.
Here is the code:

Code: [Select]@echo off
setlocal

for /F "tokens=1-6 delims=/:." %%T in ("%date:~4%.%time: =0%") do set timestamp=%%V%%U%%T-%%W.%%X

xcopy "C:\Documents and Settings\user\Desktop\allbackups" "C:\Documents and Settings\user\My Documents\TrainingLog\%timestamp%\" /C /H /R /Y /Z
Thanks in advance


This code (courtesy scripting guru Dias de Verano) will produce yesterday's date. You may have to alter the script to suit your date format.

Code: [Select]@echo off
echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
del yesterday.vbs

set ydate1=%ydate1:/=%

set m=%ydate1:~0,2%
set d=%ydate1:~2,2%
set y=%ydate1:~4,4%
set ydate2=%y%%m%%d%
echo yesterday was %ydate2%

Good luck.Quote from: Dusty on October 10, 2008, 03:13:54 AM

You may have to alter the script to suit your date format.

New! Improved! You won't have to alter this, it is independent of date format settings. You can fool around with the variables to make different strings as you can see in the 3 examples at the bottom.

Code: [Select]@echo off
echo Yesterdate = (Date^(^)- 1^)>yesterday.vbs
echo Yyyy = DatePart^("YYYY", Yesterdate^)>>yesterday.vbs
echo Mm = DatePart^("M" , Yesterdate^)>>yesterday.vbs
echo Dd = DatePart^("D" , Yesterdate^)>>yesterday.vbs
echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd>>yesterday.vbs

FOR /F "tokens=1,2,3 delims= " %%A in ('cscript //nologo yesterday.vbs') do (
set /a Year=%%A
set /a Month=%%B
set /a Day=%%C
)

if %Month% LSS 10 set Month=0%Month%
if %Day% LSS 10 set Day=0%Day%

REM Examples
echo (1)
echo.
echo Yesterday was:
echo.
echo Year : %Year%
echo Month : %Month%
echo Day : %Day%
echo.
echo (2)
echo.
echo %Day%/%Month%/%Year%
echo.
echo (3)
echo.
echo %Year%%Month%%Day%
echo.
Output today Friday 10/10/2008:

Code: [Select](1)

Yesterday was:

Year : 2008
Month : 10
Day : 09

(2)

09/10/2008

(3)

20081009Ahhh Dias...

Quote
O Lord my God, When I in awesome wonder,
Consider all the scripts Thy Hands have made;
I see the stars, I hear the rolling thunder,
Thy power throughout the internet displayed.

Then sings my soul, My Saviour God, to THEE,
How great Thou art, How great Thou art.
Then sings my soul, My Saviour God, to Thee,
How great Thou art, How great Thou art!

Although it is -- of course -- immensely flattering to be likened to God, modesty insists that I must decline such a comparison.

By the way, if you want to just keep Yesterday.vbs somewhere and call it when you need it, rather than create it from a batch script, it looks like this

Code: [Select]Yesterdate = (Date()- 1)
Yyyy = DatePart("YYYY", Yesterdate)
Mm = DatePart("M" , Yesterdate)
Dd = DatePart("D" , Yesterdate)
Wd = DatePart("W" , Yesterdate)
Wscript.Echo Yyyy&" "&Mm&" "&DdThank you for that but your elevated status is hereby revoked 'cos I couldn't get your New! Improved! version to perform.

I think this line:Quote
FOR /F "tokens=1,2,3 delims= " %%A in ('yesterday.vbs //nologo') do (

should be: Quote
FOR /F "tokens=1,2,3 delims= " %%A in ('cscript //nologo yesterday.vbs') do (



You are right - It depends on which of the two scripting hosts is the default on your system, cscript or wscript. On mine it is cscript, so I can just do...

Code: [Select]scriptname.vbs //nologo
...or even (assuming the script file has the .vbs extension)...

Code: [Select]scriptname //nologo
...from a batch WITHOUT having to specifically mention cscript.exe, but I forgot that on systems where the other host is the default it would have to use your line, so it is better (more UNIVERSAL) to do it that way, so I have edited the script above.

Incidentally, if you get bored with adding //nologo every time, you can make cscript save it as a per-user preference by using it just once with the //S switch when you run a script (any script, it applies to all scripts run with cscript by that user after that, until you change it back again by using //S with //logo)

Before (Windows default):

Code: [Select]S:\Test\Batch\Yesterday>yesterday
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

2008 10 10 6
Set //nologo as user's cscript default:

Code: [Select]S:\Test\Batch\Yesterday>cscript //nologo //s yesterday.vbs
Command line options are saved.
2008 10 10 6
After:

Code: [Select]S:\Test\Batch\Yesterday>yesterday
2008 10 10 6

To change it back again:

Code: [Select]S:\Test\Batch\Yesterday>cscript //logo //s yesterday.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Command line options are saved.
2008 10 10 6
Back the way we were...

Code: [Select]S:\Test\Batch\Yesterday>yesterday
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

2008 10 10 6


Discussion

No Comment Found