Backup Batch File
Moderator: DosItHelp
Backup Batch File
Using this code below in a batch file to backup an Access Database from a network drive to my local C: Drive.
Is here a way to also include the date into the folder name?
ROBOCOPY "G:\ACCESS FRONTEND UPDATER\ACCESS BACKEND" "C:\Users\john.smith\Desktop\BACKUPS\ACCESS BACKEND BACKUP" /s
So when it saves in my "BACKUPS" folder it will show as "ACCESS BACKEND BACKUP 10-18-2022" as the folder name.
Thank you!
Is here a way to also include the date into the folder name?
ROBOCOPY "G:\ACCESS FRONTEND UPDATER\ACCESS BACKEND" "C:\Users\john.smith\Desktop\BACKUPS\ACCESS BACKEND BACKUP" /s
So when it saves in my "BACKUPS" folder it will show as "ACCESS BACKEND BACKUP 10-18-2022" as the folder name.
Thank you!
Re: Backup Batch File
Give this a go:
However, think about how your folder names are ordered as soon as folders from 2023 are getting merged in next year. May I suggest to use date format YYYY-MM-DD instead?
Steffen
Code: Select all
@echo Off
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
echo %dt:~4,2%-%dt:~6,2%-%dt:~0,4%
pause
Code: Select all
echo %dt:~0,4%-%dt:~4,2%-%dt:~6,2%
Re: Backup Batch File
That is a wonderful idea about the date format. I will use that one. Haven't have time to test it yet but will do so soon.
Also the code I have been using all this time seems to only take everything inside the folder and not allowing me to specify what I want to backup. Is there a code to allow me to select and choose what file or folder I want and not just backup everything?
Thanks for the help. This will come in very handy.
Also the code I have been using all this time seems to only take everything inside the folder and not allowing me to specify what I want to backup. Is there a code to allow me to select and choose what file or folder I want and not just backup everything?
Thanks for the help. This will come in very handy.
Re: Backup Batch File
Gave this more thought for the code you provided since you mentioned about the sorting issue if I went with the original date format.
Would it be difficult to just have it replace whatever files are new within the backup and also replace the folder name to have the current date to the day that I run this batch file?
Right now, that is how my original code does it, without the date of course. The initial run takes a long time to backup everything because all files are new but the next time I run it, its really fast because it just looks for any files that are more updated than when I did the previous backup and only backs up those files. It will skip any files that remain unchanged since the last backup.
So basically have your code do the same thing but only update the folder date? Is that possible?
Thank you.
Would it be difficult to just have it replace whatever files are new within the backup and also replace the folder name to have the current date to the day that I run this batch file?
Right now, that is how my original code does it, without the date of course. The initial run takes a long time to backup everything because all files are new but the next time I run it, its really fast because it just looks for any files that are more updated than when I did the previous backup and only backs up those files. It will skip any files that remain unchanged since the last backup.
So basically have your code do the same thing but only update the folder date? Is that possible?
Thank you.
Re: Backup Batch File
Not too difficult. At least not if you do it the other way around (first rename using MOVE because it supports wildcards, then run ROBOCOPY for the update).Would it be difficult to just have it replace whatever files are new within the backup and also replace the folder name to have the current date to the day that I run this batch file?
Last edited by aGerman on 19 Oct 2022 06:23, edited 1 time in total.
Reason: REN does not support wildcards, MOVE does
Reason: REN does not support wildcards, MOVE does
Re: Backup Batch File
I was about to test your code but noticed that there is no directory paths showing. How does this work and will the code know where to save the backup? I wanted to test it on a test folder first before I try it only the database backend. How do I edit this code to just backup a folder that is on my C: Drive?
Thanks.
Thanks.
Re: Backup Batch File
I mean, what I've been telling you with the previous code was just how to get and assemble the date string. Of course you need to merge it with your work code
Don't worry about the MOVE command being used for the renaming of the folder. Nothing is ever physically moved as long as the virtual drive is still the same. Only the address is updated in the file system.
Steffen
Code: Select all
@echo off &setlocal
:: Source folder.
set "src=G:\ACCESS FRONTEND UPDATER\ACCESS BACKEND"
:: This part of the destination path doesn't change. It's gonna get extended either with the search pattern that represents the previous date, or with the new date suffix.
set "dst_common_part=%userprofile%\Desktop\BACKUPS\ACCESS BACKEND BACKUP"
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
set "dst_new_name=%dst_common_part% %dt:~4,2%-%dt:~6,2%-%dt:~0,4%"
:: Rename the folder.
:: Relies on exactly one subfolder being in "BACKUP" that matches with pattern "ACCESS BACKEND BACKUP *-*-*"
MOVE "%dst_common_part% *-*-*" "%dst_new_name%"
ROBOCOPY "%src%" "%dst_new_name%"
pause
Steffen
Re: Backup Batch File
Ok thanks for the info. I just tested the first code you gave me and added it after my code and basically it ran my code first and then your code was able to pull up the date in the correct format but it presents it to me through a terminal window and then your pause makes it stop so then it says to press any key to continue. When I press a key to continue it just disappears and nothing happens to the folder name. I then switched your code to be before my code and similar results happened just ran my code after yours. Here is an example of what I did in the batch file:
@echo Off
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
echo %dt:~0,4%-%dt:~4,2%-%dt:~6,2%
pause
ROBOCOPY "C:\Users\john.smith\Desktop\Wallpaper" "C:\Users\john.smith\Desktop\test" /s
@echo Off
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
echo %dt:~0,4%-%dt:~4,2%-%dt:~6,2%
pause
ROBOCOPY "C:\Users\john.smith\Desktop\Wallpaper" "C:\Users\john.smith\Desktop\test" /s
Re: Backup Batch File
In the latest post I wrote a piece of code that should work out of the box, right? And I included some comments in the code to explain what it is all about. I'm sure you already carefully read the code and the comments and tried to understand how it works. Just get back to me if you still have some difficulties.
Re: Backup Batch File
I think I am doing something wrong. I pasted your second suggested code with the notes to explain how it works before and after my ROBOCOPY code and basically the same thing happens in my previous explanation. If your code is run before mine, it will pause and tell me to press any key to continue. So I do, then it runs my code fine by backing up all the files into the folder I set as the backup folder. No name changing of the folder takes place. It's almost as if your code is just there for the "Press any key to continue" prompt. Also should mention that before I run the code I empty the backup folder before I run the batch file. This way I can see the files being backed up which would be my portion of the code (ROBOCOPY) and then your code runs with me just having to press any key to continue and once I do the terminal window closes and that's it. The backup folder name stays as is with no date in the name. I even tried to run the batch file again with the backup folder having contents in it and get the same result but faster because all the files are already backed up. The only thing I can think of is that maybe I need to update one of the source folder files so that it has a new file to backup and then maybe your code will trigger the date into the folder name for the backup folder? I guess I'll try that now and get back to you.
Re: Backup Batch File
Ok tried creating a new folder in the source folder with a notepad file just to test. Then I ran the batch file and still the same thing happens. It copies the new folder with notepad file into the backup folder then asks me to press any key to continue and when I press enter the terminal window closes and that's it. The backup folder name stays the same with no change.
Just want to make sure we are on the same page. What I would like is, every time I run the batch file, I would like the backup folder's name to have a date stamp in it to show when the last time I ran the backup batch file. So if the folder name is BACKUP for example and I ran the batch file today, the folder name would then be changed from BACKUP to BACKUP 20221022. If I run it tomorrow, the folder name would change from BACKUP 20221022 to BACKUP 20221023.
Is this what your code is trying to do? Again, thank you so much for your help and patience on this. I am not very good with writing batch files but once I get one to work, I then can apply it with situations in the future to make life easier. Appreciate your assistance on this.
Just want to make sure we are on the same page. What I would like is, every time I run the batch file, I would like the backup folder's name to have a date stamp in it to show when the last time I ran the backup batch file. So if the folder name is BACKUP for example and I ran the batch file today, the folder name would then be changed from BACKUP to BACKUP 20221022. If I run it tomorrow, the folder name would change from BACKUP 20221022 to BACKUP 20221023.
Is this what your code is trying to do? Again, thank you so much for your help and patience on this. I am not very good with writing batch files but once I get one to work, I then can apply it with situations in the future to make life easier. Appreciate your assistance on this.
Re: Backup Batch File
In your initial post the target is
"C:\Users\john.smith\Desktop\BACKUPS\ACCESS BACKEND BACKUP"
where my understanding has been that "ACCESS BACKEND BACKUP" should get the date suffix, like "ACCESS BACKEND BACKUP 10-22-2022"-
The code I wrote would rename "ACCESS BACKEND BACKUP 10-22-2022" to "ACCESS BACKEND BACKUP 10-23-2022" if you run it tomorrow.
If you want to keep the "ACCESS BACKEND BACKUP" name, and rename the parent folder from "BACKUPS 10-22-2022" to "BACKUPS 10-23-2022" instead, the path would look like that, right?
"C:\Users\john.smith\Desktop\BACKUPS 10-22-2022\ACCESS BACKEND BACKUP"
And the belonging code would be
Steffen
"C:\Users\john.smith\Desktop\BACKUPS\ACCESS BACKEND BACKUP"
where my understanding has been that "ACCESS BACKEND BACKUP" should get the date suffix, like "ACCESS BACKEND BACKUP 10-22-2022"-
The code I wrote would rename "ACCESS BACKEND BACKUP 10-22-2022" to "ACCESS BACKEND BACKUP 10-23-2022" if you run it tomorrow.
If you want to keep the "ACCESS BACKEND BACKUP" name, and rename the parent folder from "BACKUPS 10-22-2022" to "BACKUPS 10-23-2022" instead, the path would look like that, right?
"C:\Users\john.smith\Desktop\BACKUPS 10-22-2022\ACCESS BACKEND BACKUP"
And the belonging code would be
Code: Select all
@echo off &setlocal
set "src=G:\ACCESS FRONTEND UPDATER\ACCESS BACKEND"
:: This part of the destination path does never change. It's gonna get extended with the date suffix.
set "dst_common_part=%userprofile%\Desktop\BACKUPS"
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
set "dst_new_name=%dst_common_part% %dt:~4,2%-%dt:~6,2%-%dt:~0,4%"
:: Rename the folder.
:: Relies on exactly one subfolder being on your desktop which meets the pattern "BACKUP *-*-*"
MOVE "%dst_common_part% *-*-*" "%dst_new_name%"
ROBOCOPY "%src%" "%dst_new_name%\ACCESS BACKEND BACKUP"
pause
Re: Backup Batch File
You are correct with the initial post of me wanting to have the date stamp in the folder below the parent folder (would that be the daughter folder?) lol. Then in my next post I changed it to the parent folder.
I have changed some things around in the code as I am doing it on test folders right now because I do not want to apply this to the real ACCESS BACKEND folders until I understand fully how it works and to make sure it does what I need it to do. With that being said, I have created the test environment entirely on the C: Drive desktop and finally have made some progress. I am beginning to see how your code is going to work as far as the renaming of the folder. I was confused at first as I did not have a BACKUPS folder with the date in the name and so your code was creating another folder with the date in it and I didn't want that but now I realize that the folder with the date needs to exist first then your code will keep updating the date in the folder name. So that is working great because I tested it and renamed the date to yesterday then ran your code and it did update it. However, the issue I am having now is that this new folder is not backing up everything. So inside the "BACKUPS 10-22-2022" folder, there is another folder called "ACCESS BACKEND BACKUP" which is correct but inside that folder is blank with nothing in it. Here is the code that I ran that gave this result:
@echo off &setlocal
set "src=C:\Users\data8\Desktop\ACCESS FRONTEND UPDATER\ACCESS BACKEND"
:: This part of the destination path does never change. It's gonna get extended with the date suffix.
set "dst_common_part=%userprofile%\Desktop\BACKUPS"
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
set "dst_new_name=%dst_common_part% %dt:~4,2%-%dt:~6,2%-%dt:~0,4%"
:: Rename the folder.
:: Relies on exactly one subfolder being on your desktop which meets the pattern "BACKUP *-*-*"
MOVE "%dst_common_part% *-*-*" "%dst_new_name%"
ROBOCOPY "%src%" "%dst_new_name%\ACCESS BACKEND BACKUP"
pause
I have changed some things around in the code as I am doing it on test folders right now because I do not want to apply this to the real ACCESS BACKEND folders until I understand fully how it works and to make sure it does what I need it to do. With that being said, I have created the test environment entirely on the C: Drive desktop and finally have made some progress. I am beginning to see how your code is going to work as far as the renaming of the folder. I was confused at first as I did not have a BACKUPS folder with the date in the name and so your code was creating another folder with the date in it and I didn't want that but now I realize that the folder with the date needs to exist first then your code will keep updating the date in the folder name. So that is working great because I tested it and renamed the date to yesterday then ran your code and it did update it. However, the issue I am having now is that this new folder is not backing up everything. So inside the "BACKUPS 10-22-2022" folder, there is another folder called "ACCESS BACKEND BACKUP" which is correct but inside that folder is blank with nothing in it. Here is the code that I ran that gave this result:
@echo off &setlocal
set "src=C:\Users\data8\Desktop\ACCESS FRONTEND UPDATER\ACCESS BACKEND"
:: This part of the destination path does never change. It's gonna get extended with the date suffix.
set "dst_common_part=%userprofile%\Desktop\BACKUPS"
for /f %%i in ('wmic OS GET LocalDateTime /value') do for /f "tokens=2 delims==." %%j in ("%%i") do set "dt=%%j"
set "dst_new_name=%dst_common_part% %dt:~4,2%-%dt:~6,2%-%dt:~0,4%"
:: Rename the folder.
:: Relies on exactly one subfolder being on your desktop which meets the pattern "BACKUP *-*-*"
MOVE "%dst_common_part% *-*-*" "%dst_new_name%"
ROBOCOPY "%src%" "%dst_new_name%\ACCESS BACKEND BACKUP"
pause
Re: Backup Batch File
I should show you the terminal window screenshot as it says the filename, directory name, or the volume label syntax is incorrect.
I've attached the screenshot.
Also, does it matter if the source folder is backing up not only files but also subfolders with other files in them? Just want to make sure your code is not to only backup files itself but basically a whole directory of subfolders and files mixed together. I wouldn't think that would matter but I guess it's worth mentioning just in case.
I've attached the screenshot.
Also, does it matter if the source folder is backing up not only files but also subfolders with other files in them? Just want to make sure your code is not to only backup files itself but basically a whole directory of subfolders and files mixed together. I wouldn't think that would matter but I guess it's worth mentioning just in case.
- Attachments
-
- Terminal Window.JPG (76.59 KiB) Viewed 8612 times
Re: Backup Batch File
I have no idea how it comes that you appended the date twice. This doesn't fit to the code you posted.
FWIW I left out any options for the ROBOCOPY command because you know best what you want to do. However, you might want to add at least option /s
FWIW I left out any options for the ROBOCOPY command because you know best what you want to do. However, you might want to add at least option /s