I'm looking to make a backup batch for several older machines in my house that don't have a built in backup utility. I have a drive on the network, both machines can see it and access it, and I've mapped the drive. I have a batch file that with user input can make a rolling backup (think like Apple's Time Machine) that only backups up altered or new files using the xcopy with the /d:[date] switch. My desired format is, if I can do it in these steps.
1. The initial batch file is used. it creates a log file that notes the date that it has run.
2. It also copies a new batch to the start menu's startup folder that runs when the computer starts with no user interaction.
3. The initial batch backups up all the users folder to the network drive in the format [networkdrive]:\Backup\[YYYY]\[MM]\[DD]\.
4. When it runs on bootup the startup batch then reads the day, month, and the year from the logfile created by the initial batch file, and plugs those parts into variables %backupday%, %backupmonth% %backupyear%
5. The batch runs xcopy in the follow syntax; xcopy [FILES] [networkdrive]:\Backup\%backupyear%\%backupmonth%\%backupday%\ /e /y /d:%backupmonth%/%backupday%/%backupyear%
6. The startup batch redirects the current date into the same logfile that the intitial batch created, so that the next time it runs, it will simply add the last time it was backed up into the variables and thus only backup files that have been created or changed since then.
This command should backup all files modified or created after a certain date
The only issue I am facing is finding out how to extract *just* the numerical date (ignoring the mon or fri for the day of the week) and placing it into the logfile, and how to then extract just the day or month or year out of the logfile and plug it into variables. I suspect findstr or something similar is going to be needed.
Backup dos batch
Moderator: DosItHelp
Re: Backup dos batch
We use
for /F "tokens=2" %%I in ('date /t') do @set DS1=%%I
for /F "tokens=1-3 delims=/" %%K in ('echo %DS1%') do @set backupmonth=%%K
for /F "tokens=1-3 delims=/" %%K in ('echo %DS1%') do @set backupday=%%L
for /F "tokens=1-3 delims=/" %%K in ('echo %DS1%') do @set backupyear=%%M
The first line strips the day of week from date /t so the following lines can set the numeric data.
for /F "tokens=2" %%I in ('date /t') do @set DS1=%%I
for /F "tokens=1-3 delims=/" %%K in ('echo %DS1%') do @set backupmonth=%%K
for /F "tokens=1-3 delims=/" %%K in ('echo %DS1%') do @set backupday=%%L
for /F "tokens=1-3 delims=/" %%K in ('echo %DS1%') do @set backupyear=%%M
The first line strips the day of week from date /t so the following lines can set the numeric data.
Re: Backup dos batch
@rhidir, Not sure why you used four lines for this as it's a one line trick.
Keep in mind that the Regional settings of your machine determine the order of Day Month and Year as displayed, hence do an ECHO %DATE% first to know what you'll be getting.
For USA Regional Settings;
If you want you can replace ("%DATE%") with ('date /t')
If you want a method that works regardless of local regional settings of the machine, there's a function on this forum called getDate
and an alternative that I posted (which in this case would be);
Keep in mind that the Regional settings of your machine determine the order of Day Month and Year as displayed, hence do an ECHO %DATE% first to know what you'll be getting.
For USA Regional Settings;
Code: Select all
for /F "tokens=2,3,4 delims=/ " %%A in ("%DATE%") do SET backupmonth=%%A &SET backupday=%%B &SET backupyear=%%C
If you want you can replace ("%DATE%") with ('date /t')
If you want a method that works regardless of local regional settings of the machine, there's a function on this forum called getDate
and an alternative that I posted (which in this case would be);
Code: Select all
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set /a "backupyear=%ldt:~0,4%,backupmonth=1%ldt:~4,2%-100,backupday=1%ldt:~6"
exit /B
Re: Backup dos batch
@b8two I like your solution better.
The space in the delims after the / makes a world of difference and I forget about the & to execute multiple commands on a line.
Thanks
The space in the delims after the / makes a world of difference and I forget about the & to execute multiple commands on a line.
Thanks