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

Author Topic: backup batch file to recognize day names  (Read 8272 times)

0 Members and 1 Guest are viewing this topic.

RonC

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 10
    backup batch file to recognize day names
    « on: April 26, 2018, 05:19:34 PM »
    Hello all;
    I am trying to write a backup batch file and I want the code to direct the backup to a specific directory based on the day of the week, Monday, Tuesday, etc.
    I believe the coding should look something like:

    if dayname = Monday then
    copy *.* f:\monday    /y
    else
    if dayname = Tuesday then
    copy *.* f:\tuesday  /y

    etc, etc for Monday through Friday with an end if at the end.

    Can anyone provide me with the proper nomenclature? I volunteer for a non-profit and I'm trying to get them to make sure and backup their files.

    Thanks in advance for all the help.

    Regards,
    Ronc

    RonC

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 10
      Re: backup batch file to recognize day names
      « Reply #1 on: April 26, 2018, 05:33:37 PM »
      I just read the readme file for this forum and need to add; I am using Windows10 and will create my bat file using Notepad then save it as a .bat file. The program will be very straightforward; change to the proper directory, etc then  use copy (or xcopy) to copy the files to the backup directory.

      Thanks again.
      RonC

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: backup batch file to recognize day names
      « Reply #2 on: April 27, 2018, 09:18:45 AM »
      Open up a cmd prompt and type: IF /?

      This will show you the syntax for the IF command.

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: backup batch file to recognize day names
      « Reply #3 on: April 27, 2018, 09:29:08 AM »
      I had syntax on my waffles this mornin...
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: backup batch file to recognize day names
      « Reply #4 on: April 27, 2018, 10:10:45 AM »
      You don't need else in batch to do this. End If and Then are not keywords in batch. IF tests can be one line.

      Variable names have percent signs before and after and it is often preferred to quote both sides of an IF test...

      The equality test operator is a double equals sign ==

      The IF test is case sensitive unless you use the /i switch. That is:

      if "%dayname%" is "Monday":
      if "%dayname%"=="monday" will FAIL
      if /i "%dayname%"=="monday" will PASS

      Code: [Select]
      if "%dayname%"=="Monday" copy *.* f:\monday /y
      if "%dayname%"=="Tuesday" copy *.* f:\tuesday /y
      ...etc...

      HOWEVER!!! If you have already have five dayname folders, and %dayname% is only ever one of the five days of the work week, why not just do this? This method does not care about case.

      copy *.* f:\%dayname% /y
      « Last Edit: April 27, 2018, 11:11:40 AM by Salmon Trout »

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: backup batch file to recognize day names
      « Reply #5 on: April 27, 2018, 10:34:08 AM »
       8)
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: backup batch file to recognize day names
      « Reply #6 on: April 27, 2018, 11:21:35 AM »
      Variable names have percent signs before and after

      Sometimes they have exclamation points instead !varname! but that is a more advanced topic.

      Salmon Trout

      • Guest
      Re: backup batch file to recognize day names
      « Reply #7 on: April 27, 2018, 11:23:15 AM »
      HOWEVER!!!
      Salmon's motto: think about what you want the script to do and then write the code. I have to say that I often fail to see a simpler way of doing something until I have already done it a complicated way.

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: backup batch file to recognize day names
      « Reply #8 on: April 27, 2018, 11:28:43 AM »
      I should make your motto a sticky for the DOS Forum....
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: backup batch file to recognize day names
      « Reply #9 on: April 27, 2018, 11:31:39 AM »
      I don't think there's anything wrong with thinking it out as you go along, but it's good to take a good look at your code and see if you can reduce the number of lines, as long as you can still remember what it does in 6 months time!

      certainly
      if %food%==bacon copy *.* d:\bacon
      if %food%==eggs copy *.* d:\eggs
      if %food%==ham copy *.* d:\ham

      can all be collapsed into

      copy *.* d:\%food%

      as long as there are exactly the same number of folders as there are possible foods, and the food variable value will always be one of them. There is a site called The Daily *censored* where contributors share code that actually made into production where the long way around was taken, especially, it seems with dates. Also things like if number=4 then value="four"...


      RonC

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Windows 10
        Re: backup batch file to recognize day names
        « Reply #10 on: April 27, 2018, 12:15:44 PM »
        A big thank you to Salmon Trout and Patio. I'm a real novice at DOS and only use it for backups so you have been a tremendous help.

        RonC

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: backup batch file to recognize day names
        « Reply #11 on: April 27, 2018, 12:17:39 PM »
        Keep in mind this is how we all learned...Salmon is a better student than i...
        " Anyone who goes to a psychiatrist should have his head examined. "

        RonC

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Windows 10
          Re: backup batch file to recognize day names
          « Reply #12 on: April 27, 2018, 12:18:52 PM »
          My problem has been solved, is there a way to close the thread and mark it as resolved? I've looked around and can't find the information.

          Thanks,

          RonC

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1769
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: backup batch file to recognize day names
          « Reply #13 on: April 27, 2018, 12:21:53 PM »
          It's fine as it stands...someone else may benefit from it.
          " Anyone who goes to a psychiatrist should have his head examined. "

          Salmon Trout

          • Guest
          Re: backup batch file to recognize day names
          « Reply #14 on: April 27, 2018, 12:53:34 PM »
          Also things like if number=4 then value="four"...

          I meant where you see

          if number=1 then value="one"
          if number=2 then value="two"
          if number=3 then value="three"
          (etc)

          and some time later...

          if value="one" then newnumber=1
          if value="two" then newnumber=2
          if value="three" then newnumber=3
          (etc)

          I have done this sort of thing myself.


          RonC

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Windows 10
            Re: backup batch file to recognize day names
            « Reply #15 on: April 28, 2018, 10:19:37 AM »
            Sorry people, I may have screwed up and my celebration was premature. I tried the coding you suggested and it didn't work. Here is the code for my bat file:
            @echo off
            cd\
            cd\Users\RONALD\Desktop
            if /i "%dayname%"=="Monday" copy current*.* f:\monday /y
            if /i "%dayname%"=="Tuesday" copy current*.* f:\tuesday /y
            if /i "%dayname%"=="Wednesday" copy current*.* f:\wednesday /y
            if  /i"%dayname%"=="Thursday" copy current*.* f:\thursday /y
            if /i "%dayname% =="Friday" copy current*.* f:\friday /y
            if /i "%dayname% =="saturday" copy current.* f:\saturday /y

            When I run the bat file and then open the directory f:\saturday it is empty. Am I not properly identifying a variable? I can't figure it out.

            Thanks,

            RonC



            patio

            • Moderator


            • Genius
            • Maud' Dib
            • Thanked: 1769
              • Yes
            • Experience: Beginner
            • OS: Windows 7
            Re: backup batch file to recognize day names
            « Reply #16 on: April 28, 2018, 11:42:28 AM »
            Small S in Saturday ? ?
            " Anyone who goes to a psychiatrist should have his head examined. "

            RonC

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Windows 10
              Re: backup batch file to recognize day names
              « Reply #17 on: April 28, 2018, 12:04:07 PM »
              No, that didn't do it. The directory I'm copying too is small s (saturday) and I've also tried it with the /I but it still didn't work.  The code below works fine:
              cd\
              cd\Users\RONALD\Desktop
              copy current*.* f:\saturday /y


              Thanks for the suggestion.

              RonC


              Salmon Trout

              • Guest
              Re: backup batch file to recognize day names
              « Reply #18 on: April 28, 2018, 12:23:34 PM »
              RonC, how does the batch script know what day it is? That is, how does the variable %dayname% get a value? I see nothing in your script that does that.


              Salmon Trout

              • Guest
              Re: backup batch file to recognize day names
              « Reply #19 on: April 28, 2018, 12:27:26 PM »
              Gone quiet...
              « Last Edit: April 28, 2018, 01:19:07 PM by Salmon Trout »

              Salmon Trout

              • Guest
              Re: backup batch file to recognize day names
              « Reply #20 on: April 28, 2018, 02:36:42 PM »
              If anyone is interested, this will do it...

              Code: [Select]
              @echo off
              echo wscript.echo weekdayname(weekday(now)) > "%temp%\wdn.vbs"
              for /f "delims=" %%A in ('cscript //nologo "%temp%\wdn.vbs"') do set dayname=%%A
              del "%temp%\wdn.vbs"
              REM this line is optional:
              echo Today is %dayname%
              Code: [Select]
              C:\Batch\Test>dayname.bat
              Today is Saturday



              Salmon Trout

              • Guest
              Re: backup batch file to recognize day names
              « Reply #21 on: April 28, 2018, 02:40:45 PM »
              The code below works fine:
              cd\
              cd\Users\RONALD\Desktop
              copy current*.* f:\saturday /y

              That's because it doesn't depend on the script knowing what day it is, which it will not be able to magically do.

              patio

              • Moderator


              • Genius
              • Maud' Dib
              • Thanked: 1769
                • Yes
              • Experience: Beginner
              • OS: Windows 7
              Re: backup batch file to recognize day names
              « Reply #22 on: April 28, 2018, 03:17:10 PM »
              What day is it ? ?.... :P
              " Anyone who goes to a psychiatrist should have his head examined. "

              RonC

                Topic Starter


                Greenhorn

                • Experience: Beginner
                • OS: Windows 10
                Re: backup batch file to recognize day names
                « Reply #23 on: April 28, 2018, 03:29:34 PM »
                Thanks guys. I'll give it a go and let you know.

                RonC

                Salmon Trout

                • Guest
                Re: backup batch file to recognize day names
                « Reply #24 on: April 28, 2018, 03:32:16 PM »
                How did you think the script was going to know the day of the week? Curious to know.

                RonC

                  Topic Starter


                  Greenhorn

                  • Experience: Beginner
                  • OS: Windows 10
                  Re: backup batch file to recognize day names
                  « Reply #25 on: April 28, 2018, 04:25:52 PM »
                  Well guys, it worked for "Saturday". I'm going to set my alarm for 12:01am to verify that it works for "sunday".
                   ;D

                  Thanks,

                  RonC

                  Salmon Trout

                  • Guest
                  Re: backup batch file to recognize day names
                  « Reply #26 on: April 28, 2018, 05:20:25 PM »
                  How did you think the script was going to know the day of the week? Curious to know.

                  RonC

                    Topic Starter


                    Greenhorn

                    • Experience: Beginner
                    • OS: Windows 10
                    Re: backup batch file to recognize day names
                    « Reply #27 on: April 29, 2018, 12:11:11 PM »
                    Salmon Trout; the code you provided works perfectly. Thank you so much (and a big thank you to Patio and others for their comments also). Just so you know; I do volunteer work for the International Rescue Committee , a non-profit organization founded through the work of Albert Einstein, which responds to the world’s worst humanitarian crises and helps people whose lives and livelihoods are shattered by conflict and disaster to survive, recover, and gain control of their future. I'm retired and spend a lot of time writing small computer programs for them, primarily for administration support, using Microsoft Access. I use this and other forums for guidance and the availability and willingness of people like you all to provide that support has been a godsend.

                    Thanks again.

                    RonC

                    patio

                    • Moderator


                    • Genius
                    • Maud' Dib
                    • Thanked: 1769
                      • Yes
                    • Experience: Beginner
                    • OS: Windows 7
                    Re: backup batch file to recognize day names
                    « Reply #28 on: April 29, 2018, 02:33:28 PM »
                    Thank You for volunteering...
                    " Anyone who goes to a psychiatrist should have his head examined. "

                    Salmon Trout

                    • Guest
                    Re: backup batch file to recognize day names
                    « Reply #29 on: April 29, 2018, 03:30:06 PM »
                    It's good to know that there are still people like that.

                    Squashman



                      Specialist
                    • Thanked: 134
                    • Experience: Experienced
                    • OS: Other
                    Re: backup batch file to recognize day names
                    « Reply #30 on: April 30, 2018, 08:18:21 AM »
                    Sorry people, I may have screwed up and my celebration was premature. I tried the coding you suggested and it didn't work. Here is the code for my bat file:
                    @echo off
                    cd\
                    cd\Users\RONALD\Desktop
                    if /i "%dayname%"=="Monday" copy current*.* f:\monday /y
                    if /i "%dayname%"=="Tuesday" copy current*.* f:\tuesday /y
                    if /i "%dayname%"=="Wednesday" copy current*.* f:\wednesday /y
                    if  /i"%dayname%"=="Thursday" copy current*.* f:\thursday /y
                    if /i "%dayname% =="Friday" copy current*.* f:\friday /y
                    if /i "%dayname% =="saturday" copy current.* f:\saturday /y

                    When I run the bat file and then open the directory f:\saturday it is empty. Am I not properly identifying a variable? I can't figure it out.

                    Thanks,

                    RonC
                    This code should have given you an error.  You are missing a double quote for the comparison on Friday and Saturday.

                    patio

                    • Moderator


                    • Genius
                    • Maud' Dib
                    • Thanked: 1769
                      • Yes
                    • Experience: Beginner
                    • OS: Windows 7
                    Re: backup batch file to recognize day names
                    « Reply #31 on: April 30, 2018, 08:59:36 AM »
                     8)
                    " Anyone who goes to a psychiatrist should have his head examined. "