Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to make backup of a folder based on time and day of week  (Read 22644 times)

0 Members and 1 Guest are viewing this topic.

bhulku123

    Topic Starter


    Rookie

    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.

    Dusty



      Egghead

    • I could if she would, but she won't so I don't.
    • Thanked: 75
    • Experience: Beginner
    • OS: Windows XP
    Re: Batch file to make backup of a folder based on time and day of week
    « Reply #1 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

    One good deed is worth more than a year of good intentions.

    bhulku123

      Topic Starter


      Rookie

      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #2 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?


      Dias de verano

      • Guest
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #3 on: December 23, 2008, 03:07:45 AM »
      Dusty meant

      echo %date%



      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #4 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)
      One good deed is worth more than a year of good intentions.

      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #5 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.

       
      « Last Edit: December 24, 2008, 02:53:48 PM by Dusty »
      One good deed is worth more than a year of good intentions.

      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #6 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.
      One good deed is worth more than a year of good intentions.

      Dias de verano

      • Guest
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #7 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.

      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #8 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.
      « Last Edit: December 24, 2008, 06:06:46 PM by Dusty »
      One good deed is worth more than a year of good intentions.

      Dias de verano

      • Guest
      Re: Batch file to make backup of a folder based on time and day of week
      « Reply #9 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


      bhulku123

        Topic Starter


        Rookie

        Re: Batch file to make backup of a folder based on time and day of week
        « Reply #10 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

        Dusty



          Egghead

        • I could if she would, but she won't so I don't.
        • Thanked: 75
        • Experience: Beginner
        • OS: Windows XP
        Re: Batch file to make backup of a folder based on time and day of week
        « Reply #11 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.
        « Last Edit: December 29, 2008, 01:08:42 AM by Dusty »
        One good deed is worth more than a year of good intentions.

        bhulku123

          Topic Starter


          Rookie

          Re: Batch file to make backup of a folder based on time and day of week
          « Reply #12 on: December 27, 2008, 05:05:38 PM »
          Thank you Dusty,
          I will keep you guys posted with the result on Monday.

          Again Thanks.

          bhulku123

            Topic Starter


            Rookie

            Re: Batch file to make backup of a folder based on time and day of week
            « Reply #13 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.

            Dusty



              Egghead

            • I could if she would, but she won't so I don't.
            • Thanked: 75
            • Experience: Beginner
            • OS: Windows XP
            Re: Batch file to make backup of a folder based on time and day of week
            « Reply #14 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 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.


            One good deed is worth more than a year of good intentions.