Backup File in BAT file with certain options
Moderator: DosItHelp
Backup File in BAT file with certain options
I would like to be able to do the following in a bat file , if possible ,
Copy a file called chauffeur.accdb to a file called chauffeur1.accdb
The next time the bat file is run copy the file to chauffeur2.accdb . the next time to chauffeur3.accdb etc
also to throw into the mix delete the oldest version (retaining 5 copies on disk)
Many thanks
Ian
Copy a file called chauffeur.accdb to a file called chauffeur1.accdb
The next time the bat file is run copy the file to chauffeur2.accdb . the next time to chauffeur3.accdb etc
also to throw into the mix delete the oldest version (retaining 5 copies on disk)
Many thanks
Ian
Re: Backup File in BAT file with certain options
Most of the techniques you need are already presented at Increase counter written inside bat file every time it runs
The part that is missing is retention of only 5 copies. Once you have the number of the most recent file, you can simply delete the file that is 5 versions before.
If you want the oldest file on disk to always be 1, you will also have to rename the versions
Dave Benham
The part that is missing is retention of only 5 copies. Once you have the number of the most recent file, you can simply delete the file that is 5 versions before.
Code: Select all
:: assume CUR is number of most recent file
set /a OLD=CUR-5
if exist chauffeur%OLD%.accdb del chauffeur%OLD%.accdb
If you want the oldest file on disk to always be 1, you will also have to rename the versions
Code: Select all
setlocal enableDelayedExpansion
if %CUR% gtr 5 (
for /l %%N in (1 1 5) do (
set /a SRC=%%N+1
ren chauffeur!SRC!.accdb chauffeur%%N.accdb
)
)
Dave Benham
Re: Backup File in BAT file with certain options
Much appreciated Dave, will endevour to digest and hope I can make it work.
Ian
Ian
Re: Backup File in BAT file with certain options
Have looked at coding and don't understand a word of it. My expertise in dos commands just about allows me to cd md and copy.
Ian
Ian
Re: Backup File in BAT file with certain options
"chauffeur%digit%.accdb" name allowed only? May it be "chauffeur%date%%time%.accdb" ?
Code: Select all
for /f "skip=1 delims=." %%d in ('wmic OS Get LocalDateTime') do copy /b "chauffeur.accdb" "chauffeur_%%d.accdb"
for /f "skip=5" %%f in ('dir /b/o-d/tc "chauffeur_??????????????.accdb"') do del /q "%%f"
Re: Backup File in BAT file with certain options
A simpler solution might be to use a double extension, i.e.
chauffeur.01.accdb
chauffeur.02.accdb
chauffeur.03.accdb
chauffeur.04.accdb
chauffeur.05.accdb
chauffeur.06.accdb
etc.
chauffeur.01.accdb
chauffeur.02.accdb
chauffeur.03.accdb
chauffeur.04.accdb
chauffeur.05.accdb
chauffeur.06.accdb
etc.
Re: Backup File in BAT file with certain options
Much appreciated for your help , I have put the code in a BAT file and it works great ie copies the file and retains only the last 5 copies.
@echo off
for /f "skip=1 delims=." %%d in ('wmic OS Get LocalDateTime') do copy /b "chauffeur.accdb" "chauffeur_%%d.accdb"
for /f "skip=5" %%f in ('dir /b/o-d/tc "chauffeur_??????????????.accdb"') do del /q "%%f"
pause
But when run , it displays the following
1 File<s> copied
The file name. directory name , or volume label syntax is incorrect.
0 File<s> copied
Press any key to continue . . .
Any suggestions please
Regards
Ian
@echo off
for /f "skip=1 delims=." %%d in ('wmic OS Get LocalDateTime') do copy /b "chauffeur.accdb" "chauffeur_%%d.accdb"
for /f "skip=5" %%f in ('dir /b/o-d/tc "chauffeur_??????????????.accdb"') do del /q "%%f"
pause
But when run , it displays the following
1 File<s> copied
The file name. directory name , or volume label syntax is incorrect.
0 File<s> copied
Press any key to continue . . .
Any suggestions please
Regards
Ian
Re: Backup File in BAT file with certain options
del
Last edited by !k on 03 Nov 2011 09:10, edited 1 time in total.
Re: Backup File in BAT file with certain options
all it is saying now is
The file name. directory name , or volume label syntax is incorrect.
Regards
Ian
The file name. directory name , or volume label syntax is incorrect.
Regards
Ian
Re: Backup File in BAT file with certain options
what is output of
Code: Select all
for /f "skip=1 delims=." %%d in ('wmic OS Get LocalDateTime') do echo _%%d_
Re: Backup File in BAT file with certain options
c:\mycompany> for /F "skip=1 delims=." xd in <'wmic OS Get LocalDateTime'> do echo _%d_
c:\mycompany>echo _20111103141403_
_20111103141403_
_\mycompany>echo _
_
c:\mycompany>echo _20111103141403_
_20111103141403_
_\mycompany>echo _
_
Re: Backup File in BAT file with certain options
Code: Select all
for /f "skip=1 delims=." %%d in ('wmic OS Get LocalDateTime') do (copy /b "chauffeur.accdb" "chauffeur_%%d.accdb" >nul &goto:del)
:del
for /f "skip=5" %%f in ('dir /b/a-d/o-d/tc "chauffeur_??????????????.accdb"') do del /q "%%f"
what`s your OS?
Re: Backup File in BAT file with certain options
Windows 7 Home premium