Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Independent Software Vendors: 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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

636 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