Solved

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

Posted on 2008-06-18
5
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

726 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