Solved

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

Posted on 2016-10-19
9
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 54

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 54

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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
 
LVL 54

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 54

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 54

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to find then copy to anotehr file 3 62
Robycopy Copy Data \ Disk to Disk (new\changed data) 19 143
Windows updates batch files 7 103
powershell script 9 74
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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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