Backup current folder to another location
Moderator: DosItHelp
Backup current folder to another location
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.
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.
Re: Batch File help
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
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"
Re: Batch File help
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?
Re: Batch File help
It is copying from the folder the batch file is in.
Re: Batch File help
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.
Re: Batch File help
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
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
Re: Batch File help
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:
may suggest /XF %0/XF file [file]... :: eXclude Files matching given names/paths/wildcards.
Re: Batch File help
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.
Re: Backup current folder to another location
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
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

Re: Backup current folder to another location
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.
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.
Re: Backup current folder to another location
Oh my didnt see compo's response. Apologies on that part.
Re: Backup current folder to another location
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.