Link to home
Start Free TrialLog in
Avatar of Endri Azizi
Endri Azizi

asked on

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

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

Avatar of Bill Prew
Bill Prew

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
Avatar of Endri Azizi

ASKER

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!!!
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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
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
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
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