LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

finding most recent file in a directory

Hi,

Is there a simple and easy way in labview to write a program that goes into a directory and looks for the most recent file. I can think of one way where I use the list directory vi to obtain an array of the file names in the directory and then use the file/directory info vi to obtain a time stamp for each file in the directory and afterwards sort all the time stamps. I'm not sure if this is the simplest way to do this, and wanted to ask if anyone on the discussion forums knows of any.

Thank you
0 Kudos
Message 1 of 9
(5,325 Views)
Using pure LabVIEW functions that's pretty much all you can do. If you're on Windows and want to try .NET you can use the DirectoryInfo class GetFileSystemInfos method. This returns a list of FileSystemInfo objects with the creation times.

You could also try the "dir" command through a System Exec call and parse the string output, but I'm not sure if that will work.
Message 2 of 9
(5,313 Views)
Message 3 of 9
(5,307 Views)
Hi,

Thank you for your suggestions. Unclebump, the vi at the link you suggested is exactly what I am looking for. I will just need to modify it slightly to find a set of the most recent files.

Thank you
0 Kudos
Message 4 of 9
(5,298 Views)


@unclebump wrote:
Try here. http://zone.ni.com/devzone/cda/epd/p/id/3175


This example is not that great and a few tweaks can greatly improve it!
  1. Building the path with string operations inside the loop automatically breaks this VI on anything but windows. There is a reason we have the "build path" function! It should always be used. The "string to path" and "path to string" should only be used for OS specific tasks.
  2. You probably want to skip files that are actually links. Newer versions of "File/Directory info" tell you if the path points to a link instead of a file with a boolean output.
  3. WIy do we need to keep an array of all files and maintain the corret index in a shift register. It seems more intuitive and less wasteful to just keep the newest file path in the shift register and not worry about the index.
  4. ...

The image below shows a quick attempt at improving the example. Modify as needed.

Message Edited by altenbach on 04-12-2007 02:45 AM

Message 5 of 9
(5,283 Views)

i know this is an old thread but it does just what it's supposed to and it would seem it works fine just as it is.

 

i was wondering if i had done the right modifications according to what you suggested altenbach?

also, i was thinking that i would set the default folder as an input from the main VI (maybe with additional subdir), your thoughts?

and one last thing, if i want to get the entire file path (including name and extension) as output instead of just folder, how would i go about it?

attached is the modified VI.

0 Kudos
Message 6 of 9
(4,644 Views)

@abenigma wrote:

i know this is an old thread but it does just what it's supposed to and it would seem it works fine just as it is.

 

i was wondering if i had done the right modifications according to what you suggested altenbach?

also, i was thinking that i would set the default folder as an input from the main VI (maybe with additional subdir), your thoughts?

and one last thing, if i want to get the entire file path (including name and extension) as output instead of just folder, how would i go about it?

attached is the modified VI.


I think your code is pretty good.  The only suggestion I would have is to wire a constant to the uninitialized shift register index.

 

I attached my attempt to do the same using OpenG.  It is going to be less efficient without a doubt, but I wasn't sure what would be more useful, having the newest file or the oldest, so my code finds both, as well as sorts the files by their date/time.  Saved in 2012.

0 Kudos
Message 7 of 9
(4,626 Views)

abenigma wrote:

i was wondering if i had done the right modifications according to what you suggested altenbach?


No, you are building an potentially huge array of paths, only to later keep only one element. This is wasteful and inefficient. All you need to keep is the currently newest path as in my code. Right?


abenigma wrote:

also, i was thinking that i would set the default folder as an input from the main VI (maybe with additional subdir), your thoughts?


Sorry, I don't understand what you are trying to say here. If you set the folder via an input terminal, the default value is irrelevant. Please explain.


@abenigma wrote:

iand one last thing, if i want to get the entire file path (including name and extension) as output instead of just folder, how would i go about it?


I don't understand your point. My VI already outputs the entire path, including name and extension (note the "build path" primitive!). Make sure your indicator is wide enough to display it.

 

 

 

 

 

0 Kudos
Message 8 of 9
(4,620 Views)
  1. correct. i'm not sure just what the input is besides the path control element is.
  2. i meant something like a default path (not sure how to set it to always change with VI location).
  3. just figured that the path indicator would display the end of the path which contains the file name (arguably most important part of the path) but it really was just a matter of indicator window size :))

this isn't that urgent now since my lectrer "suggested" i don't use it, but i would still like to know.

correction, not urgent at all...

0 Kudos
Message 9 of 9
(4,599 Views)