Solved

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

Posted on 2008-06-18
5
319 Views
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?
0
Comment
Question by:Robert Pollick
5 Comments
 
LVL 14

Accepted Solution

by:
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.

sew

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 & "\" & objFile.name
		arrList(i) = objFile & "|" & objFile.DateLastModified
		i = i + 1
	Next
	
 
	'msgbox LBound(arrList)
	Call BubbleSort(arrList)
	
	For i = 0 To 2
		arrFile = Split(arrList(i),"|")
		objFSO.CopyFile arrFile(0), strTargetPath & "\" & objFSO.GetFileName(arrFile(0))
	Next
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
		Next
	Next
End Function

Open in new window

0
 

Author Comment

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

Expert Comment

by:devil_himself
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
 
:sub1
 
if %numDONE%==3 goto :eof
echo copy %1 place
set /a numDONE+=1
goto :eof
:: DONE

Open in new window

0
 
LVL 14

Expert Comment

by:sungenwang
ID: 21818795
You're welcome!
sew
0
 
LVL 65

Expert Comment

by:RobSampson
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.

Regards,

Rob.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

733 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