XCOPY in batch file to create new folder


  1. Posts : 5
    Windows 7 Pro
       #1

    XCOPY in batch file to create new folder


    Hello
    I’ve been using the “xcopy” command in a batch file to backup a folder & all contents inside. I use task schedule to back up the folder files each day and then it backups what has changed from the previous backup.
    Example:
    XCOPY "E:\Stuff" "J:\Stuff" /s /f /y /c /d /I


    Now I need to backup these files each day to a new folder so I can retain the data in the previous folder.
    For example if I wanted to backup
    XCOPY "E:\Stuff" "J:\Stuff" /s /f /y /c /d
    and I wanted to retain the data located in J:\Stuff folder during the next backup.
    How would I configure the batch file to create a new folder the next time the backup starts.
    For example J:\Stuff_1 and the next day create folder J:\Stuff_2 and so forth

    Thanks for helping
      My Computer


  2. Posts : 7,607
    Windows 10 Home 20H2
       #2

    You may add a date after "Stuff", e.g. "Stuff-2022-03-05".
      My Computer


  3. Posts : 5
    Windows 7 Pro
    Thread Starter
       #3

    Would I do that after the backup or inside the batch file?
      My Computer


  4. Posts : 7,607
    Windows 10 Home 20H2
       #4

    In the CMD/batch file...
    Code:
    @echo off
    For /F "tokens=*" %%# in ('"PowerShell Get-Date -format '"yyyy-MM-dd a\t HH-mm-ss"'"') DO (
    MD "J:\Stuff (%%#)"&XCOPY "E:\Stuff" "J:\Stuff (%%#)" /s /f /y /c /d)
    Pause
    You may remove a\t HH-mm-ss if you don't need the exact time in the folder name.
      My Computer


  5. Posts : 31,989
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #5

    Here's a simpler option. It creates a date number in the format appropriate for your region, mmddyyyy if your regional format is US (or ddmmyyyy if it's UK like mine).

    xcopy C:\temp\*.* /s C:\Stuff_%date:/=%\

    This is it running from a batch file....

    XCOPY in batch file to create new folder-image.png

    ...and here's the result.

    XCOPY in batch file to create new folder-image.png
      My Computers


  6. Posts : 7,607
    Windows 10 Home 20H2
       #6

    The date format does not look good in your simpler option.
      My Computer


  7. Posts : 31,989
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #7

    Matthew Wai said:
    The date format does not look good in your simpler option.
    True, but there's a lot less to type (or get wrong) - just one line, no 'for' loop or Powershell needed
      My Computers


  8. Posts : 7,607
    Windows 10 Home 20H2
       #8

    You don't have to type. Just copy and paste.
      My Computer


  9. Posts : 5
    Windows 7 Pro
    Thread Starter
       #9

    The batch file works great!
    Thanks for all the help and guidance
    I appreciate it
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:34.
Find Us




Windows 10 Forums