Backup current folder to another location

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ecmel
Posts: 5
Joined: 14 Jun 2014 21:10

Backup current folder to another location

#1 Post by Ecmel » 14 Jun 2014 21:18

I want to make a batch file to copy the folder batch file is currently in to another location and if the folder already exists first delete the folder and all of its content then copy the new folder.

So c:\a folder got batch file in it and i want it to go to d:\x\y

when i put the batch file in A and use i want it to check if d:\x\y has a folder called a in it if it does delete it then copy the a folder there (If possible excluding the batch file itself)

Is this possible and if so how? also reason i say it should coyp the folder its currently in is so if i put the .bat into antoher folder and use it should do same for that folder without any editing.

Thanks for your help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File help

#2 Post by foxidrive » 14 Jun 2014 22:10

This should do what you require.

It's not necessary to check if the folder exists or to delete one, because the /mir switch will create a mirror backup and delete any files that aren't part of the backup.

Be very sure not to get d:\x\y wrong

Code: Select all

@echo off
for %%a in ("%cd%") do robocopy "."  "d:\x\y\%%~nxa" /mir & del "d:\x\y\%%~nxa\%%~nx0"

Ecmel
Posts: 5
Joined: 14 Jun 2014 21:10

Re: Batch File help

#3 Post by Ecmel » 15 Jun 2014 03:28

Ok did not know about mir. Is it possible to not use %%a instead a command that automaticly does it for the folder batch file in it? So i can use it in any folder i want?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File help

#4 Post by Squashman » 15 Jun 2014 05:12

It is copying from the folder the batch file is in.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File help

#5 Post by foxidrive » 15 Jun 2014 05:43

Ecmel wrote:Is it possible to not use %%a instead a command that automaticly does it for the folder batch file in it? So i can use it in any folder i want?


As Squashman says - it already does that.

Ecmel
Posts: 5
Joined: 14 Jun 2014 21:10

Re: Batch File help

#6 Post by Ecmel » 15 Jun 2014 12:32

Okey i was confused cause example was also "a" lol sorry! and thanks a bunch! gonna test it soon great and fast help!

edit: Works like a charm!.. 1 more thing..is there anyway to exclude the batchfile itself from the process so it does exactly this except doesnt copy itself. If not this is still awesome

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch File help

#7 Post by Compo » 15 Jun 2014 13:04

Ecmel wrote:Okey i was confused cause example was also "a" lol sorry! and thanks a bunch! gonna test it soon great and fast help!

edit: Works like a charm!.. 1 more thing..is there anyway to exclude the batchfile itself from the process so it does exactly this except doesnt copy itself. If not this is still awesome


Typing ROBOCOPY /? and looking at:
/XF file [file]... :: eXclude Files matching given names/paths/wildcards.
may suggest /XF %0

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File help

#8 Post by Squashman » 15 Jun 2014 18:35

Ecmel wrote:Okey i was confused cause example was also "a" lol sorry! and thanks a bunch! gonna test it soon great and fast help!

edit: Works like a charm!.. 1 more thing..is there anyway to exclude the batchfile itself from the process so it does exactly this except doesnt copy itself. If not this is still awesome

that is why the delete command is in there.

Ecmel
Posts: 5
Joined: 14 Jun 2014 21:10

Re: Backup current folder to another location

#9 Post by Ecmel » 16 Jun 2014 10:42

Sorry i explained it wrong. What i asked was if its possible to do same thing exactly but once its done the .bat file shouldnt exist anymore in the new folder. (So it doesnt copy itself with the folder..or deletes itself afterwards) <- This is NOT That important so only if its easy.

And 1 last request.. if i may ask. If i wanted to make another bat file that does same thing BUT instead of copying the folder its in - it copies folders/files inside it (folders/files next to .bat file). would you be kind to show the code for that?

Again these arent so important and you helped me a lot already so understand if you dont want to :)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Backup current folder to another location

#10 Post by Squashman » 16 Jun 2014 10:55

1) Did you try Compo's code?

2) Have you bothered to read the help file for the ROBOCOPY command? What does the /MIR option do and that might answer your last question.

Ecmel
Posts: 5
Joined: 14 Jun 2014 21:10

Re: Backup current folder to another location

#11 Post by Ecmel » 16 Jun 2014 10:59

Oh my didnt see compo's response. Apologies on that part.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Backup current folder to another location

#12 Post by foxidrive » 16 Jun 2014 17:04

Ecmel wrote:Sorry i explained it wrong. What i asked was if its possible to do same thing exactly but once its done the .bat file shouldnt exist anymore in the new folder. (So it doesnt copy itself with the folder..or deletes itself afterwards) <- This is NOT That important so only if its easy.


It already does that. The del command at the end deletes the batch file from the target folder - if you also changed d:\x\y in it.

Post Reply