Solved

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

Posted on 2016-10-19
9
48 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 51

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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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 51

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 51

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

12 Experts available now in Live!

Get 1:1 Help Now