Page 1 of 1

Xcopy batch file that creates new folder

Posted: 03 Mar 2022 23:12
by Ones_Zeros
Hello
I’ve been using the “xcopy” command in a batch file to backup the contents in a folder. I use task schedule to back up these files each day and backup what has changed from the previous backup.

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
C:\data to D:\data and I wanted to retain the data located in d:\data 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 c:\data to d:\data_1

Thanks for helping

Re: Xcopy batch file that creates new folder

Posted: 18 Apr 2022 09:55
by Puccilillo
I'd use the date variable to create a UNIQUE backup everytime.
If you want to just use numbers you could go like:

Code: Select all

@echo off

::assign new number
:newnumber
set /a counter+=1
if exist d:\data_%counter% goto :newnumber

::execute backup
xcopy /e c:\data d:\data_%counter%
So, if the folder data_1 already exist, the new folder will be called data_2... and so on.

Bye