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
Solved

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

Posted on 2008-06-18
5
318 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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 …

790 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