Solved

Batch File - do a backup of file that is the last business day of the month

Posted on 2016-10-19
9
62 Views
Last Modified: 2016-10-20
Hi there!
I am new with batch scripting does anyone can help me,
I have to backup the last file only and only if is the last business day of th month using batch script

Thanks a lot.

business days
1=Monday
2=Tuesday
3=Wednesday
4=Thursday
5=Friday
not business days
6=Saturday
0=Sunday
@echo off

setlocal enabledelayedexpansion
:: *** Get the current date into the variables dd, mm, yy:
set day = %date%
echo %date%
for /f "tokens=1-3 delims=/" %%a in ("%date%") do (
 
  set dd=%%a
  set mm=%%b
  set yy=%%c
)
set Feb=28
set /a Leap=yy %% 4
if "%Leap%"=="0" set Feb=29
set i=101
for %%a in (31 %Feb% 31 30 31 30 31 31 ott34 31 35 31) do (
  set /a i+=1
  if !i!==1%mm% set LastDay=%%a
)
echo This month's last Day: [%LastDay%]
if %dd%==%LastDay% echo Today is the last day of this month.

echo ***** start business day script *****

 

echo 0=Sunday - 1=Monday - 2=Tuesday - 3=Wednesday - 4=Thursday - 5=Friday - 6=Saturday

for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x
set day=%DayOfWeek%
echo ***** number day: %DayOfWeek% *****

set result=false

IF "%day%"=="6" set result=true
IF "%day%"=="0" set result=true

IF "%result%" == "true" (
    ECHO ***** is not a business day *****
) ELSE (
    ECHO ***** business day %LastDay% *****
)

REM if %dd%==%LastDay% call fri.bat

pause

Open in new window

0
Comment
Question by:Endri Azizi
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 41850594
So, can I clarify.  You want to check the CURRENT DATE and if this is the last business day of the month, then copy a file to be specified?

So if the script ran on July 29, 2016 you want the file to copy, but not on July 30, 2016 or July 31, 2016?

~bp
1
 

Author Comment

by:Endri Azizi
ID: 41851589
HI Bill,
I would like to copy the last business day's file of the month
for example:

business days
1=Monday
2=Tuesday
3=Wednesday
4=Thursday
5=Friday

not business days
6=Saturday
0=Sunday

(Monday 31 July 2016) is the last day of the month & is a business day ---  copy to  --- > a backup folder



(Saturday 31 December 2016) is the last day of the month BUT is not a business day  ---  don't copy to  --- > a backup folder

than check back the first business day but the last business day of the month
(Fryday 30 December 2016) is the last business day of the month ---  copy to  --- > a backup folder

I hope now is more clear

Thanks a lot!!!
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41852028
Okay, I used some routines I had and tried to comment enough that you might be able to learn and understand it.  The basic idea is:

  • Call a routine I use to get current date and time from WMIC and store into environment variables
  • Call a routine to calculate the number of days in the current month
  • Call a routine to determine if this is the last workday of the month

For the last item, the basic idea I used there was to assume we aren't at the last workday unless one of these is true:

  • If this is the last day of the month, and not a weekend day, then this is the last work day of the month.
  • If this is a Friday, and the month ends in the next two days, then this is the last work day of the month.

@echo off
setlocal

rem Get current date and time into LocalTime. environment variables (see comments in subroutine)
call :GetLocalTime N

rem Calculate days in the current month
call :DaysInMonth %LocalTime.Year% %LocalTime.Month%

rem Determine if this is the last workday of the month
call :IsLastWorkDay

rem ===== JUST FOR TESTING TO SHOW SOME OF THE VARIABLES INVOLVED =====
echo Year: %LocalTime.Year%
echo Month: %LocalTime.Month%
echo Day: %LocalTime.Day%
echo DayOfWeek: %LocalTime.DayOfWeek%
echo DaysInMonth: %DaysInMonth%
echo IsLastWorkDay: %IsLastWorkDay%

rem If last work day of month do desired processing
if "%IsLastWorkDay%" EQU "Y" (
  rem ===== TO BE ADDED, PERFORM WHATEVER LOGIC YOU WANT ON THE LAST WORKDAY =====
  rem call fri.bat
)

rem End main script
exit /b


rem Get current date and time via WMIC, return in variables (see below)
:GetLocalTime [zero-pad(Y/N)]
  rem Sets these variables for further reference ("*" items will be zero filled if requested)
  rem LocalTime.Year
  rem LocalTime.Month (*)
  rem LocalTime.Day (*)
  rem LocalTime.Hour (*)
  rem LocalTime.Minute (*)
  rem LocalTime.Second (*)
  rem LocalTime.DayOfWeek (0 to 6, where 0=Sunday)
  rem LocalTime.WeekInMonth
  rem LocalTime.Quarter

  rem Execute WMIC command to get date time elements
  for /f "tokens=*" %%A in ('wmic /output:stdout Path Win32_LocalTime Get * /Format:value^|find "="') do (
    rem pass output through another FOR to remove trailing carraige returns
    for /f "tokens=*" %%B in ("%%A") do (
      rem set values into variables prefaced by "LocalTime."
      set "LocalTime.%%B"
    )
  )

  rem If requested, left pad with zero common 2 digit values
  if /i "%~1" EQU "Y" (
    for %%a in (LocalTime.Month LocalTime.Day LocalTime.Hour LocalTime.Minute LocalTime.Second) do (
      if !%%a! LSS 10 set %%a=0!%%a!
    )
  )

  rem Return to caller
  exit /b

rem Subroutine to calculate days in current month, handling leap years
:DaysInMonth Year Month
  setlocal DisableDelayedExpansion
  set /a "yy = %~1, mm = %~2"
  set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
  endlocal & set DaysInMonth=%n%
  exit /b

rem Subroutine to determine if this is last work day of month
:IsLastWorkDay
  setlocal EnableDelayedExpansion

  rem Assume it isn't for now
  set Result=N

  rem If today is the last day of the month, and not a weekend day, then it is last workday
  if %LocalTime.Day% EQU %DaysInMonth% (
    if %LocalTime.DayOfWeek% NEQ 0 (
      if %LocalTime.DayOfWeek% NEQ 6 (
        set Result=Y
      )
    )
  ) else (
    rem Not last day of month, see if its a friday, and if the month ends over the weekend
    if %LocalTime.DayOfWeek% EQU 5 (
      set /a "DaysToEndOfMonth = DaysInMonth - LocalTime.Day"
      if !DaysToEndOfMonth! LEQ 2 set Result=Y
    )
  )

  rem Return result to caller in environment variable
  endlocal & set IsLastWorkDay=%Result%
  exit /b

Open in new window

~bp
1
 

Author Comment

by:Endri Azizi
ID: 41852188
Thanks a lot Bill

I would like to find the first business day at the and of the month...

Since I have to take a backup of the first business day at the end of the month and put it from the current folder into a backup folder.

Down below my batch script that I must add this new feature
DataBackupCompressorMain.bat.txt
DataBackupCompressorSingle.bat.txt
Immagine.png
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 41852199
So you are just looking for a way to determine what the first work day of the current month is?  Basically starting at day 1 of the current month and moving forward until a weekday of Monday through Friday is found?

~bp
1
 

Author Comment

by:Endri Azizi
ID: 41852267
Exactly, I'am looking for the last business day at the month.

for example:

October 2016:
Backup file of Monday 31 October 2016

November 2016
Backup file of Wednesday 30 November 2016

December 2016
Backup file of Friday 30 December 2016 because Saturday 31 December 2016 is not a business day

April 2017
Backup file of Friday 28 April 2016 because Sunday 30 December 2016 is not a business day

Thanks for your help
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 41852385
Now I'm a little confused.  You said "I would like to find the first business day at the and of the month" but now you are saying "I'am looking for the last business day at the month" so not sure what you are after.

The solution I provided and you accepted did what I thought you originally asked for, it determined if the current day was the last work day of the current month, so that a backup could be taken on that day.

Is there something else you are looking for?

~bp
0
 

Author Comment

by:Endri Azizi
ID: 41852416
I was confused too,

You are right

"I would like to find the first business day at the and of the month"

I hope you can help me to copy this file into backup folder
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 41852584
Okay, I've added some routines and logic to determine the first work day of the month, take a look at the updated solution.

@echo off
setlocal

rem Get current date and time into LocalTime. environment variables (see comments in subroutine)
call :GetLocalTime N

rem Calculate days in the current month
call :DaysInMonth %LocalTime.Year% %LocalTime.Month%

rem Determine if this is the last workday of the month
call :IsLastWorkDay

rem ===== JUST FOR TESTING TO SHOW SOME OF THE VARIABLES INVOLVED =====
echo Year: %LocalTime.Year%
echo Month: %LocalTime.Month%
echo Day: %LocalTime.Day%
echo DayOfWeek: %LocalTime.DayOfWeek%
echo DaysInMonth: %DaysInMonth%
echo IsLastWorkDay: %IsLastWorkDay%

rem If last work day of month do desired processing
if "%IsLastWorkDay%" EQU "Y" (
  rem ===== TO BE ADDED, PERFORM WHATEVER LOGIC YOU WANT ON THE LAST WORKDAY =====
  rem call fri.bat
)

rem Find out what the first work day was for this month
call :FirstWorkDay %LocalTime.Month% %LocalTime.Year%
echo FirstWorkDay: %LocalTime.Month%/%FirstWorkDay%/%LocalTime.Year%

rem End main script
exit /b


rem Get current date and time via WMIC, return in variables (see below)
:GetLocalTime [zero-pad(Y/N)]
  rem Sets these variables for further reference ("*" items will be zero filled if requested)
  rem LocalTime.Year
  rem LocalTime.Month (*)
  rem LocalTime.Day (*)
  rem LocalTime.Hour (*)
  rem LocalTime.Minute (*)
  rem LocalTime.Second (*)
  rem LocalTime.DayOfWeek (0 to 6, where 0=Sunday)
  rem LocalTime.WeekInMonth
  rem LocalTime.Quarter

  rem Execute WMIC command to get date time elements
  for /f "tokens=*" %%A in ('wmic /output:stdout Path Win32_LocalTime Get * /Format:value^|find "="') do (
    rem pass output through another FOR to remove trailing carraige returns
    for /f "tokens=*" %%B in ("%%A") do (
      rem set values into variables prefaced by "LocalTime."
      set "LocalTime.%%B"
    )
  )

  rem If requested, left pad with zero common 2 digit values
  if /i "%~1" EQU "Y" (
    for %%a in (LocalTime.Month LocalTime.Day LocalTime.Hour LocalTime.Minute LocalTime.Second) do (
      if !%%a! LSS 10 set %%a=0!%%a!
    )
  )

  rem Return to caller
  exit /b

rem Subroutine to calculate days in current month, handling leap years
:DaysInMonth Year Month
  setlocal DisableDelayedExpansion
  set /a "yy = %~1, mm = %~2"
  set /a "n = 30 + !(((mm & 9) + 6) %% 7) + !(mm ^ 2) * (!(yy %% 4) - !(yy %% 100) + !(yy %% 400) - 2)"
  endlocal & set DaysInMonth=%n%
  exit /b

rem Subroutine to determine if this is last work day of month
:IsLastWorkDay
  setlocal EnableDelayedExpansion

  rem Assume it isn't for now
  set Result=N

  rem If today is the last day of the month, and not a weekend day, then it is last workday
  if %LocalTime.Day% EQU %DaysInMonth% (
    if %LocalTime.DayOfWeek% NEQ 0 (
      if %LocalTime.DayOfWeek% NEQ 6 (
        set Result=Y
      )
    )
  ) else (
    rem Not last day of month, see if its a friday, and if the month ends over the weekend
    if %LocalTime.DayOfWeek% EQU 5 (
      set /a "DaysToEndOfMonth = DaysInMonth - LocalTime.Day"
      if !DaysToEndOfMonth! LEQ 2 set Result=Y
    )
  )

  rem Return result to caller in environment variable
  endlocal & set IsLastWorkDay=%Result%
  exit /b

rem Subroutine to determine first work day of a month
:FirstWorkDay [Month] [Year]
  setlocal EnableDelayedExpansion

  rem Check from day 1 to 7 to find first day that is not a weekend day
  for /l %%A in (1,1,7) do (
    call :DayOfWeek %~1 %%A %~2
    if !DayOfWeek! NEQ 6 (
      if !DayOfWeek! NEQ 0 (
        endlocal & set FirstWorkDay=%%A
        exit /b
      )
    )
  ) 

  rem Should never get here, return -1 just in case
  endlocal & set FirstWorkDay=-1
  exit /b

rem Calculate the day of week number (0=Sunday) for a specific date
:DayOfWeek [Month] [Day] [Year]
  setlocal

  set /a a=(14-%1)/12
  set /a y=%3-%a%
  set /a m=%1+12*%a%-2
  set /a dow=(%2+%y%+%y%/4-%y%/100+%y%/400+(31*%m%)/12)%%7

  endlocal & set DayOfWeek=%dow%
  exit /b

Open in new window

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

16 Experts available now in Live!

Get 1:1 Help Now