Page 1 of 1
Backup File in BAT file with certain options
Posted: 02 Nov 2011 08:47
by ian1956
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
Re: Backup File in BAT file with certain options
Posted: 02 Nov 2011 09:39
by dbenham
Most of the techniques you need are already presented at
Increase counter written inside bat file every time it runsThe 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
Posted: 02 Nov 2011 09:44
by ian1956
Much appreciated Dave, will endevour to digest and hope I can make it work.
Ian
Re: Backup File in BAT file with certain options
Posted: 02 Nov 2011 10:54
by ian1956
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
Re: Backup File in BAT file with certain options
Posted: 02 Nov 2011 15:56
by !k
"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
Posted: 02 Nov 2011 17:43
by alan_b
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.
Re: Backup File in BAT file with certain options
Posted: 03 Nov 2011 02:05
by ian1956
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
Re: Backup File in BAT file with certain options
Posted: 03 Nov 2011 07:11
by !k
del
Re: Backup File in BAT file with certain options
Posted: 03 Nov 2011 07:24
by ian1956
all it is saying now is
The file name. directory name , or volume label syntax is incorrect.
Regards
Ian
Re: Backup File in BAT file with certain options
Posted: 03 Nov 2011 07:55
by !k
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
Posted: 03 Nov 2011 08:21
by ian1956
c:\mycompany> for /F "skip=1 delims=." xd in <'wmic OS Get LocalDateTime'> do echo _%d_
c:\mycompany>echo _20111103141403_
_20111103141403_
_\mycompany>echo _
_
Re: Backup File in BAT file with certain options
Posted: 03 Nov 2011 09:06
by !k
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
Posted: 03 Nov 2011 09:12
by ian1956
Windows 7 Home premium