Waiting for a file to appear

Having trouble finding a sound method that doesn't break when waiting
for a file to appear. My initial approach was this:

while File.exists?( "filename" ) == false
   sleep( 5 )
end

This worked for a while, but doesn't seem to be robust. I then tried
this:

until File.exists?( "filename" )
   sleep( 5 )
end

Which also doesn't seem to work.

I'm running Ruby 1.8.7. Is there a better way to do this?

···

--
Posted via http://www.ruby-forum.com/.

Each OS family has its own system-level way of notifying programs when
files are added, removed, or changed. There are Ruby interfaces for the
Windows, Linux, and OS X methods; perhaps one of these gems would be of use?

For Windows: rb-fchange
For Linux: rb-inotify
For OS X: rb-fsevent

···

On Thu, Jul 25, 2013 at 5:42 PM, Thomas Luedeke <lists@ruby-forum.com>wrote:

Having trouble finding a sound method that doesn't break when waiting
for a file to appear. My initial approach was this:

while File.exists?( "filename" ) == false
   sleep( 5 )
end

This worked for a while, but doesn't seem to be robust. I then tried
this:

until File.exists?( "filename" )
   sleep( 5 )
end

Which also doesn't seem to work.

I'm running Ruby 1.8.7. Is there a better way to do this?

--
Posted via http://www.ruby-forum.com/\.

Having trouble finding a sound method that doesn't break when waiting
for a file to appear. My initial approach was this:

while File.exists?( "filename" ) == false
   sleep( 5 )
end

This worked for a while, but doesn't seem to be robust.

What do you mean by not robust?

I then tried this:

until File.exists?( "filename" )
   sleep( 5 )
end

Which also doesn't seem to work.

`while !condition' and `until condition' are exactly the same
(like `if !condition' and `unless condition').

I'm running Ruby 1.8.7.

Unless you have a good reason to specifically use 1.8.7,
you should upgrade, it's not maintained by the Ruby core team anymore.

Is there a better way to do this?

There already are a couple of gems to watch directories or files,
have a look at e.g. the `fssm' gem or at `Guard', both should work
on all common platforms. But without knowing what exactly you want
to achieve, it's difficult to make a specific recommendation.

Example for fssm:

  require 'fssm'

  # monitor all txt files in the working directory
  FSSM.monitor('.', '*.txt') do
    create {|base, relative| puts "File #{relative} has been created" }
    update {|base, relative| puts "File #{relative} has been updated" }
    delete {|base, relative| puts "File #{relative} has been deleted" }
  end

Of course, instead of just printing a message you could trigger
arbitrary actions.

Regards,
Marcus

···

Am 26.07.2013 00:42, schrieb Thomas Luedeke:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

The only time I used something like this, it was waiting for a download,
so I could do something with the file afterwards.
If that's the case you might want to wait for the file's size to be
constant as well.

···

--
Posted via http://www.ruby-forum.com/.