R - check if the file is Open / Closed and by which user

I have a document that is used by several people, and we need to constantly check whether the file is used and by whom.

I was wondering if there is anyway in R that I can get the status of the .xlsx file if it is closed or open, and who has the file open.

Then I would pull this result into an HTML page that will be updated on a regular basis, then this should eliminate the need for manual validation.

+6
source share
1 answer

Here is a starting point that you might consider. This option uses C code, which you can compile with R Cmd and call from R.

In the "islocked.c" file, paste this:

#include <stdio.h> #include <share.h> void testLock(int *locked, char **filename) { FILE *stream; if( (stream = _fsopen( *filename, "wt", _SH_DENYWR )) != NULL ) { fclose( stream ); *locked = 0; } else { *locked = 1; } } 

Open a command prompt (windows | find | 'cmd)

Go to the folder where you saved the file above
At the c: \ prompt, type the following command "Program File\R\R-3.1.2\bin\r" CMD SHLIB islocked.c
It should not throw any errors or warnings and create .o and .dll files as a result.

Now in R:
dyn.load('c:\pathtothe_c_file\islocked.dll') result->.C('testLock', islocked=as.integer(0), filename="d:\tools\r\test.dll") result$islocked [1] 1

The DLL is blocked by R, so this should return 1. Try the .o file, and it should return 0. Windows 7 has an API, and newer has IFileInUse, which can return the process and, possibly, the user who has the file open that you can see if you need more information.

IsFileInUse API: http://msdn.microsoft.com/en-us/library/windows/desktop/ee330722%28v=vs.85%29.aspx

The Microsoft utility that runs on the command line, you can use the shell from R, which can create what you need if you can install the tools on the server: http://technet.microsoft.com/en-us/sysinternals/bb896655 .aspx

+1
source

Source: https://habr.com/ru/post/980527/


All Articles