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 8611 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.