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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

919 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

16 Experts available now in Live!

Get 1:1 Help Now