# How can I check which file is modified in specific directory?

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



How can I solve this?

-
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
@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

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

Test[directoryPath_] :=
Module[
{
fileNames = FileNames["*", directoryPath],
lastModified,
booleanValue = {}
},
lastModified = Table[{}, {Length[fileNames]}];
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]}
];

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.