The event_handler will pass 10 arrays to the file_added method. One for
each file. Here's the whole script

require 'dbi'
require 'yaml'
require 'win32/changenotify'
require 'win32/file'
include Win32

class Fsm

  attr_accessor :server, :db, :user, :password, :folder_to_watch,
:count

  def initialize(folder)

    dbconfig = YAML::load(IO.read('config.yml'))

    @server = dbconfig['server']
    @db = dbconfig['db']
    @user = dbconfig['user']
    @password = dbconfig['password']
    @folder_to_watch = folder
    @count = 0
    @dbh = DBI.connect("DBI:ADO:Provider=SQLNCLI; Data
Source=#{@server};Database=#{@db};uid=#{@user}; pwd=#{@password};
{'NonBlocking'=>true}")

  end

  def tmf_insert_new_file(name, length, creation_time, directory,
extension, fullname, is_read_only, last_access_time, last_write_time,
file_type, parent_directory)
   sql = "DECLARE	@return_value int EXEC	@return_value =
sp_UpdateMasterFileList @Name = ?,	@Length = ?,	@CreationTime =
?,	@DirectoryName = ?,	@Extension = ?,	@FullName =?,	@IsReadOnly =
?,	@LastAccessTime = ?,	@LastWriteTime = ?,	@FileType =
?,	@ParentDirectory = ? SELECT	'Return Value' = @return_value"

   myKey = ""
    sth = @dbh.execute(sql, name, length, creation_time, directory,
extension, fullname, is_read_only, last_access_time, last_write_time,
file_type, parent_directory)
    myKey = sth.fetch
    sth.finish

    @dbh.commit()

    return myKey.to_s

    rescue DBI::DatabaseError => e
     puts "An error occurred in tmf_insert_new_file"
     puts "Error code: #{e.err}"
     puts "Error message: #{e.errstr}"

  end

  def monitor
    filter = ChangeNotify::FILE_NAME | ChangeNotify::DIR_NAME |
ChangeNotify::LAST_WRITE

    cn = ChangeNotify.new(@folder_to_watch, true, filter)

    cn.wait{|arr|

      event_handler(arr)

    } while true

   rescue Exception => e
      puts e
  end

  def event_handler(arr)

    if arr[0][0].to_s.include? "added"
      #puts @count+=1
      #puts "Adding file " + arr[0][1].to_s
      file_added(arr)
    end

   rescue Exception => e
    puts e

  end

  def file_added(arr)

        date_format_str = "%Y%m%d %X"
        filename = arr[0][1].to_s

        if filename.include?"/"
          filename.gsub!("/","\\")
        end

        if @folder_to_watch.include?"/"
         @folder_to_watch.gsub!("/","\\")
        end

        fullname = @folder_to_watch + filename
        stat = File::Stat.new(fullname)
        file_type = File.ftype(fullname).capitalize
        dirname = File.dirname(fullname)

        if file_type.include? "File"

            myKey = tmf_insert_new_file(File.basename(filename),
stat.size, stat.ctime.strftime(date_format_str), dirname,
File.extname(fullname), fullname,0,
stat.atime.strftime(date_format_str),
stat.mtime.strftime(date_format_str), file_type,
File.dirname(fullname))

        else

          myKey = tmf_insert_new_file(File.basename(filename), 0,
stat.ctime.strftime(date_format_str), fullname, 'None', fullname, 0,
stat.atime.strftime(date_format_str),
stat.mtime.strftime(date_format_str), "Directory", dirname)

        end

    rescue Exception =>e
      puts "Error in file_added method."
      puts e

  end

end

MyFSM = Fsm.new('C:\\')
MyFSM.monitor()







Phrogz wrote:
> lrlebron / gmail.com wrote:
> > I am running Ruby 1.8.5 and windows XP. I am working on a program that
> > monitors a directory and updates and updates a database based on the
> > changes. At the moment I have some code to handle files that are added.
> > Basically I have 3 methods:
> >
> > Pseudo code
> >
> > def monitor
> >    event_handler(array)
> > end
> >
> > event_handler(array)
> >     p array
> >     If file is added
> >         file_added(arr)
> >    end
> > end
> >
> > file_added(array)
> >   #Insert into database
> > end
> >
> > Here's my problem. Let's say I drag 10 files into the directory. If I
> > only have the event_handler print the array I get all 10. However, when
> > the event_handler calls the file_added method I only get 5 or 6 files
> > inserted in the database. The code is not throwing exceptions.
>
> You'd better post code to your file_added method, and ensure that the
> same array is getting passed in, and still has 10 items in it.