Solved

Dos Batch Routine needed that will only run on the 28th, 29th, 30th or 31st of the month.

Posted on 2011-09-22
24
334 Views
Last Modified: 2012-05-12
Hello Experts,

I use the following command to rename a log file that I use for a client's backup system. I want the command to proceed only if the current date is equal to or greater than 28

This client creates a backup every thursday and on the last day of the month and the last day of the month always changes. it's either the 28th, 29th, 30th or 31st.

The log file becomes too big and so I want the following command to run which will rename the log file.

ren "C:\Backup_Logs.txt" Backup_Logs-%date:~4,2%%date:~7,2%%date:~10,4%.txt

I need an IF statement in front of this that says if current date = 28 or greater then run, if not then end

Thanks for your help!

Lasareath

0
Comment
Question by:Lasareath
  • 9
  • 8
  • 5
  • +2
24 Comments
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36585145
I sthat Windows environment? If so, it's better to use Task Scheduler to run scheduled task (batch file) on those days and that's all :)

If you still require only batch for that, please wait for other experts, thanks.

Regards,
Krzysztof
0
 
LVL 2

Expert Comment

by:ASP8MBM
ID: 36585189
One way of doing it :
Set d =%date:~,-5%

Then run your test:

if %d:~8%==28 Echo "BackUP now"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36585241
Nearly there.  You can use %date:~4;2% or whatever gives you the day part of the date and use if what that... But do you get 01 etc. In your format or just 1 as this can be awkward.

i always prefer to get the date bits using a one line vbscript, I'll post the link when on pc in a bit but if you can get reliable day into variable, e.g.

Set day=%date:~4;2%   (amend as needed)

then you can do

if %day% GTR 27 (
  echo day is 28 or more...
  rename ....
)

or all one line

if %date....% GTR 27 rename ....

wiould still recommend not using %date% for this as if it runs under a different user or gets scheduled or whatever it could easily return a different date format and you'd be checking the wrong bit.

steve
0
 

Author Comment

by:Lasareath
ID: 36585284
@iSiek I thought about using the task scheduler but the code I need above is really only phase 1 of what I am trying to accomplish. The problem with the task scheduler is that if the end user really needed to run the backup on the 29th but they were not going to be in on the 29th and so they run it on the 28th then when it runs again on the 29th it will rename the log file again and we'll have two renames in one month.

I want to make some additional code that will create a temporary file after the rename has taken place. on the 28th the file will be named 28.txt, on the 29th it will be 29.txt and so on.

If the batch routine runs on the 28th again and it finds the 28.txt file then it knows that the log file has already been renamed and it quits the batch routine.

When the current date equals 1 through 27 and it finds the 28.txt, 29.txt, 30.txt or 31.txt it will attempt to delete all 4 txt files which will reset the batch again and it will be ready to rename the log file again on either of the 4 last days of the month.

Does that make sense?

Maybe there is an easier way. Basicially I have a scheduled task that run the backup program every night. if the PC is off it won't run but if they leave the PC on for the last 3 days of the month then I want to make sure I don't rename the log file 3 times. Only once per month on the last 4 days of the month.

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36585690
My article on how to get the date parts reliably is here: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html






My code below will get the date into a variable %day% and %yyyymmdd% for you which is then used to check the date and do two different things.  If the log file exists then it doesnt do the rename again.

Check log.txt for details of what happens too.

hth

Steve


@echo off
REM See http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html for dates info.
REM Steve Knight (http://scripts.dragon-it.co.uk)
REM Gets two bits from vbscript -- day and yyyy-mm-dd

set log=c:\logfile.txt
set script="%temp%\dateparts.vbs"
  echo wscript.echo day(date) ^& "," ^& year(date) ^& "-" ^& right(100 + month(date),2) ^& "-" ^& right(100+day(date),2) > %script%
  for /f "tokens=1,2 delims=," %%a in ('cscript //nologo %script%') do (set day=%%a) & (set yyyymmdd=%%b)
del %script%

if %day% GEQ 28 (
  if exist c:\Backup_Logs-%yyyymmdd%.txt (
    echo The day is %day% but log file to Backup_Logs-%yyyymmdd%.txt already exists
    echo Day=%day%.  Log already exists Backup_Logs-%yyyymmdd%.txt >>%log%
  ) ELSE (
    echo The day is %day% so renaming log file to Backup_Logs-%yyyymmdd%.txt
    echo Day=%day%.  Rename Backup_Logs-%yyyymmdd%.txt >>%log%
    ren "C:\Backup_Logs.txt" Backup_Logs-%yyyymmdd%.txt >>%log% 2>>&1
  )
) ELSE (
  echo The day is %day% and so no rename is done
  echo Day=%day%.  No rename >>%log%
  echo You will need to delete the old log files you don't want here?
)

Open in new window

0
 

Author Comment

by:Lasareath
ID: 36586218
@dragon-it

That script is almost perfect!

I changed my date on my PC to 9/28/11 and I ran the batch routine and it renames the Backup_Logs.txt to Backup_Logs-2011-09-28.txt which is correct.

But let's say somebody runs the backup again on the 30th, even though they shouldn't they might. the backup batch routine makes another Backup_Logs.txt file and then your script renames it to Backup_Logs-2011-09-30.txt

I'd like to have the script so that it only performs one rename between the dates of 28 through the 31st

It needs to say to itself, hey I already renamed the log file on the 28th and so I do not want to make a 2 day old log file on the 30th so I won't rename it. The next rename will happen between 10-28-11 through 10-31-11

What do you think?

Thanks!

Lasareath
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36586275
Why not run the script via Task Scheduler?  And then to keep it simple you could run it at say 1:00am on the FIRST of each month, that way you don't have to worry about what day the month ends on.  Task Scheduler also won't rerun the job on the same day.  You can also schedule it to run each Thursday as needed.

~bp
0
 

Author Comment

by:Lasareath
ID: 36586306
I've tried this. these clients only keep their PCs on at night one day a week during the backup . it's a peer to peer network and I back up files from 3 PC from one PC onto external hdds.

So if I tell it to run on the 1st of the month and the end of the week is the 3rd or 4th then that rename never happens. that is my problem.



0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36586353
If you do the following command on the PC where this will run, what exactly does it display?

ECHO %DATE%

~bp
0
 

Author Comment

by:Lasareath
ID: 36586357
Fri 09/23/2011
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36586358
Right, try this.  Just drops a flag file when it does a rename and deletes it on 1st time it runs the following month.

Possibly getting a little complicated, miught be easier way to achieve this but if it si what you want...

Steve
@echo off
REM See http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html for dates info.
REM Steve Knight (http://scripts.dragon-it.co.uk)
REM Gets two bits from vbscript -- day and yyyy-mm-dd

set log=c:\logfile.txt
set script="%temp%\dateparts.vbs"
  echo wscript.echo day(date) ^& "," ^& year(date) ^& "-" ^& right(100 + month(date),2) ^& "-" ^& right(100+day(date),2) > %script%
  for /f "tokens=1,2 delims=," %%a in ('cscript //nologo %script%') do (set day=%%a) & (set yyyymmdd=%%b)
del %script%


if %day% GEQ 28 (
  if exist C:\backup_Renamed_This_Month.txt (
    echo DAY: %day% This has already been run this month >>%log%
    echo Stopping.  This does not need to run again
  ) ELSE (
    if exist c:\Backup_Logs-%yyyymmdd%.txt (
      echo The day is %day% but log file to Backup_Logs-%yyyymmdd%.txt already exists
      echo Day=%day%.  Log already exists Backup_Logs-%yyyymmdd%.txt >>%log%
    ) ELSE (
      echo The day is %day% so renaming log file to Backup_Logs-%yyyymmdd%.txt
      echo Day=%day%.  Rename Backup_Logs-%yyyymmdd%.txt >>%log%
      ren "C:\Backup_Logs.txt" Backup_Logs-%yyyymmdd%.txt >>%log% 2>>&1
      echo Renamed to Backup_Logs-%yyyymmdd%.txt > C:\backup_Renamed_This_Month.txt
    )
  )
) ELSE (
  echo The day is %day% and so no rename is done
  echo Day=%day%.  No rename >>%log%
  If exist C:\backup_Renamed_This_Month.txt (
  echo Removed old flag file
  echo Day=%day%.  Removed flag file >>%log%
    DEL C:\backup_Renamed_This_Month.txt
  )
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36586364
Okay, I'll take a try...

~bp
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36586367
BTW your date format does not matter with the way I do it.... exactly why I do it this way.

I have made it much lengthier than requiired so as to show what it is doing at each stage more easily and to record the log file for you to show what it is doing.  It could soon be stripped down by taking out majority of the echo commands and anything writing to >> %log% except for the rename itself of course.

Steve
0
 

Author Comment

by:Lasareath
ID: 36586405
Steve,

The Backup_log.txt file does not get renamed at all now.

there's a logfile.txt that get updated with this info:

DAY: 28 This has already been run this month
DAY: 30 This has already been run this month

and there's a file named backup_Renamed_This_Month.txt which contains:

Renamed to Backup_Logs-2011-09-30.txt

but that file does not exist. I hit refresh many times. the original Backup_Logs.txt remains

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36586484
ok, probably missed a ) will check when back in a bit unless bill jumps in first!
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36586727
Here's what I was thinking, see if this makes sense.

@echo off

REM get pieces of date STRINGS from %DATE% variable
set yyyy=%DATE:~-4%
set mm=%DATE:~-10,2%
set dd=%DATE:~-7,2%

REM Get NUMERIC version of day (handles issues with leading zero days)
set /A Day=1%dd%-100

REM Switch to folder where backup files reside
pushd C:\

REM If day less than 28 (not "end of month") than exit
if %Day% LSS 28 goto :EOF

REM Check if we already did this for this "end of month"
set Done=N
for /L %%A in (28,1,31) do if exist "Backup_Logs-%yyyy%%mm%%%A.txt" set Done=Y

REM If not done yet for this "end of month" rename log file with yyyymmdd stamp
if %Done% == N ren "Backup_Logs.txt" Backup_Logs-%yyyy%%mm%%dd%.txt

REM Restore current directory
popd

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36587267
Ok delete the flag file, or run it as "day=1".   When I run it I get this.... I added the line "set day=29" or "set day=28" after the for statement to fake it being those days...


C:\Utils\scripts>checkday
The day is 23 and so no rename is done

C:\Utils\scripts>checkday
The day is 28   so renaming log file to Backup_Logs-2011-09-23.txt

C:\Utils\scripts>checkday
Stopping.  This does not need to run again

C:\Utils\scripts>checkday
Stopping.  This does not need to run again

C:\Utils\scripts>checkday
The day is 1  and so no rename is done
Removed old flag file

C:\Utils\scripts>checkday
The day is 1  and so no rename is done

C:\Utils\scripts>checkday
The day is 28 so renaming log file to Backup_Logs-2011-09-23.txt

Open in new window

0
 

Author Comment

by:Lasareath
ID: 36587279
Bill,

That works, the only issue I noticed is that with October the 28th is a friday and they usually run the backups on thursday night. So it doesn't change. So I'll just make the start date the 25th then I think it should work for almost every month.

Thanks!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36587287
and my log file looked like this:

Day=23.  No rename    
Day=28  .  Rename Backup_Logs-2011-09-23.txt    
DAY: 28   This has already been run this month    
DAY: 29   This has already been run this month    
Day=1 .  No rename    
Day=1 .  Removed flag file    
Day=1 .  No rename    
Day=28.  Rename Backup_Logs-2011-09-23.txt    
0
 

Author Comment

by:Lasareath
ID: 36587358
Bill,

I made it the 24th, makes more sense. one week before the end of the longest month. In Nov the 24th is the last Thursday.

Thanks a lot!

Lasareath
0
 

Author Closing Comment

by:Lasareath
ID: 36587382
Bill hit the nail right on the head with this code plus it does not create extra files.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36587413
Oh.... silly me for giving you a log file!!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 36587791
Thanks, glad that helped.  

~bp
0
 

Author Comment

by:Lasareath
ID: 36587934
I'm sorry Steve.  I appreciate your efforts.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now