1.

Solve : batch file to run on last day of the month...?

Answer»

good day,

i have a batch file that i have made slowly over the past months to do backups, with help from here, and several other websites. i want to update the batch file to backup up some other FOLDERS but only need that part of the batch file to run if it is the last day of the month.

ex. currently i back up C:\DOCS every day. But if it's the last day of the month, i want it to back up C:\TOSHIBA along with the C:\DOCS folder. can that be done, or is it easier to write the code to do it on the first of the month (last day of the month varies as ending on the 28th, 29th, 30th and 31st.

I was thinking just write another batch and run it on the first of every month to do the monthly backup, but the daily backup creats a log file, and when that back up is done, it reboots the system. i do not want the system to reboot while the monthly backup is still being performed.

any ideas?

jat
This combination of VB Scripting and batch commands should do what you want. The VB script gets tomorrow's day-of-month number and passes it to the FOR command which sets the NewDay variable. Should be run on the last day of each month. I have done some testing in XP but you should confirm...

Quote

@ECHO off
cls

set vbsfile=%temp%\newdate.vbs

echo Newdate = (Date()+1)>%vbsfile%
echo Dd = DatePart("D" , Newdate)>>%vbsfile%

echo Wscript.Echo Dd>>%vbsfile%

FOR /F %%A in ('cscript //nologo %vbsfile%') do (
set NewDay=%%A
)
del %vbsfile%

echo %NewDay%

IF %NewDay% NEQ 1 GOTO Docs

{backup commands for C:\TOSHIBA here}

:Docs
{backup commands for C:\DOCS here}


Code: [Select]if exist ME del ME
Mshta VBScript:Execute("If Month(Now)<>Month(DateAdd(""d"",1,Now)) Then CreateObject(""Scripting.FileSystemObject"").CreateTextFile(""ME""):END If:Close")

if not exist ME goto Docs
del ME
{backup commands for C:\TOSHIBA here}

:Docs
{backup commands for C:\DOCS here}And another which uses the Doff.exe utility from this site..

Code: [Select]@echo off
cls

for /f %%a in ('doff dd +1') do (
if %%a neq 1 goto docs
)

{Toshiba backup commands here}

:docs
{Docs backup commands here}

Good luckhave used the following for exactly what your doing, used it for 2 different
small businesses I supported...worth a look at:

http://www.karenware.com/powertools/ptreplicator.asp

fully automated alsoThis really useful tool. Thanks a lot for the postQuote from: Prince_ on November 27, 2008, 01:22:42 AM
Code: [Select]if exist ME del ME
Mshta VBScript:Execute("If Month(Now)<>Month(DateAdd(""d"",1,Now)) Then CreateObject(""Scripting.FileSystemObject"").CreateTextFile(""ME""):End If:Close")

if not exist ME goto Docs
del ME
{backup commands for C:\TOSHIBA here}

:Docs
{backup commands for C:\DOCS here}

i have this in my code as follows:

@echo off
set folder=%date:~0,3%

ECHO %date%, %time% Checking connections, please wait... > C:\BackUpHistory\Report.txt

ECHO %date%, %time% Checking to see if www.google.ca can be reached... >> C:\BackUpHistory\Report.txt
PING -n 3 www.google.ca >> C:\BackUpHistory\Report.txt|find "Reply from " >NUL

:if exist ME del ME
Mshta VBScript:Execute("If Month(Now)<>Month(DateAdd(""d"",1,Now)) Then CreateObject(""Scripting.FileSystemObject"").CreateTextFile(""C:\BackUpHistory\ME""):End If:Close")

if not exist "C:\BackUpHistory\ME" goto Finish
del "C:\BackUpHistory\ME"
ECHO ------------------------------------------------------- >> C:\BackUpHistory\Report.txt
ECHO %time% Initiating monthly cleanup >> C:\BackUpHistory\Report.txt
FOR /D %%G IN ("C:\Documents and Settings\*.*") DO DEL/S/Q/F "%%G\Cookies\*.*"
FOR /D %%G IN ("C:\Documents and Settings\*.*") DO DEL/S/Q/F "%%G\Local Settings\Temp\*.*"
FOR /D %%G IN ("C:\Documents and Settings\*.*") DO DEL/S/Q/F "%%G\Local Settings\History\*.*"
FOR /D %%G IN ("C:\Documents and Settings\*.*") DO DEL/S/Q/F "%%G\Local Settings\Temporary Internet Files\*.*"
ECHO %time% Finished cleaning up internet explorer >> C:\BackUpHistory\Report.txt
:Finish
for /f "tokens=1-6 delims=/: " %%d in ("%date%" "%time%") do rename "C:\BackUpHistory\Report.txt" %%g-%%e-%%f(%%d)@%%h%%i.txt

:END

it was tested and does work in a batch file format (this is only part of the entire code). i convert the file to an exe file using a batch file to exe converter. when the exe file is run, the code does not run COMPLETELY. the reporting stops at the following line:

Initiating Monthly cleanup

there is nothing after this line.

any suggestions?

thank you,

jat
Quote from: kalasha on August 13, 2009, 11:12:03 PM

any suggestions?

thank you,

jat


Yes. Do not use or rely upon batch-to-exe converters. They generally only work with a limited subset of batch language. They are a kludge.

Get tomorrow's day number

Employ one-line vbs to determine date of tomorrow, yesterday, 999 days time/ago (whatever)

Code: [Select]@echo off

echo Wscript.echo eval(WScript.Arguments(0))>"%temp%\evaluate.vbs"

REM put date difference in a variable
REM tomorrow is +1
REM today is +0
REM yesterday is -1
set diff=+1

REM set trigger number in a variable
set trigger=1

For /f %%D in ( ' cscript //nologo "%temp%\evaluate.vbs" "day(date%diff%)" ' ) do set daynum=%%D

del "%temp%\evaluate.vbs"

if "%daynum%"=="%trigger%" (
do stuff
)



Discussion

No Comment Found