Discussion:
Copy only newest file in directory
(too old to reply)
Swift
2005-04-14 08:55:11 UTC
Permalink
Hey everybody,

I want to make a vbs script which will copy only the newest file in a
directory to another location.
For the 'copy' part I've used this syntax:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "E:\DCIM\100IMAGE\*.jpg", "P:\POORT 5\Foto's sper LKW"


But how can I make sure only the newest file will be copied? Is it possible
to work with a kind of timestamp or something?

Thanks in advance,

Dirk
Tim
2005-04-14 09:47:26 UTC
Permalink
Dirk, Depends upon exactly what you mean by 'newest' - last modifed or
most recently created?
Anyway, the code below should help.

It iterates through all files in a particular folder and displays when
each
was last modified and created.

You'll just need to keep track of which is the 'newest' file, and then
copy it at the end of the loop.



Dim oDir
Dim oThisFile
Dim oFSO
Dim oNewestFile
Dim TheMsg

TheDir = "C:\MyDirectory"
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oDir = oFSO.GetFolder(TheDir)

For Each oFile in oDir.Files
TheMsg = oFile.Name & vbCrLf
TheMsg = TheMsg & "Last modified: " & oFile.DateLastModified & vbCrLf
TheMsg = TheMsg & "Was Created: " & oFile.DateCreated
MsgBox TheMsg

Next


Regards,

Tim
Swift
2005-04-14 10:35:45 UTC
Permalink
Thanks for the quick response, Tim.
With this code one can get a chronologic sequence of the files, with the
newest at top and the oldest last. But how can I select only the newest
(most recently created) file? Is there a way to chronologic list up all
files in an directory and next copy the first file of this list to
another directory?

Regards,

Dirk
Post by Tim
Dirk, Depends upon exactly what you mean by 'newest' - last modifed or
most recently created?
Anyway, the code below should help.
It iterates through all files in a particular folder and displays when
each
was last modified and created.
You'll just need to keep track of which is the 'newest' file, and then
copy it at the end of the loop.
Dim oDir
Dim oThisFile
Dim oFSO
Dim oNewestFile
Dim TheMsg
TheDir = "C:\MyDirectory"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDir = oFSO.GetFolder(TheDir)
For Each oFile in oDir.Files
TheMsg = oFile.Name & vbCrLf
TheMsg = TheMsg & "Last modified: " & oFile.DateLastModified &
vbCrLf TheMsg = TheMsg & "Was Created: " & oFile.DateCreated
MsgBox TheMsg
Next
Regards,
Tim
Christoph Basedau
2005-04-14 12:32:47 UTC
Permalink
Post by Swift
Thanks for the quick response, Tim.
With this code one can get a chronologic sequence of the files, with the
newest at top and the oldest last.
If you use JScript it's easy, cause you can let an array get sorted
by date.
Post by Swift
But how can I select only the newest
(most recently created) file? Is there a way to chronologic list up all
files in an directory and next copy the first file of this list to
another directory?
var fs = new ActiveXObject("Scripting.FileSystemObject");
var dir = fs.GetFolder("C:\\DATA");

var files = [];

for (var F = new Enumerator(dir.Files);
!F.atEnd(); F.moveNext()) {
files.push(F.item());
}
files.sort(BY_CREATIONDATE_DESC);
if (files.length) {
WScript.Echo("Most recently created file: " + files[0]);
files[0].Copy "C:\\another directory";
}
else
WScript.Echo("no files found in " + dir);

function BY_CREATIONDATE_DESC(f1, f2){
var p1 = f1.DateCreated, p2 = f2.DateCreated;
if (p1 < p2) return 1;
if (p1 > p2) return -1;
return 0;
}
--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979
Tim
2005-04-14 12:43:38 UTC
Permalink
Hi Dirk,

My previous code snip doesn't order the files in any way. It simply
loops through them and tells you the date created / modified of each
file. To obtain the most recently created file, we can use the same
technique, but keep a track within the loop of the most recently
created file and the date it was created. See below.

Dim oDir
Dim oThisFile
Dim oFSO
Dim oNewestFile
Dim MRCTime
Dim MRCFile
Dim TheDestinationDir

TheDir = "C:\MyFolder"
TheDestinationDir = "C:\MyDestinationFolder"

Set oFSO = CreateObject("Scripting.FileSystemObject")
MRCTime = "NotInitialised"

Set oDir = oFSO.GetFolder(TheDir)

For Each oFile in oDir.Files

If MRCTime = "NotInitialised" Then
MRCTime = oFile.DateCreated
Else
If DateDiff("s", oFile.DateCreated, MRCTime) < 0 Then
MRCFile = oFile.Name
MRCTime = oFile.DateCreated
End If
End If

Next

MsgBox "Most Recently Created file = " & MRCFile & " : " & MRCTime

oFSO.CopyFile TheDir & "\" & MRCFile,TheDestinationDir


Regards,

Tim
Swift
2005-04-14 13:52:55 UTC
Permalink
Thanks to the both of you, now I have enough to make it work !

Kind regards,

Dirk

Continue reading on narkive:
Loading...