Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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,
   result},
  (
   lastModified = Table[{}, {Length[fileNames]}];
   result = Table[{}, {Length[fileNames]}];
   taskId = 
    CreateScheduledTask[
     Table[(With[{
        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]}];
   StartScheduledTask[taskId];
   {taskId, Dynamic[result]}
   )]

Test[SystemDialogInput["Directory"]]

How can I solve this?

share|improve this question
1  
Could you be more specific than "it doesn't work"? –  Sjoerd C. de Vries Mar 13 '13 at 8:55
    
@SjoerdC.deVries I edited my question,can you check it once. –  subbu Mar 13 '13 at 9:05
    
@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. >. –  Albert Retey Mar 13 '13 at 9:33
    
@AlbertRetey again,I updated my question with updated code..could you check it once.. –  subbu Mar 13 '13 at 9:47
2  
@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. –  Sjoerd C. de Vries Mar 13 '13 at 10:00
show 5 more comments

1 Answer

up vote 3 down vote accepted

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

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

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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