Copy the three newest files. This batch or script will run every day.

Posted on 2008-06-18
Last Modified: 2012-05-05
I need to copy the three newest files from a log directory to another. This process will repeat every day at a designated time so I would like to be able to batch this or script this so that it only pulls the three newest files. Can anyone help?
Question by:Robert Pollick
LVL 14

Accepted Solution

sungenwang earned 125 total points
ID: 21818102
The vbscript below will get all the files into an array, and use Bubble Sort to sort the dates and copy the latest three to a target path.


strSourcePath = "c:\temp"

strTargetPath = "c:\temp1"

Call CopyNewestThree(strSourcePath, strTargetPath)

Function CopyNewestThree(strSourcePath, strTargetPath)

	Dim arrList()


	Set objFSO = CreateObject("Scripting.FileSystemObject")

	Set objCurrentFolder = objFSO.GetFolder(strSourcePath)

	ReDim arrList(objCurrentFolder.Files.Count - 1)


	' store all files into dictionary object except LOG files

	i = 0

	For Each objFile In objCurrentFolder.Files

		'msgbox objFile.ParentFolder & "\" &

		arrList(i) = objFile & "|" & objFile.DateLastModified

		i = i + 1



	'msgbox LBound(arrList)

	Call BubbleSort(arrList)


	For i = 0 To 2

		arrFile = Split(arrList(i),"|")

		objFSO.CopyFile arrFile(0), strTargetPath & "\" & objFSO.GetFileName(arrFile(0))


End Function

Function BubbleSort(arrSortList)

	For i = LBound(arrSortList) to UBound(arrSortList)

		For j = LBound(arrSortList) to UBound(arrSortList)

			If j <> UBound(arrSortList) Then

				arrCurItem = Split(arrSortList(j),"|")

				dtCur = arrCurItem(1)

				arrNextItem = Split(arrSortList(j+1),"|")

				dtNext = arrNextItem(1)

				If CDate(dtCur) < CDate(dtNext) Then

					TempValue = arrSortList(j + 1)

					arrSortList(j + 1) = arrSortList(j)

					arrSortList(j) = TempValue

				End If

			End If



End Function

Open in new window


Author Comment

by:Robert Pollick
ID: 21818178
Works perfectly! Thank you very much.

Expert Comment

ID: 21818477
here a batch .. no points
@echo off

set /a numDONE=0

for /f %%D in ('dir/b/a-d/o-d') do call :sub1 %%D

goto :eof


if %numDONE%==3 goto :eof

echo copy %1 place

set /a numDONE+=1

goto :eof


Open in new window

LVL 14

Expert Comment

ID: 21818795
You're welcome!
LVL 65

Expert Comment

ID: 21818987
rpollick, although you've been registered on EE for some time, congratulations on receiving a perfectly accurate solution on your first post.  Nicely done sew, that's a great solution.

rpollick, you should now be able to accept the relevant comment as a solution, so the question can be closed.  Also note that in future, if you feel that one or comments contributed to your solution, you can accept multiple answers.



Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now