Bat File to copy file and delete oldest copy

November 4, 2011 at 02:12:37
Specs: Windows 7
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

See More: Bat File to copy file and delete oldest copy

Report •

November 5, 2011 at 19:12:56
My colleague Valerie tried to post this script on another forum in response to your query but had difficulty due to that forum not being able to handle redirection chars < and >.

Note that the files she set up are located in the %temp% folder, not a desirable location except for testing purposes. The file Chaufnbr.txt is key to numbering files and starts off containing just 0 (zero). Remove the REM command when you are satisfied that the output is what you want.

Good luck.

@Echo Off
setlocal enabledelayedexpansion

set /p nbr=<%temp%\chaufnbr.txt
set /a nbr+=1

copy %temp%\chauffeur.accdb %temp%\chauffeur!nbr!.accdb>nul

echo %nbr% > %temp%\chaufnbr.txt

if %nbr% gtr 5 (
   set /a delnum=!nbr!-5
   echo Chauffeur!delnum!.accdb being deleted
   REM del %temp%\chauffeur!delnum!.accdb


Please come back & tell us if your problem is resolved.

Report •

November 6, 2011 at 01:13:27
Hi Wahine / Valerie

Thanks for your reply

Have tried to test it but it doesn't seem to work (might be me)
I could not find the temp folder on my disk that it was trying to use

But someone posted this on a forum and it works a treat

@echo off
cd c:\mycompany
set /p cpn=<cpn.txt
rem echo cpn=%cpn%
set /a cpn=%cpn% + 1
echo cpn=%cpn%
copy c:\mycompany\chauffeur.accdb C:\mycompany\chauffeur%cpn%.accdb
rem echo %cpn% > cpn.txt
set /a ecp=%cpn% - 5
del chauffeur%ecp%.accdb



Report •

Related Solutions

Ask Question