?
Solved

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

Posted on 2008-06-18
5
Medium Priority
?
336 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

777 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