Date Modification Batch file

August 11, 2011 at 10:42:40
Specs: Windows XP
I am using a script that I compiled from a post off ( http://www.computing.net/answers/wi... )this web site along with my modifications.

the script is to copy files and move them to a storage folder with the previous business days date.

I will post the first half of my script. It has been working great with the exception of a few days that fall on the 8th and 9th. as of this month the 8th the files are being created with the correct day but year 2010 and month 12 i.g. 12052010

I have been using this script since 12-2010 and did not have any issues until a day fell on the 8th.

There had been a mention of issue with the numbers 08 and 09 with this script which I did not see before. this must be the issue when i get file names like today's (8th) 12-0-1--1

as of the new issue with the year wrong, I have noticed when the script runs I get: for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set todaydow=%%a& set todaymonth=%%b& set todayday=%%c& set todayyear=%%d)
%%a was unexpected.

Here is the main first half of my file and under it will be the original posts from the thread I used it from:

@echo off
rem calculation for mmddyyyy including leapyear
for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set todaydow=%%a& set todaymonth=%%b& set todayday=%%c& set todayyear=%%d)

set /a leapyear=todayyear/4*4

set yy=%todayyear%
set /a mm=todaymonth
set /a dd=todayday-1
if %dd% lss 10 set dd=0%dd%
if %dd%==00 set /a mm=todaymonth-1
if %mm% lss 10 set mm=0%mm%
if %mm%==00 set /a yy=todayyear-1
if %mm%==00 set /a mm=12
if %dd%==00 if %mm%==02 if %yy%==%leapyear% set dd=29
if %dd%==00 if %mm%==02 if not %yy%==%leapyear% set dd=28
if %dd%==00 if %mm%==04 set dd=30
if %dd%==00 if %mm%==06 set dd=30
if %dd%==00 if %mm%==09 set dd=30
if %dd%==00 if %mm%==11 set dd=30
if %dd%==00 set dd=31

echo %date% %time% >>C:\"Documents and Settings"\doa\"My Documents"\"AutoLaserVault Scripts"\"LOG FILE"\"LV Log %mm%-%dd%-%yy%.log"
rem

REM 1 ASACRP
set source=D:\lvdata\TODAY\ASACRP\*.R*
set destination=D:\lvdata\TODAY\ASACRP\ASACRP %mm%%dd%%yy%
echo %time% Source= %source% Destination= %destination% >>C:\"Documents and Settings"\doa\"My Documents"\"AutoLaserVault Scripts"\"LOG FILE"\"LV Log %mm%-%dd%-%yy%.log"
xcopy "%source%" "%destination%" /d /i /y /v >>C:\"Documents and Settings"\doa\"My Documents"\"AutoLaserVault Scripts"\"LOG FILE"\"LV Log %mm%-%dd%-%yy%.log"


----------------------------------------
---------------------------------------
Here are the posts from the other thread.

NOTS3W June 6, 2006 at 22:43:53 Pacific
This fixes the leap year problem. The next time March 1 falls on a Sunday in a leap year is in 2020 (I'll hopefully be retired). But this will now work regardless of the day of the week. The day before the 1st of March in a leap year is now February 29.
for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set todaydow=%%a& set todaymonth=%%b& set todayday=%%c& set todayyear=%%d)
set /a leapyeartest=todayyear/4*4
set yesterdayyear=%todayyear%
set /a yesterdaymonth=todaymonth
set /a yesterdayday=todayday-1
if %yesterdayday% lss 10 set yesterdayday=0%yesterdayday%
if %yesterdayday%==00 set /a yesterdaymonth=todaymonth-1
if %yesterdaymonth% lss 10 set yesterdaymonth=0%yesterdaymonth%
if %yesterdaymonth%==00 set /a yesterdayyear=todayyear-1
if %yesterdaymonth%==00 set /A yesterdaymonth=12
if %yesterdayday%==00 if %yesterdaymonth%==02 if %yesterdayyear%==%leapyeartest% set yesterdayday=29
if %yesterdayday%==00 if %yesterdaymonth%==02 if not %yesterdayyear%==%leapyeartest% set yesterdayday=28
if %yesterdayday%==00 if %yesterdaymonth%==04 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==06 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==09 set yesterdayday=30
if %yesterdayday%==00 if %yesterdaymonth%==11 set yesterdayday=30
if %yesterdayday%==00 set yesterdayday=31
Ray
Mechanix2Go June 6, 2006 at 23:39:30 Pacific
________________________________________
Hi Ray,
I applaud your effort.
A couple cautions:
[1] Your for line is hard wired for a particular date layout which is bound to cause problems. That's why I put up YMD6.bat
[2] I don't have the energy right now to phony up a matching date layout to match yours, but be aware thst if you try to do a set /a on either 08 or 09 it will fail. Because some idiot at M$ decided that 0x is OCTAL notation; so naturally 08 and 09 are NG. [Who the heck uses OCTAL?]

If at first you don't succeed, you're about average.
M2


----------------------------
I have also seen this on ( http://stackoverflow.com/questions/... )

The problem is that you're using %m% inside a for loop. This is evaluated when the loop is read (before any iterations at all). In other words, the entire loop, up to and including the closing parenthesis, is read and evaluated before executing. So %m% will always be it's initial value no matter what you actually set it to within the loop.
-------------------------------------


So what I am looking for is a way to solve the 9 and 8 issue which might solve the issue with it being the 8th month??
And fix the "%%a was unexpected." issue at the start of the script


See More: Date Modification Batch file

Report •


#1
August 12, 2011 at 04:50:15
When the day and/or month is <10 you should remove the leading zero then replace it after the Set /A command has been performed - or - you can replace all the date calculations commands with a small VBScript which will end your date modification miseries. I assume you are extracting yesterday's date:

setlocal enabledelayedexpansion

set vbs=%temp%\vbs.vbs

> %vbs% echo WScript.Echo DateAdd("d",-1,Date)

for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do (
    set newdate=%%a

    for /f "tokens=1-4 delims=/ " %%1 in ("!newdate!") do (
        set dow=%%1&set mm=%%2&set dd=%%3&set yy=%%4
  )
)
del %vbs%
endlocal


Please come back & tell us if your problem is resolved.


Report •

#2
August 15, 2011 at 07:48:17
Thank you for your reply Wahine,

I still am having the same kind of issue with the vbs add in. I get "was unexpected at this time" errors.

here is how I modded my script:

setlocal enabledelayedexpansion

set vbs=%temp%\vbs.vbs

> %vbs% echo WScript.Echo DateAdd("d",-1,Date)

for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do (
set newdate=%%a

for /f "tokens=1-4 delims=/ " %%a in ("!newdate!") do (
set todaydow=%%a&set todaymonth=%%b&set todayday=%%c&set todayyear=%%d
)
)
del %vbs%
endlocal


set /a leapyear=todayyear/4*4

set yy=%todayyear%
set /a mm=todaymonth
set /a dd=todayday-1
if %dd% lss 10 set dd=0%dd%
if %dd%==00 set /a mm=todaymonth-1
if %mm% lss 10 set mm=0%mm%
if %mm%==00 set /a yy=todayyear-1
if %mm%==00 set /a mm=12
if %dd%==00 if %mm%==02 if %yy%==%leapyear% set dd=29
if %dd%==00 if %mm%==02 if not %yy%==%leapyear% set dd=28
if %dd%==00 if %mm%==04 set dd=30
if %dd%==00 if %mm%==06 set dd=30
if %dd%==00 if %mm%==09 set dd=30
if %dd%==00 if %mm%==11 set dd=30
if %dd%==00 set dd=31

echo %date% %time% >>C:\"Documents and Settings"\sd6\"My Documents"\"LOG FILE"\"LV Log %mm%-%dd%-%yy%.log"

---------------------------------------------------------------------------------------------------

HERE IS THE ERRORS I GET:

C:\WINDOWS>setlocal enabledelayedexpansion

C:\WINDOWS>
C:\WINDOWS>set vbs=%temp%\vbs.vbs

C:\WINDOWS>
C:\WINDOWS>> %vbs% echo WScript.Echo DateAdd("d",-1,Date)

C:\WINDOWS>
C:\WINDOWS>for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do (
%%a was unexpected at this time.
C:\WINDOWS> set newdate=%%a

C:\WINDOWS>
C:\WINDOWS> for /f "tokens=1-4 delims=/ " %%a in ("!newdate!") do (
%%a was unexpected at this time.
C:\WINDOWS> set todaydow=%%a&set todaymonth=%%b&set todayday=%%c&set toda
yyear=%%d

------------------------------------------------------------------------------------

THE FILE NAME THAT IS PRODUCED:
LV Log 12-0-1--1.log

which is the same name I was getting before.


also, placing the remove leading zero: "When the day and/or month is <10 you should remove the leading zero then replace it after the Set /A command has been performed"
If you set it like this:

if %dd%==00 set /a mm=todaymonth-1
if %dd% lss 10 set dd=0%dd%
if %mm%==00 set /a yy=todayyear-1
if %mm% lss 10 set mm=0%mm%

------ I get a file named LV Log 12-0-1-%%d.log


Report •

#3
August 15, 2011 at 15:45:02
Yes, but I wrote you can replace all the date calculations commands with a small VBScript which will end your date modification miseries. which you haven't done, you still have:
set /a leapyear=todayyear/4*4

set yy=%todayyear%
set /a mm=todaymonth
set /a dd=todayday-1
if %dd% lss 10 set dd=0%dd%
if %dd%==00 set /a mm=todaymonth-1
if %mm% lss 10 set mm=0%mm%
if %mm%==00 set /a yy=todayyear-1
if %mm%==00 set /a mm=12
if %dd%==00 if %mm%==02 if %yy%==%leapyear% set dd=29
if %dd%==00 if %mm%==02 if not %yy%==%leapyear% set dd=28
if %dd%==00 if %mm%==04 set dd=30
if %dd%==00 if %mm%==06 set dd=30
if %dd%==00 if %mm%==09 set dd=30
if %dd%==00 if %mm%==11 set dd=30
if %dd%==00 set dd=31

in your script. The small .vbs script takes care of all these leap year calculations.

Here is a slightly modified script incorporating the .vbs script, play around with it on its own before incorporating it into your batch script. It will hopefully create the .log file in your %temp% folder (for testing purposes only) with yesterday as the filename and today's date and time as the content.

setlocal enabledelayedexpansion

set vbs=%temp%\vbs.vbs

> %vbs% echo WScript.Echo DateAdd("d",-1,Date)

for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do (
    set newdate=%%a

        for /f "tokens=1-4 delims=/ " %%a in ("!newdate!") do (
            set mm=%%b&set dd=%%c&set yy=%%d
  )
)
del %vbs%

echo %date% %time% >%temp%\%mm%-%dd%-%yy%.log"

endlocal


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
August 17, 2011 at 07:35:48
I have been messing around with the original VBS you gave me, and now this one.
I guess I was not clear on where to implement this script, as I am not well versed in VBS, just a few basics.

And my issue still sets with this VBS script that I get " a%% was unexpected at this time" errors.

which stops the rest of the script for being able to run properly.
Its this line right here where I get that error:

for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do (
set newdate=%%a


Report •

#5
August 18, 2011 at 00:59:15
Sorry, I am unable to replicate the error you get using the script supplied. Please save the following as a .bat file, run it and post the exact output displayed. Please do not incorporate it with any other script commands, run it as is.

@echo off
cls

setlocal enabledelayedexpansion

set vbs=%temp%\vbs.vbs

> %vbs% echo WScript.Echo DateAdd("d",-1,Date)

for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do (
    set newdate=%%a

        for /f "tokens=1-4 delims=/ " %%a in ("!newdate!") do (
            set mm=%%b&set dd=%%c&set yy=%%d
  )
)

del %vbs%

echo %date% %time% >%temp%\%mm%-%dd%-%yy%.log"
dir %temp%\*.log
echo.&echo.
echo Content of log file is:
type %temp%\%mm%-%dd%-%yy%.log

endlocal


Please come back & tell us if your problem is resolved.


Report •

Ask Question