Backup File in BAT file with certain options

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Backup File in BAT file with certain options

#1 Post by ian1956 » 02 Nov 2011 08:47

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Backup File in BAT file with certain options

#2 Post by dbenham » 02 Nov 2011 09:39

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.

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

ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Re: Backup File in BAT file with certain options

#3 Post by ian1956 » 02 Nov 2011 09:44

Much appreciated Dave, will endevour to digest and hope I can make it work.

Ian

ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Re: Backup File in BAT file with certain options

#4 Post by ian1956 » 02 Nov 2011 10:54

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Backup File in BAT file with certain options

#5 Post by !k » 02 Nov 2011 15:56

"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"

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Backup File in BAT file with certain options

#6 Post by alan_b » 02 Nov 2011 17:43

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.

ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Re: Backup File in BAT file with certain options

#7 Post by ian1956 » 03 Nov 2011 02:05

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Backup File in BAT file with certain options

#8 Post by !k » 03 Nov 2011 07:11

del
Last edited by !k on 03 Nov 2011 09:10, edited 1 time in total.

ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Re: Backup File in BAT file with certain options

#9 Post by ian1956 » 03 Nov 2011 07:24

all it is saying now is

The file name. directory name , or volume label syntax is incorrect.

Regards

Ian

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Backup File in BAT file with certain options

#10 Post by !k » 03 Nov 2011 07:55

what is output of

Code: Select all

for /f "skip=1 delims=." %%d in ('wmic OS Get LocalDateTime') do echo _%%d_

ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Re: Backup File in BAT file with certain options

#11 Post by ian1956 » 03 Nov 2011 08:21

c:\mycompany> for /F "skip=1 delims=." xd in <'wmic OS Get LocalDateTime'> do echo _%d_

c:\mycompany>echo _20111103141403_
_20111103141403_

_\mycompany>echo _
_

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Backup File in BAT file with certain options

#12 Post by !k » 03 Nov 2011 09:06

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?

ian1956
Posts: 7
Joined: 02 Nov 2011 06:31

Re: Backup File in BAT file with certain options

#13 Post by ian1956 » 03 Nov 2011 09:12

Windows 7 Home premium

Post Reply