Jump to content

Copy only the newest file in a directory


r00ster

Recommended Posts

Hi,

I'm looking for a way to automate the copy of a windows backup file (BKF). My server (WIN2K3 SBS) is configured to leave 3 versions of the backup on a external harddisk so I can retrieve files that were previously deleted. I also would like to copy the latest created BKF file to another external harddisk that I can take back home with me so in case of a fire or theft I have an extra copy.

The BKF file is large (over 90 Gb) and takes a few hours to be created, this is done every night from 02:00 till 7:00 in the morning. What I would like to accomplish is that during lunch time I click on a vb script that copies the newest BKF file to another external harddisk so that a few hour later I can unplug the harddisk and take it home. Because there are 3 files of over 90 Gb the normal "xcopy *.* f:\" line will take to long to copy all the files to the harddisk.

Can somebody help me out with a simple script that will copy only the newest BKF to my external harddisk. :thumbup

Thanks,

r00ster

Link to comment
Share on other sites


You can easily use this (just copy and paste into a new file newer.cmd):

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set /A Counter=0
FOR /F "skip=2 tokens=4" %%A in ('dir %1 /o-D') DO (
IF !Counter!==0 ECHO %%A
SET Counter=!Counter!+1
)

use as

newer.cmd "drive\Path\directory"

this will simply display the filename of the most recent file in the given directory.

Just change the %1 in the FOR loop with your hardcoded path (if you wish to do so) and change the ECHO command into your xcopy one.

The following is slightly more elaborate but returns more data on the file:

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set /A Counter=0
FOR /F "skip=2 tokens=4" %%A in ('dir %1 /o-D') DO (
IF !Counter!==0 (
ECHO Name : %%~nA
ECHO Extension: %%~xA
ECHO Full Path: %%~fA
)
SET Counter=!Counter!+1
)

In a command prompt type FOR /? to see other possible expansions of variables.

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...