R -检查文件是否打开/关闭,以及由哪个用户打开/关闭

时间:2022-02-27 16:59:43

I have a document that is used by multiple people, and we have to continually check if the file is in use and by who.

我有一个由多人使用的文档,我们必须不断检查文件是否在使用中,以及由谁使用。

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

我想知道在R中是否有我可以获得.xlsx文件的状态,如果它是关闭的还是打开的,以及是谁打开了这个文件。

I would then push this result to a HTML page which would refresh on a regular basis, this should then remove the need for the manual checking.

然后,我将这个结果推送到一个HTML页面,该页面将定期刷新,这样就不需要手动检查了。

1 个解决方案

#1


1  

Here is a starting point you might consider. This option is using C code you can compile in with R Cmd and call from R.

这里有一个你可能会考虑的起点。这个选项使用C代码,您可以用R Cmd编译并从R调用。

In a file "islocked.c" paste this:

在一个文件中“islocked。c”粘贴:

#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')
Change into the folder you saved the file above
From your command prompt c:\ type the following
"Program File\R\R-3.1.2\bin\r" CMD SHLIB islocked.c
It shouldn't throw any errors or warnings and create a .o and .dll file as a result.

打开命令提示符(windows |查找| 'cmd'),将文件从命令提示符c中保存到上面的文件夹中。它不应该抛出任何错误或警告,并创建一个.o和.dll文件作为结果。

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

现在进入R: dyn.load('c:\pathtothe_c_file is locke. dll')结果->。C('testLock', islocked=as.integer(0), filename="d:\tools\r\test.dll")结果$islocked [1] 1

The dll is locked by R so this should return 1. Try the .o file and it should return 0. There is an API in Windows 7 and newer called IFileInUse which may return the process and possibly the user that has the file open that you might review if you need more information.

dll被R锁定,所以这个应该返回1。尝试.o文件,它应该返回0。Windows 7中有一个叫做IFileInUse的API,它可以返回进程,如果需要更多信息,还可以返回打开文件的用户。

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

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

A utility from Microsoft that runs on a command line you can shell to from R that may produce what you need, if you are able to install tools on the server: http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

如果您能够在服务器上安装工具:http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx,那么可以从R调用一个运行在命令行上的Microsoft实用程序,它可以生成您需要的内容

#1


1  

Here is a starting point you might consider. This option is using C code you can compile in with R Cmd and call from R.

这里有一个你可能会考虑的起点。这个选项使用C代码,您可以用R Cmd编译并从R调用。

In a file "islocked.c" paste this:

在一个文件中“islocked。c”粘贴:

#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')
Change into the folder you saved the file above
From your command prompt c:\ type the following
"Program File\R\R-3.1.2\bin\r" CMD SHLIB islocked.c
It shouldn't throw any errors or warnings and create a .o and .dll file as a result.

打开命令提示符(windows |查找| 'cmd'),将文件从命令提示符c中保存到上面的文件夹中。它不应该抛出任何错误或警告,并创建一个.o和.dll文件作为结果。

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

现在进入R: dyn.load('c:\pathtothe_c_file is locke. dll')结果->。C('testLock', islocked=as.integer(0), filename="d:\tools\r\test.dll")结果$islocked [1] 1

The dll is locked by R so this should return 1. Try the .o file and it should return 0. There is an API in Windows 7 and newer called IFileInUse which may return the process and possibly the user that has the file open that you might review if you need more information.

dll被R锁定,所以这个应该返回1。尝试.o文件,它应该返回0。Windows 7中有一个叫做IFileInUse的API,它可以返回进程,如果需要更多信息,还可以返回打开文件的用户。

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

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

A utility from Microsoft that runs on a command line you can shell to from R that may produce what you need, if you are able to install tools on the server: http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

如果您能够在服务器上安装工具:http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx,那么可以从R调用一个运行在命令行上的Microsoft实用程序,它可以生成您需要的内容