如果给定的文件或目录被锁定(由任何进程使用),如何检入命令行? [英] How to check in command-line if a given file or directory is locked (used by any process)?

查看:167
本文介绍了如果给定的文件或目录被锁定(由任何进程使用),如何检入命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道在尝试对此类文件执行任何操作之前。

I need to know that before any attempt to do anything with such file.

推荐答案

Windows

但是检测一个文件是否被另一个进程写入并不难。

But detecting if a file is being written to by another process is not difficult.

@echo off
2>nul (
  >>test.txt echo off
) && (echo file is not locked) || (echo file is locked)

我使用下面的测试脚本从另一个窗口档案。

I use the following test script from another window to place a lock on the file.

(
  >&2 pause
) >> test.txt

当我从一个窗口运行第二个脚本,然后从第二个脚本窗口,我得到我的锁定消息。一旦我在第一个窗口中按< Enter> ,如果我重新运行第一个脚本,我会收到unlocked消息。

When I run the 2nd script from one window and then run the 1st script from a second window, I get my "locked" message. Once I press <Enter> in the 1st window, I get the "unlocked" message if I rerun the 1st script.

说明

Explanation

每当命令的输出重定向到文件时,必须打开以进行写访问。 Windows CMD会话将尝试打开该文件,即使该命令不产生任何输出。

Whenever the output of a command is redirected to a file, the file of course must be opened for write access. The Windows CMD session will attempt to open the file, even if the command does not produce any output.

>> 重定向操作符以附加模式打开文件。

The >> redirection operator opens the file in append mode.

因此> >将尝试打开文件,它不会写入文件(假设echo已经关闭),然后它关闭文件。该文件不以任何方式修改。

So >>test.txt echo off will attempt to open the file, it writes nothing to the file (assuming echo is already off), and then it closes the file. The file is not modified in any way.

大多数进程在打开文件以进行写访问时锁定文件。 (有一些OS系统调用允许打开一个文件,以共享模式写入,但这不是默认值)。因此,如果另一个进程已经有test.txt锁定写入,则重定向将失败,并将以下错误消息发送到stderr - 进程无法访问该文件,因为它正被另一个进程使用。。此外,重定向失败时将生成错误代码。如果命令和重定向成功,则返回成功代码。

Most processes lock a file whenever they open a file for write access. (There are OS system calls that allow opening a file for writing in a shared mode, but that is not the default). So if another process already has "test.txt" locked for writing, then the redirection will fail with the following error message sent to stderr - "The process cannot access the file because it is being used by another process.". Also an error code will be generated upon redirection failure. If the command and the redirection succeed, then a success code is returned.

只需将 2> nul 添加到命令不会阻止错误消息,因为它重定向命令的错误输出,而不是重定向。这就是为什么我把括号中的命令括起来,然后将错误输出重定向到nul外的括号。

Simply adding 2>nul to the command will not prevent the error message because it redirects the error output for the command, not the redirection. That is why I enclose the command in parentheses and then redirect the error output to nul outside of the parens.

所以错误消息被有效隐藏,但错误代码是仍然在括号外传播。标准的Windows && || 运算符用于检测括号内的命令是成功还是失败。假设 echo off 永远不会失败,因此失败的唯一可能原因是重定向失败。

So the error message is effectively hidden, but the error code is still propagated outside of the parens. The standard Windows && and || operators are used to detect whether the command inside the parens was successful or failed. Presumably echo off will never fail, so the only possible reason for failure would be the redirection failed. Most likely it fails because of a locking issue, though technically there could be other reasons for failure.

这是一个好奇的功能,Windows没有设置%ERRORLEVEL %动态变量在重定向失败时出错,除非使用 || 运算符。 (请参阅 Windows中的文件重定向和%errorlevel%)。因此, || 运算符必须在某个低级别读取返回的错误代码,而不是通过%ERRORLEVEL%变量。

It is a curious "feature" that Windows does not set the %ERRORLEVEL% dynamic variable to an error upon redirection failure unless the || operator is used. (See File redirection in Windows and %errorlevel%). So the || operator must read the returned error code at some low level, not via the %ERRORLEVEL% variable.

使用这些技术检测重定向失败在批处理上下文中非常有用。它可以用于建立允许在并行进程中串行化多个事件的锁。例如,它可以使多个进程在相同时间安全地写入相同的日志文件。 如何在Windows下共享日志文件?

Using these techniques to detect redirection failure can be very useful in a batch context. It can be used to establish locks that allow serialization of multiple events in parallel processes. For example, it can enable multiple processes to safely write to the same log file at the "same" time. How do you have shared log files under Windows?

EDIT

我不知道Windows如何实现这一点,也许有一个锁。但是如果进程具有涉及该文件夹的活动目录,则不能重命名该文件夹。这可以很容易地使用

Regarding locked folders. I'm not sure how Windows implements this, perhaps with a lock. But if a process has an active directory involving the folder, then the folder cannot be renamed. That can easily be detected using

2>nul ren folderName folderName && echo Folder is NOT locked || echo folder is LOCKED






EDIT


EDIT

我已经学会了(call) (有空格)效果,保证成功,ERRORLEVEL设置为0.和(call) (没有空格)是一个快速命令没有副作用,保证使用ERRORLEVEL 1失败。

I have since learned that (call ) (with a space) is a very fast command without side effects that is guaranteed to succeed with ERRORLEVEL set to 0. And (call) (without a space) is a fast command without side effects that is guaranteed to fail with ERRORLEVEL 1.

所以我现在使用以下方法检查文件是否被锁定:

So I now use the following to check if a file is locked:

2>nul (
  >>test.txt (call )
) && (echo file is not locked) || (echo file is locked)

这篇关于如果给定的文件或目录被锁定(由任何进程使用),如何检入命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆