PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → WD18 Trackfile()
WD18 Trackfile()
Débuté par DW, 22 avr. 2014 16:39 - 15 réponses
Posté le 22 avril 2014 - 16:39
Hello All,

I using HFCS to store documents of various types on the server. I put the document in a memo save it in the DB then with a stored procedure remove it from the db and store on the hardrive of the server saving on a link to the document.

This all works fine.

The problem I am facing:
When a user wants to open the document I put into the in temp folder and open it with shellexecute.

From this point I have no way to track when the user close the document.I would like to remove the document from the temp file after viewing.

If the user make changes I can trap that with TrackFile but can't trap if a user opens and closes with out a save.

Any suggestions?

Thank you,

DW
Posté le 22 avril 2014 - 16:45
Just my point of view:

Would it be possible to track the PID of the program used to view the document. If this PID stops to exists. U can check if the file is changed or whatever u like.
Posté le 22 avril 2014 - 17:59
Hello PIM,

I was leaning that way but can not figure out how to get it in windev.

ShellExecute does not give you the sysHandle or ProcessID of the new window.

I tried to loop thru all open windows that are visible to get the window handle but if the user some how has 2 documnets with the same name open could cause issues.


DW
Posté le 22 avril 2014 - 18:03
DW,

Another option is to use low level file functions to see if the file can be accessed and/or deleted. This could be done in a timer or in some cleanup code when the application ends, for example.
Posté le 22 avril 2014 - 18:16
Hello JP,

Can you give me a little more detail?

DW
Posté le 22 avril 2014 - 19:32
DW,

Took me some time to find as well but i think u can use this :
http://doc.windev.com/…

It will return a pid u can use to check it.

Pim
Posté le 22 avril 2014 - 19:51
Hi

@Pim, I do not think that would work, as word (by example) can have 10 documents open at the same time, stay open for weeks, even though YOUR document has been closed after 5 minutes

@Dennis
You can probably do a fopen on the file with a foAutomaticDeletion flag, then close it... This way, if the file is still open by something else, nothing happens. If not, it's deleted... If you do that in a "cleaning procedure" running against the list of opened files, you can delete anything at anypoint in time

Best regards
Posté le 22 avril 2014 - 20:05
@Fabrice As far as i know Word (And most other applications) create a instance for every document. The PID will be only used once (per boot). So u can identify the process with it. So also the document u opened.
Posté le 22 avril 2014 - 20:09
Hi DW,

Basically you try and open the file using a low level file function e.g. FOPEN() see - http://doc.windev.com/en-US/…

If you can open it exclusively then the file is free and thus not in use. If you cannot open it then it must still be in use. Have a look at the mode for locking files parameters. Fabrice has indicated the same solution using the foAutomaticDeletion parameter i.e. if it can be opened then it will also be deleted when you issue the FCLOSE() on it. This saves one step.

Where to put this code? Well you can have a cleanup routine which is run regularly or on application exit, as one example.
Posté le 22 avril 2014 - 21:32
Hi , Why do you want to remove the temporary file ? I have the same kind o thing in my app. If you make sure the filename is unique . (That is I save as abinairy immage not a memo)

Put GetGUID(guidRough ) into the name, then it isnot really a problem if the are there. But that is my opinion.

regards

Allard
Posté le 22 avril 2014 - 21:40
Depending on the number and size of the files. I think it's always better to remove them. Leaving them in the temp folder is kind of the default (looking at the mess most programs leave there). But still i think it's cleaner to remove them. But as so many thinks there is no good or wrong in this case.
Posté le 23 avril 2014 - 07:02
DW,

Just a suggestion: I create a temp folder within the standard Windows temp folder and name it according to our company name or the application name, for example. Then within that folder I create a user specific folder using the users login name and machine name. Then on application exit I delete this user specific folder and then try to delete the parent folder of all user specific folders. This system caters for creating temp files and removing then in one go each time a user exists the app. Plus it keeps all my application temp files in one place, neat and tidy, and can be manually removed if needed. Lastly, it allows us to see if we have made mistakes with temp file usage since we can easily look into that temp folder to see what is going on. Just my 2c.
Posté le 24 avril 2014 - 05:16
ExeRun() is your friend. From your main program, use ExeRun() to launch a small wrapper program that launches your document viewer. The wrapper program can wait for the temp file to be accessible and then delete it. You can launch as many wrapper programs as you want meaning multiple documents open at once. I have used this approach with Visual Foxpro to handle PDF documents but it should work equally well with WinDev.

Stewart Crisler
Posté le 24 avril 2014 - 06:07
I think you will have to use WinAPI for this.

I hope this will help you: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261%28v=vs.85%29.aspx

It would be better to first extract the file in question to a directory and then set Folder Change Notification for that folder.

Now if the file that is opened in another software is modified your program will get the required notification and you can do whatever you want to do then!

HTH

Yogi Yang
Posté le 29 avril 2014 - 01:26
Hello All,

Just to give final conclusion.

When a user calls for a document to be opened I first bring it over network thru the DB. sTempFolder is string = fTempPath()

Make 2 copies in the Temp folder on the users computer. 1 with original file name. 1 with a unique name.

I open the Document using shell execute.

I call a procedure set on a timer every three seconds that will compare the 2 files to see if any changes happen. fCompare(sDocPath,sFileToCompare)

On the first run of the procedure I have a function that uses API to get the handle of the document I opened. (Since I gave it a unique name there could only be one.)

In the middle of the procedure it will compare the 2 files and if changes are made ask the user to save the modified version back to the DB and delete both copies.

At the end of the timer procedure I check to see if the handle is valid with SysValidHandle(nHandle). if the user closes the document without a save it will delete both copies.

DW
Posté le 29 avril 2014 - 08:30
Hi,

Instead of keeping 2 copies, you could use a hash of the file and re-calculate the hash to see if the document really has changed or not.

You can use HashFile function

http://doc.pcsoft.fr/en-US/?1000007112&name=hashfile_function

Danny