I want to find which files are recently modified in a specific directory. For this purpose, I wrote a function named Test.

The function's argument is DirectoryPath, and it returns file names whose file dates have been modified. In this example the functions runs once every five seconds.

For this purpose I wrote the following code, but it shows all the file names that are in the directory. That means it doesn't check the ModificationDate with lastModified.

Test[directoryPath_] := Module[{taskId,
   fileNames = FileNames["*", SystemDialogInput["Directory"]],
   lastModified = Table[{}, {Length[fileNames]}];
   result = Table[{}, {Length[fileNames]}];
   taskId = 
        i = i, 
        modificationDate = 
          Table[FileDate[Part[fileNames, j], "Modification"],
            {j, 1, Length[fileNames]}]
       (If[(Part[lastModified, i] === Part[modificationDate, i]),
         (Part[result, i] =  ""), 
         (Part[lastModified, i] = Part[modificationDate, i];
          Part[result, i] = Part[fileNames, i])])]),
      {i, 1, Length[fileNames]}],
     {OptionValue[ScanRate], \[Infinity]}];
   {taskId, Dynamic[result]}


How can I solve this?

  • 1
    $\begingroup$ Could you be more specific than "it doesn't work"? $\endgroup$ – Sjoerd C. de Vries Mar 13 '13 at 8:55
  • $\begingroup$ @SjoerdC.deVries I edited my question,can you check it once. $\endgroup$ – subbu Mar 13 '13 at 9:05
  • $\begingroup$ @subbu: I think your code has at leat 2 problems and seems to be more complicated than what would be necessary to demonstrate your problem. Have you any indication that this only fails when run as a ScheduledTask? If not, then you'd better simplify the code to run without it. Here are some potential problems that you want to investigate: a) using fileNames in the RHS of the local variable definitions of Module most probably doesn't do what you expect. b) comparing the result of FileDate doesn't look right, I'd rather suggest to convert it to an AbsoluteTime and check with e.g. >. $\endgroup$ – Albert Retey Mar 13 '13 at 9:33
  • $\begingroup$ @AlbertRetey again,I updated my question with updated code..could you check it once.. $\endgroup$ – subbu Mar 13 '13 at 9:47
  • 2
    $\begingroup$ @subbu You should not update the version of the code in your question each time we find an error in it. In the end the whole question doesn't make sense anymore. Furthermore, I spent considerable time in improving the horrible layout of the original code and your replacement nullifies that all. $\endgroup$ – Sjoerd C. de Vries Mar 13 '13 at 10:00

Here is a minimally changed version of your program that works:

Test[directoryPath_] :=
   fileNames = FileNames["*", directoryPath],
   booleanValue = {}
  lastModified = Table[{}, {Length[fileNames]}];
  taskId =
    booleanValue =
      With[{j = j, 
        modificationDate = FileDate[fileNames[[j]], "Modification"]},
       If[(lastModified[[j]] === modificationDate),
        lastModified[[j]] = modificationDate;
      {j, 1, Length[fileNames]}
    {5, \[Infinity]}

I moved the initialization of lastModified outside the local variable definition section, because you can't define local variable in terms of other local variables. I changed a == into a = and a == into a ===.

I also removed all non-functional ( ) pairs in your code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.