Track Directory
fTrackDirectory (Function)
When there is a requirement to track the files creation, modification, deletion of the files in a specified directory we can use the function fTrackDirectory.
This function detects the modifications performed on the content of a directory. Only the modifications performed on the files found in the directory are detected. In case of modification, a specific procedure is run in a thread.
Example
// Name of directory to track
sDirectoryName is string = "C:\Temp\MyDir"
// The ProcessDirModification procedure will be called
// whenever a file or directory found in the "C:\Temp\MyDir" directory is modified.
IF fTrackDirectory(sDirectoryName, ProcessDirModification, ftCreateFile ...
+ ftModifyFile + ftDeleteFile + ftRename) THEN
// Inform the user that the directory will be tracked
Info("The " + sDirectoryName + " directory will be tracked.")
ELSE
// Inform the user that the directory will not be tracked
Info("The " + sDirectoryName + " directory will not be tracked.")
END
sDirectoryName is string = "C:\Temp\MyDir"
// The ProcessDirModification procedure will be called
// whenever a file or directory found in the "C:\Temp\MyDir" directory is modified.
IF fTrackDirectory(sDirectoryName, ProcessDirModification, ftCreateFile ...
+ ftModifyFile + ftDeleteFile + ftRename) THEN
// Inform the user that the directory will be tracked
Info("The " + sDirectoryName + " directory will be tracked.")
ELSE
// Inform the user that the directory will not be tracked
Info("The " + sDirectoryName + " directory will not be tracked.")
END
// Code of the procedure
PROCEDURE ProcessDirModification(sDirectoryName, sFileName, nAction, sFormerFileName)
ExecuteMainThread(AddTable,sDirectoryName, sFileName, nAction, sFormerFileName)
PROCEDURE ProcessDirModification(sDirectoryName, sFileName, nAction, sFormerFileName)
ExecuteMainThread(AddTable,sDirectoryName, sFileName, nAction, sFormerFileName)
// Procedure that acts on the main thread
PROCEDURE AddTable(sDirectoryName, sFileName, nAction, sFormerFileName)
sActionDesignation is string
// The designation of the action depends on nAction
SWITCH nAction
CASE ftCreateFile: sActionDesignation = "Creating files"
CASE ftDeleteFile: sActionDesignation = "Deleting files"
CASE ftModifyFile: sActionDesignation = "Modifying files"
CASE ftRename: sActionDesignation = "Renaming files"
END
// Add a line containing the information about the modification
// into the TABLE_MODIFICATIONS table
TableAddLine(TABLE_MODIFICATIONS, sDirectoryName, sFileName, , ...
sActionDesignation, sFormerFileName)
sActionDesignation is string
// The designation of the action depends on nAction
SWITCH nAction
CASE ftCreateFile: sActionDesignation = "Creating files"
CASE ftDeleteFile: sActionDesignation = "Deleting files"
CASE ftModifyFile: sActionDesignation = "Modifying files"
CASE ftRename: sActionDesignation = "Renaming files"
END
// Add a line containing the information about the modification
// into the TABLE_MODIFICATIONS table
TableAddLine(TABLE_MODIFICATIONS, sDirectoryName, sFileName, , ...
sActionDesignation, sFormerFileName)
Syntax
<Result> = fTrackDirectory(<Directory Name> , <Procedure Name> [, <Modifications to Notify> [, <Sub-directory>]])
<Result>: Boolean
- True is the directory is tracked,
- False otherwise. To find out the error details, use ErrorInfo.
<Directory Name>: Character string (with quotes)
Full name of the directory to track.
<Procedure Name>: Character string (with or without quotes)
Name of the WLanguage procedure ("callback" procedure) that will be called when a change is detected in the specified directory.
This procedure has the following format:
where:PROCEDURE <Procedure Name>(<Name of Tracked Directory>
[, <Name of Modified Element>
[ <, <Action> [, <Former Name>])
- <Name of Tracked Directory>: character string corresponding to the full name of the tracked directory.
- <Name of Modified Element>: chaaracter string indicating the name of the modified file or directory.
- <Action>: Integer constant that can take the following values:
ftCreateFile The <Name of Modified Element> file or directory was created in <Directory>. ftModifyFile The <Name of Modified Element> file or directory was modified in <Directory>. ftRename The <Name of Modified Element> file or directory was renamed in <Directory>. ftDeleteFile The <Name of Modified Element> file or directory was deleted from <Directory>. - <Former Name>: Former name of the modified file or directory if the action performed is a rename operation.
Special cases
- fTrackDirectory tracks the directory content only. The changes of directory name or location are ignored.
- To stop tracking the directory, use fTrackStop and fTrackStopAll.
- To track the modifications performed in a file, use fTrackFile.
- Limitation: Only 5 directories can be tracked at the same time.
Comments
Post a Comment