Xcopy batch file that creates new folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ones_Zeros
Posts: 4
Joined: 10 Apr 2016 20:16

Xcopy batch file that creates new folder

#1 Post by Ones_Zeros » 03 Mar 2022 23:12

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

Puccilillo
Posts: 8
Joined: 11 Apr 2022 09:33
Location: Italy
Contact:

Re: Xcopy batch file that creates new folder

#2 Post by Puccilillo » 18 Apr 2022 09:55

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

Post Reply