Announcement

Collapse
No announcement yet.

Monitoring a folder for changes... stuck

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Monitoring a folder for changes... stuck

    Hey everyone,

    I am testing some thing withs AMS to monitor a folder for changes. (For now just alert when a new file is created/found in the folder.) I did not find anything related to monitor a folder, or comparing tables.

    The program runs in tray, and monitors a folder for changes. When a new file is found show a dialog with filepath and name.

    This is what I have so far but it isn't working as expected.

    On Preload:
    Code:
    -- Find all current files in folder and write it to a file.
    Files = File.Find("C:\\test\\", "*.*", true, false, nil, nil);
    TextFile.WriteFromTable(_TempFolder.."\\found.table", Files, false);
    
    -- Start the timer to monitor folder.
    Page.StartTimer(1000);
    On Timer:
    Code:
    -- Find files again and save into variable.
    findFiles = File.Find("C:\\test\\", "*.*", true, false, nil, nil);
    
    -- Read back the previous search results into STRING (because I had no idea how to find something in a table easily)
    readStringed = TextFile.ReadToString(_TempFolder.."\\monitor.table");
    
    -- Now loop through the CURRENT file list and look for every filename into the previous search results.
    for j,k in pairs(findFiles) do
    Finder = String.Find(readStringed, k, 1, false);
    
    -- If a filename is not found in the previous results, show dialog. Because a new file is found!
    if (Finder == -1) then
    Dialog.Message("Notice", k, MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    -- do something with the file here if you want.
    
    -- Insert the newly found file into the table (so we proceed with monitoring again)
    Table.Insert(findFiles, 1, k);
    TextFile.WriteFromTable(_TempFolder.."\\found.table", findFiles, false);
    
    end
    
    -- Read back the new table to prevent showing dialog of previously found files.
    readStringed = TextFile.ReadToString(_TempFolder.."\\found.table");
    
    end
    The issue is that I think this is not very effective, plus the dialog show stays in a loop.

    Anyone who can help me out with this code?

  • #2
    You could move the dialog out side the look set a count and build up a string and if that count it more the 0 get it to show the dialog with a sting of files and there paths, well I would us a dialogEX for that but that's just me better means of feed back to the user like using a parragrath object to load the string.

    You could also maybe check first when the folder was last modifiyed no point counting all them files and sub folders if the folder as not been changed in the first place.
    Plugins or Sources MokoX
    BunnyHop Here

    Comment


    • #3
      Originally posted by kingzooly View Post
      You could move the dialog out side the look set a count and build up a string and if that count it more the 0 get it to show the dialog with a sting of files and there paths, well I would us a dialogEX for that but that's just me better means of feed back to the user like using a parragrath object to load the string.

      You could also maybe check first when the folder was last modifiyed no point counting all them files and sub folders if the folder as not been changed in the first place.
      Well the dialog is just for debugging for now. The issue I am facing is how to compare tables. I already tried to find a function to get folder attributes, but there is no function like that...

      Comment


      • #4
        Originally posted by Tamer View Post
        Well the dialog is just for debugging for now. The issue I am facing is how to compare tables. I already tried to find a function to get folder attributes, but there is no function like that...
        http://www.indigorose.com/products/a...deo-tutorials/

        AutoPlay Advanced Chapter 2 section might help
        Plugins or Sources MokoX
        BunnyHop Here

        Comment


        • #5
          I have put together a sample application which allows you to monitor a folder. If one or more files are added, deleted or changed, the program will display a message like shown in the screen capture below.

          This is achieved by using two tables - one for the previous test, and one for the current situation. Each element in these tables is a table itself, with two fields: the filename and the last modification timestamp. If one filename is present in one table but not in the other, this means that this file was added or deleted, and if the timestamp is different, it is safe to assume that the file was updated. You can of course add an MD5 checksum field and compare those if you want to make sure that the file contents were indeed changed. If you want this kind of sophistication, you should be able to add this, using the project provided here as a starting point. This would also allow you to detect when a file is renamed, because the checksum wouldn't change. Currently, if a file is renamed while the folder is being monitored, you will get one mention for a deletion, and one for the newly added file.

          Click image for larger version

Name:	SCRN-2014-10-16-02.png
Views:	1
Size:	29.5 KB
ID:	284236

          Ulrich
          Last edited by Ulrich; 10-15-2014, 10:27 PM.

          Comment


          • #6
            Nice little example I new it be something like that but only just redownload AMS and takes me time to get swing of things
            Plugins or Sources MokoX
            BunnyHop Here

            Comment


            • #7
              Originally posted by Ulrich View Post
              I have put together a sample application which allows you to monitor a folder. If one or more files are added, deleted or changed, the program will display a message like shown in the screen capture below.

              This is achieved by using two tables - one for the previous test, and one for the current situation. Each element in these tables is a table itself, with two fields: the filename and the last modification timestamp. If one filename is present in one table but not in the other, this means that this file was added or deleted, and if the timestamp is different, it is safe to assume that the file was updated. You can of course add an MD5 checksum field and compare those if you want to make sure that the file contents were indeed changed. If you want this kind of sophistication, you should be able to add this, using the project provided here as a starting point. This would also allow you to detect when a file is renamed, because the checksum wouldn't change. Currently, if a file is renamed while the folder is being monitored, you will get one mention for a deletion, and one for the newly added file.

              [ATTACH=CONFIG]12662[/ATTACH]

              Ulrich
              That's a fine example Ulrich, thanks for you help.

              I need to read the code for a while before I will fully understand it.

              Regards

              Comment


              • #8
                Now I am trying to add limitation of file types. I tried *.exta|*.extb in the pattern, but that's not supported.

                I found the following snippet on this forum, but it's written for AMS 7.5, so I couldn't get it working.

                I think I am missing pairs() but that didn't worked either.

                Code:
                function DelimitedStringToTable(DelimitedString, Delimiter)
                	tbReturn = {};
                	local strWorking;
                	local nPos = nil;
                	local strData;
                	local nTableIndex = 1;
                	local nDelimiterLength = String.Length(Delimiter);
                	
                	if(nDelimiterLength < 1)then
                		tbReturn[nTableIndex] = DelimitedString;
                		return tbReturn;
                	end
                	
                	strWorking = DelimitedString;
                	nPos = String.Find(strWorking,Delimiter);
                	while(nPos ~= -1)do
                		strData = String.Left(strWorking,nPos-1);
                		tbReturn[nTableIndex] = strData;
                		nTableIndex = nTableIndex + 1;
                		local nLength = String.Length(strWorking);
                		strWorking = String.Right(strWorking,nLength - (nPos + (nDelimiterLength-1)));
                		nPos = String.Find(strWorking,Delimiter);
                	end
                	if(strWorking ~= "")then
                		tbReturn[nTableIndex] = strWorking;
                	end
                	
                	return tbReturn;
                end
                
                
                function FilesFind (files, drives)
                
                a = 0
                my_table = DelimitedStringToTable(files, "|")
                fix = DelimitedStringToTable(drives, "|")
                allfolder_files = {}
                
                for count,drive in fix do
                   
                   for index,file in my_table do
                    foundfile = File.Find(drive, file, true, false, nil, nil);
                
                     if (foundfile ~= nil) then 
                      
                      for f,g in foundfile do
                       a=a+1
                       Table.Insert(allfolder_files, a, g);
                      end
                     
                     end
                   
                   end
                
                end
                
                	return allfolder_files
                end
                Using this code, I can monitor multiple directories too.

                Comment


                • #9
                  Originally posted by Tamer View Post
                  Now I am trying to add limitation of file types. I tried *.exta|*.extb in the pattern, but that's not supported.

                  I found the following snippet on this forum, but it's written for AMS 7.5, so I couldn't get it working.

                  I think I am missing pairs() but that didn't worked either.

                  Code:
                  function DelimitedStringToTable(DelimitedString, Delimiter)
                  	tbReturn = {};
                  	local strWorking;
                  	local nPos = nil;
                  	local strData;
                  	local nTableIndex = 1;
                  	local nDelimiterLength = String.Length(Delimiter);
                  	
                  	if(nDelimiterLength < 1)then
                  		tbReturn[nTableIndex] = DelimitedString;
                  		return tbReturn;
                  	end
                  	
                  	strWorking = DelimitedString;
                  	nPos = String.Find(strWorking,Delimiter);
                  	while(nPos ~= -1)do
                  		strData = String.Left(strWorking,nPos-1);
                  		tbReturn[nTableIndex] = strData;
                  		nTableIndex = nTableIndex + 1;
                  		local nLength = String.Length(strWorking);
                  		strWorking = String.Right(strWorking,nLength - (nPos + (nDelimiterLength-1)));
                  		nPos = String.Find(strWorking,Delimiter);
                  	end
                  	if(strWorking ~= "")then
                  		tbReturn[nTableIndex] = strWorking;
                  	end
                  	
                  	return tbReturn;
                  end
                  
                  
                  function FilesFind (files, drives)
                  
                  a = 0
                  my_table = DelimitedStringToTable(files, "|")
                  fix = DelimitedStringToTable(drives, "|")
                  allfolder_files = {}
                  
                  for count,drive in fix do
                     
                     for index,file in my_table do
                      foundfile = File.Find(drive, file, true, false, nil, nil);
                  
                       if (foundfile ~= nil) then 
                        
                        for f,g in foundfile do
                         a=a+1
                         Table.Insert(allfolder_files, a, g);
                        end
                       
                       end
                     
                     end
                  
                  end
                  
                  	return allfolder_files
                  end
                  Using this code, I can monitor multiple directories too.
                  Here is the working function. I have added pairs() now.

                  Code:
                  function FilesFind (files, drives)
                  
                  a = 0
                  my_table = DelimitedStringToTable(files, "|")
                  fix = DelimitedStringToTable(drives, "|")
                  allfolder_files = {}
                  
                  for count,drive in pairs(fix) do
                     
                     for index,file in pairs(my_table) do
                      foundfile = File.Find(drive, file, true, false, nil, nil);
                  
                       if (foundfile ~= nil) then 
                        
                        for f,g in pais(foundfile) do
                         a=a+1
                         Table.Insert(allfolder_files, a, g);
                        end
                       
                       end
                     
                     end
                  
                  end
                  
                  	return allfolder_files
                  end

                  Comment

                  Working...
                  X