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

Author Topic: Using xcopy in batch file for backup in Win 7 & Win 10  (Read 2968 times)

0 Members and 1 Guest are viewing this topic.

KeithGeorge

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 10
    Using xcopy in batch file for backup in Win 7 & Win 10
    « on: August 15, 2018, 04:28:51 AM »
    Good Morning,

    I have been using xcopy in a batch file now for several years without any problems.

    The command I am using is:- xcopy/y  "S:\Documents"  "G:\Documents"   /e /d

    What I would like to do, when I run the batch on a Monday, Wednesday or Friday then
      xcopy/y "S:\Documents"  "G:\DocumentsMWF"   /e /d        and on the other days of the week it should be
      xcopy/y  "S:\Documents"  "G:\DocumentsTTSS"   /e /d

    So I end up with 2 copies, one a day older than the other.

    nil

    • Global Moderator


    • Intermediate
    • Thanked: 15
      • Experience: Experienced
      • OS: Linux variant
      Re: Using xcopy in batch file for backup in Win 7 & Win 10
      « Reply #1 on: August 17, 2018, 07:36:53 AM »
      Windows has a %DATE% environment variable, but it is notoriously unreliable because the format can change based on registry settings

      Instead, you can get the day of the week as a number using wmic. You can extract the number from the wmic output using for /f and findstr.

      From the command line:

      Code: [Select]
      for /f "skip=1" %a in ('wmic path win32_localtime get dayofweek ^| findstr /r /v "^$"') do @set DOW=%a


      The number is from 0-6 (0 for sunday, 6 for saturday).

      To use this in a batch script, just replace %a with %%a.

      whats going on here -

      • for /f operates on all lines of a command's output, in this case everything single quoted in the parentheses
      • "skip=1" tells for /f to ignore the first line of the output (wmic outputs "DayOfWeek" on the first line)
      • To exclude blank lines in the wmic output from being processed by for /f, we pipe (^|) the wmic command to findstr, which uses a regular expression (/r) and excludes all matches (/v). The regular expression is "^$", which matches any line containing exactly "beginning of line, end of line, and nothing in between". This will match any blank line and exclude it.

      more info -

      for: https://www.computerhope.com/forhlp.htm
      wmic: https://www.computerhope.com/wmic.htm
      findstr: https://www.computerhope.com/findstr.htm
      regular expressions: https://www.computerhope.com/unix/regex-quickref.htm

      hope this helps
      Do not communicate by sharing memory; instead, share memory by communicating.

      --Effective Go