Computer Hope

Microsoft => Microsoft DOS => Topic started by: bhulku123 on December 21, 2008, 07:26:02 PM

Title: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 21, 2008, 07:26:02 PM
I am trying to write a batch file which will do following.

When it is opened (manually, i do not want to have it run automatically) or double clicked on it from desktop shortcut:

1. It will exit a program called CheckInn first, which is on drive C and directory CheckInn\checkInn.exe

2. It will flash the words like " Please wait for Backup to complete" and do all the copying in background.

3. It will make the directory on Drive E (external USB 160gb Hard drive) named "CheckInn_Sun_A" based on Sunday (Sun) and A for first shift (between 4.01am to 2pm, B for second shift, from 2.01pm to 10pm and C for third shift, from 10.01pm to 4am Monday morning), If the day is Monday (after 4.01am) it will say CheckInn_Mon_A or CheckInn_Mon_B for second shift etc.

4. The CheckInn directory had sub directory also and only some files which are changed or created new since last copy need to be added to the earlier copy so it is not taking too much time to finish.

5. It will say " Backup Done! CheckInn is starting now"

6. It will restart the Checkinn.exe from Drice C:\CheckInn\

7. Close the Command window.

Here is what I did so far, (I do not know how to insert SUN or A as per the day and time so i asked to inupt manually) too much about XP commands but know limited DOS commands which i learned 15 years ago.

::
@echo off

C:\kill /f checkInn.exe

echo What day is today? Type the corresponding number and hit Enter.
echo Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6 or Sat=7

set /p D=
go to %D%

:1
echo what shift you are working?
echo Type A for First Shift, B for Second and C for Third Shift and hit Enter.

set /p S=
goto %S%

:a
:A

echo Please wait for Backup to complete. Thank you.

@echo off
E:\
mkdir CheckInn_Sun_A
xcopy c:\CheckInn\*.* E:\CheckInn_SUN_A /S /E /M /H /Q

cls
echo Backup Done! CheckInn is starting now.

start c:\CheckInn\CheckInn.exe


.........Same way it will be for B and C shift and same sequence for rest of days.


Can somebody help with writing the batch file for XP to name directory based on time and Day of week so no manual input is needed and it overwrite the changed files and add new as needed.

Appreciate all your help.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 22, 2008, 12:43:32 AM
Welcome to the CH forums.

Need to know which version of XP you are using and the format of the date returned by %date% or date /t

Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 22, 2008, 05:20:27 PM
Thanks for the reply.
I am using Xp Pro.
I don't know what you mean by "format of the date returned by %date% or date /t"
when i type %date% at command prompt it says " 'Mon' is not reconized as an internal or external command, operable program or batch file.

When I type date /t it says Mon 12/22/2008, so I think you mean this format, right?

Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dias de verano on December 23, 2008, 03:07:45 AM
Dusty meant

echo %date%


Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 23, 2008, 05:33:00 PM
Thank you Dias de V, that was my bad in assuming the OP would have known about the Echo!!

Please feel free to drop into this thread any time to correct my error(s) or omission(s)
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 23, 2008, 06:04:42 PM
bhulku123.  The code below sets Environment Variables which can be used when Xcopying the files you want to backup.  I have tested it briefly on my pc, now the major testing is over to you.  You should confirm that the displayed output is what is expected at various times, especially with respect to the Shift Identity letters.  The code covers only item 3. in your posting without creating output on drive E:

When you are satisfied that the results are what you want we can then go on to try to add the requirements of the other items.

There are some lines in the code which are possibly not required, or which could be changed for better performance, but I've left them in to make the code fairly simple.

If you find an error in the displayed output please post the entire output.

Code: [Select]
::  Checkinn.bat
::  With thanks to Dias De Verano for his assistance.

@echo off & setlocal enabledelayedexpansion
cls

:: Create/run vbs file (extracts date components) & set variables..

set vbsfile=%temp%\newdate.vbs
echo Newdate = (Date())>%vbsfile%
echo Yyyy = DatePart("YYYY", Newdate)>>%vbsfile%
echo   Mm = DatePart("M"   , Newdate)>>%vbsfile%
echo   Dd = DatePart("D"   , Newdate)>>%vbsfile%
echo   Wd = DatePart("WW"  , Newdate)>>%vbsfile%
echo   Wn = DatePart("Y"   , Newdate)>>%vbsfile%
echo   Ww = datepart("W"   , Newdate)>>%vbsfile%

echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd^&" "^&Wd^&" "^&Ww^&" "^&Wn>>%vbsfile%

FOR /F "tokens=1-6 delims= " %%A in ('cscript //nologo %vbsfile%') do (
        set weekday#=%%E
)
del %vbsfile% & set vbsfile=


for /f "tokens=1-3 delims=: " %%A in ('time/t') do (
    set hour=%%A
    set mins=%%B
    set ampm=%%C
)
set hourmins=%hour%%mins%

:   Set shift identifier based on time of day:

set shift=A

if "%ampm%"=="PM" if %hourmins% gtr 0200 if %hourmins% lss 1001 (
   set shift=B & goto next
)

if "%ampm%"=="PM" if %hourmins% gtr 1000 if %hourmins% lss 1201 (
   set shift=C & goto next
)

if "%ampm%"=="AM" if %hourmins% lss 0401 (
   set shift=C
   set /a weekday# -=1
   if !weekday#! lss 1 set weekday#=7 & goto next
)

if "%ampm%"=="AM" if %hourmins% gtr 1159 (
   set shift=C
   set /a weekday# -=1
   if !weekday#! lss 1 set weekday#=7
)

:next

::  Set alpha day from the week day number:

for /f "tokens=%weekday#%" %%a in ("Sun Mon Tues Wed Thu Fri Sat") do (
    set alfaday=%%a
)

::  Environment Variables set are:
::  %weekday#% = Day number within week (range 1 thru' 7, Sun is day #1)
::  %alfaday%  = Alpha day (range Sun thru' Sat)
::  %hour%     = Hour of the day
::  %mins%     = Minutes of the hour
::  %ampm%     = AM or PM indicator
::  %shift%    = Shift identity letter ( range A thru C)

::  Display for checking purposes:
echo.&echo.&echo.&echo.&echo.&echo.&echo.
echo                        Today's date is %date%
echo                        Current time is %hour%:%mins% %ampm%
echo.
echo                        Day number of week = %weekday#%   Alpha day = %alfaday%
echo.
echo                        AM or PM = %ampm%
echo.
echo                        Hour plus mins = %hourmins%   Shift identifier = %shift%
echo.&echo.
echo                        After checking the displayed output press any key
echo                        to continue...
set shift=
echo.&echo.&echo.&echo.&echo.&echo.&echo.
pause > nul
cls

endlocal
exit /b


Good luck.

 
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 24, 2008, 04:18:05 AM
Coding error found - when time passes midnight time /t returns 12:01 not 00:01 as expected.  Amended code posted.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dias de verano on December 24, 2008, 06:41:49 AM
Coding error found - when time passes midnight time /t returns 12:01 not 00:01 as expected.  Amended code posted.

depends on your locale.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 24, 2008, 03:15:00 PM
Thank you D de v.  Such problems would be more unlikely to occur if everyone used the 24-hour clock system ;D ;D

I found another ??? in that should this IF statement be used:

Quote
if "%ampm%"=="AM" if %hourmins% lss 401 (

When %ampm% is AM and %hourmins% is a four-digit number beginning with 0 (zero) so that the line would be expanded to say:

Quote
if AM == AM if 0935 lss 401 (

TRUE is returned by both IFs.   My cure was to add a zero to 401 and that appears to work fine.

It's well into Xmas day in my locale, a couple of hours to go before your witching hour.  Merry Xmas.

D.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dias de verano on December 24, 2008, 05:09:55 PM
Thank you D de v.  Such problems would be more unlikely to occur if everyone used the 24-hour clock system ;D ;D


A convenient way to avoid the problem has recently appeared...

http://www.computerhope.com/forum/index.php/topic,72945.0.html

Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 27, 2008, 08:38:19 AM
Thank you all for great help. I am little confused because i do not know much about windows batch file commands, can somebody please shed some more light may be add an backup example in the above batch file.

Please.

Thank You
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 27, 2008, 03:41:15 PM
I take it that the testing results were ok so here's the completed batch script which hopefully will do all you want.  All testing must be done by you, get back if you have any queries or problems:

Code: [Select]
::  Checkinn.bat
::  With thanks to Dias De Verano for his assistance.

@echo off
cls

taskkill /im CheckInn.exe

echo.&echo.&echo.&echo.&echo.&echo.&echo.
echo                    PLEASE WAIT FOR BACKUP TO COMPLETE...
sleep 3

setlocal enabledelayedexpansion


:: Create/run vbs file (extracts date components) & set variables..

set vbsfile=%temp%\newdate.vbs
echo Newdate = (Date())>%vbsfile%
echo Yyyy = DatePart("YYYY", Newdate)>>%vbsfile%
echo   Mm = DatePart("M"   , Newdate)>>%vbsfile%
echo   Dd = DatePart("D"   , Newdate)>>%vbsfile%
echo   Wd = DatePart("WW"  , Newdate)>>%vbsfile%
echo   Wn = DatePart("Y"   , Newdate)>>%vbsfile%
echo   Ww = datepart("W"   , Newdate)>>%vbsfile%

echo Wscript.Echo Yyyy^&" "^&Mm^&" "^&Dd^&" "^&Wd^&" "^&Ww^&" "^&Wn>>%vbsfile%

FOR /F "tokens=1-6 delims= " %%A in ('cscript //nologo %vbsfile%') do (
        set weekday#=%%E
)
del %vbsfile% & set vbsfile=


for /f "tokens=1-3 delims=: " %%A in ('time/t') do (
    set hour=%%A
    set mins=%%B
    set ampm=%%C
)
set hourmins=%hour%%mins%

:   Set shift identifier based on time of day:

set shift=A

if "%ampm%"=="PM" if %hourmins% gtr 0200 if %hourmins% lss 1001 (
   set shift=B & goto next
)

if "%ampm%"=="PM" if %hourmins% gtr 1000 if %hourmins% lss 1201 (
   set shift=C & goto next
)

if "%ampm%"=="AM" if %hourmins% lss 0401 (
   set shift=C
   set /a weekday# -=1
   if !weekday#! lss 1 set weekday#=7 & goto next
)

if "%ampm%"=="AM" if %hourmins% gtr 1159 (
   set shift=C
   set /a weekday# -=1
   if !weekday#! lss 1 set weekday#=7
)

:next

::  Set alpha day from the week day number:

for /f "tokens=%weekday#%" %%a in ("Sun Mon Tues Wed Thu Fri Sat") do (
    set alfaday=%%a
)

::  Environment Variables set are:
::  %weekday#% = Day number within week (range 1 thru' 7, Sun is day #1)
::  %alfaday%  = Alpha day (range Sun thru' Sat)
::  %hour%     = Hour of the day
::  %mins%     = Minutes of the hour
::  %ampm%     = AM or PM indicator
::  %shift%    = Shift identity letter ( range A thru C)

:: Xcopy files for backup.

set outpath=e:\checkinn_%alfaday%_%shift%

if not exist %outpath% md %outpath%

xcopy c:\checkinn\*.* %outpath% /s /e /m /h /q > nul

endlocal

cls

echo.&echo.&echo.&echo.&echo.&echo.&echo.
echo                   BACKUP DONE - CHECKIN IS STARTING NOW...
sleep 3
start c:\checkinn\checkinn.exe

exit

Good luck.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 27, 2008, 05:05:38 PM
Thank you Dusty,
I will keep you guys posted with the result on Monday.

Again Thanks.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 29, 2008, 02:54:45 PM
I tried to run this script and came up with couple stops during it. 

It said the Taskill and sleep are not a valid functions.

When i removed them, it worked fine (I had to exit the CheckInn manually first) at 11am. But I don't think the assingment of naming folder as Shift B worked when i tried to run the script again after 2pm today. I will have to check it out tomorrow again.

Is there an timer function to run this script say every 6 hours by itself?

Thank you again for all the help.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 29, 2008, 03:55:03 PM

It said the Taskill and sleep are not a valid functions.

When i removed them, it worked fine (I had to exit the CheckInn manually first) at 11am. But I don't think the assingment of naming folder as Shift B worked when i tried to run the script again after 2pm today. I will have to check it out tomorrow again.


Most unusual, Taskkill.exe ships with XP Pro but note the two kk's, you mention Taskill not Taskkill.  Please search for Taskkill.exe on your system and if it's there ensure that it's in your Path.   If not found you can download it from here (http://members.ziggo.nl/gigajosh/2005/05/taskkillexe.html) and Unzip it to somewhere in your Path, I suggest the C:\Windows\System32 folder.

As for Sleep, I overlooked that it is part of the Resource Kit which you probably have not installed.   Instead of Sleep 3 use "ping -n 4 127.0.0.1 > nul" (without the " ") in both cases.

Naming a folder as Checkinn_day_B works perfectly for me so far.  I have continued to test the script and the directory listing of folders created so far shows and expected files were copied.

Quote
Volume in drive E is Slave E
 Volume Serial Number is EEEE-EEEE

 Directory of e:\

Thu 12/25/2008  11:09 PM    <DIR>          checkinn_Fri_A
Fri 12/26/2008  02:56 PM    <DIR>          checkinn_Fri_B
Mon 12/29/2008  09:23 AM    <DIR>          checkinn_Mon_A
Mon 12/29/2008  09:27 PM    <DIR>          checkinn_Mon_B
Mon 12/29/2008  10:02 PM    <DIR>          checkinn_Mon_C
Sun 12/28/2008  10:38 AM    <DIR>          checkinn_Sun_A
Sun 12/28/2008  02:44 PM    <DIR>          checkinn_Sun_B
Thu 12/25/2008  01:46 PM    <DIR>          checkinn_Thu_A
Thu 12/25/2008  11:09 PM    <DIR>          checkinn_Thu_C
Tue 12/30/2008  11:03 AM    <DIR>          checkinn_Tue_A
               0 File(s)              0 bytes
              10 Dir(s)  27,937,628,160 bytes free

When posting about an error message please be specific about what the message is, please post the actual message(s).  For Taskill I can generate the message "Taskill" is not recognised as an internal or external command, operable program or batchfile, but have been unable to generate "Taskill" is not a valid function.  The two have entirely different meanings.

We can look at automatically scheduling the script when you are satisfied that is is producing the results you want.


Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 29, 2008, 04:25:33 PM
Thank You Dusty, I tried the script at work and i do not know exactly was the wording of the error was, so sorry for the confusion but the taskkill wpont work because CHeckInn need to exit by pressing Exit on its menu screen so i found a program called Eventcoder and i recorded the mouse movement to click Exit and I added the path in the script to run where the Taskkill command line use to be and it worked perfectly. So i deleted the sleep command lines and the whole script ran perfectly.

But I will make sure it works as intended and I will post the result tomorrow for you.

But Thank You again for your kind help.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 30, 2008, 05:58:59 PM
Hello Dusty, The script worked great it did made and copied all files to correct folder also even at3.58am last night which it recorded to MON_C shift folder.

Thanks DUsty again. I wanted to add couple things if possible,

1) If the Batchfile can be run as a startup batch file and if it can repeate doing backup every say 3 hours by itself.

2) If there can be option showup which says If you are ready to Backup Press Y and Hit ENTER to continue doing Backup or Press X to wait for 5 minutes.
 
In option 2 if the backup need to be delayed by 5 minutes can it go to end and restart after 5 minutes in case the CheckInn program is being used. In this case the next back still runs on time e.g. 3pm, 6pm, 9pm, 12pm etc.

Can there be a command added to enlarge fonts and may be color the lines seperately.


Thanks you.

Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on December 30, 2008, 07:43:05 PM
1.  Copy the .bat file to "C:\DOCUMENTS AND SETTINGS\USERNAME\START MENU\PROGRAMS\STARTUP\".  Username is the name of the account which starts on your system, it could be Administrator or some other name.  You may also want to delete the commands you have added to kill Checkinn.exe if it has not already been started.  You don't need me to schedule when the .bat file will run, see the XP Task Scheduler here (http://support.microsoft.com/kb/308569)..

2.  Will respond later.

The use of the COLOR command is shown here (http://www.robvanderwoude.com/ntcolor.html).. or enter COLOR/? at the command prompt.  The only way I know of increasing/decreasing the font size in Cmd.exe is to R.Click the top bar, select Properties>Font and change the font size in the font size slider.  You'd have to make this setting apply to all output in the Cmd.exe window.



Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on December 31, 2008, 08:05:46 AM
Thank you Dusty,

I will schedule with Xp Task Scheduler ans report the results.

Thank you again.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on January 01, 2009, 06:43:28 PM
Unfortunately  I have been unable to locate a timing feature in batch scripting so all timing must be done using the Task Scheduler in order to do what you want.

Make the start of the Checkinn.bat file look like this:
Quote
@echo off
cls
setlocal enabledelayedexpansion


for /f "delims=*" %%1 in (runit.dat) do (
    set runit=%%1
)
if "%runit%"=="No" exit

echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.&echo.
color 1c
set/p run=            CAN BACKUP BE RUN NOW? (Y/N)...   

cls

if "%run%"=="y" call :start

if "%run%"=="Y" call :start
 
exit

:start

::  Checkinn.bat

and the end of the script to this:
Quote
echo No>runit.dat
exit

Create the file C:\runit.dat containing just the word "Yes" without the quotes.

Create the following script and save it as filename.bat:
Quote
@echo off
cls
echo Yes>runit.dat

Task scheduling Suggestions.
===================
Schedule the main script to run at the times you want, 3 6 9 and 12 pm (note that according to the specs I extracted from your post, these times to not include any backup in Shift A which I have noted as being 4 am to 2pm daily, please confirm).  Also schedule the script to start 5 mins after the above times.

This will allow the script to run at the appointed hours and at five mins after those times if backup does not proceed.

Schedule filename.bat to run at say 3.20 6.20 9.20 and 12.20 pm


 
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: bhulku123 on January 07, 2009, 04:17:14 PM
Thanks Dusty, The Backup is running good so far except i have not added the last post suggestions yet, I wanted to try it first. I a  wondering is there a way to get out of screensaver when the batch file starts or say play a music file in wma format so that to bring attention of the person working?

I will add script from your last post tomorrow and let you know how it goes.

Thanks Again.
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: him6182 on September 24, 2009, 04:55:13 AM
Hi i m new to batch file can any one plz tell me the batch file making process from begning.
will appericiate for helping me out.
regds
him
Title: Re: Batch file to make backup of a folder based on time and day of week
Post by: Dusty on September 25, 2009, 02:17:10 AM
Him6182 - Welcome to the CH forums.

Please do NOT hijack another member's thread, start your own.

Batch scripting info can be found here (http://www.computerhope.com/batch.htm) and there a many many on-line tutorials.

Good luck