如何在批处理脚本中检查文件/目录是否可写

发布于 2024-08-16 17:41:03 字数 87 浏览 2 评论 0原文

在 bash 中,我会使用

[ -w ... ]

Windows 批处理文件的等效项是什么?

In bash, I would use

[ -w ... ]

What's the equivalent for Windows batch files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

春风十里 2024-08-23 17:41:03

据我所知,您可以查明该文件是否 存在与否,但除了尝试在其上写入之外,无法知道它是否可写。这不仅仅是没有 R 标志的问题;还涉及网络和 NTFS 权限(可能还涉及组策略)。

如果您可以重写代码,则可以通过 错误级别

As far as I know, you can find out whether the file exists or not, but there's no way to know if it's writeable, apart from trying to write on it. It's not only a matter of not having the R flag; network and NTFS permissions are involved as well (and probably group policies too).

If you can rewrite your code, you can capture the return code of the write operation trough the errorlevel.

梦言归人 2024-08-23 17:41:03

抱歉各位在这里插嘴。

这可能不是 100% 您正在寻找的内容,但我已将其与 Apache Tomcat 的正在使用的日志文件一起使用,并且它工作得绝对完美。

感谢@dbenham 提供的出色代码! https://stackoverflow.com/a/10520609/175063

SETLOCAL ENABLEDELAYEDEXPANSION
REM TOMCAT LOGS
FOR /r "D:\logs" %%X IN (*) DO (
    SET FileName=%%~nxX
    2>nul (   >>D:\logs\!FileName!" (call )) && (
    REM DO STUFF HERE
    SET ModDt=%%~tX
    FOR /f "tokens=1-3 delims=.:/ " %%j IN ("!ModDt!") DO SET FDate=%%l-%%j-%%k&Set RegDate=%%j-%%k-%%l
    IF "%CurrentDate%" NEQ "!FDate!" (
        IF %%~zX GTR 0 (
            ECHO ARCHIVING "D:\logs\!FileName!" >> %logfile%
            7za.exe -tzip -y a "D:\Zips\%COMPUTERNAME%-Tomcat-!RegDate!-compressed.zip" "D:\logs\!FileName!" && (
            DEL /Q "D:\logs\!FileName!"
            ) || (
                if "%ERRORLEVEL%" == "2" (
                    echo Zipping failed ^(exit status %ERRORLEVEL%^).  Trying again in 5 seconds...
                ) else (
                    echo Zip completed with warnings ^(most likely because a file was locked by another
                    echo process and had to be skipped^).  Trying again in 5 seconds...
                )
                del "D:\Zips\%COMPUTERNAME%-Tomcat-!RegDate!-compressed.zip" >NUL 2>&1
                PING 0.0.0.0 -n 6 -w 1000 >NUL
            )
        )
    )
    REM END OF UNLOCKED ZONE
    ) || (
    ECHO FILE IS LOCKED
    )
)

Sorry folks for chiming in here..

This may not be 100% what you are looking for, but I have used this with in-use log files for Apache Tomcat and it works absolutely perfectly.

Thanks to @dbenham for his awesome code! https://stackoverflow.com/a/10520609/175063

SETLOCAL ENABLEDELAYEDEXPANSION
REM TOMCAT LOGS
FOR /r "D:\logs" %%X IN (*) DO (
    SET FileName=%%~nxX
    2>nul (   >>D:\logs\!FileName!" (call )) && (
    REM DO STUFF HERE
    SET ModDt=%%~tX
    FOR /f "tokens=1-3 delims=.:/ " %%j IN ("!ModDt!") DO SET FDate=%%l-%%j-%%k&Set RegDate=%%j-%%k-%%l
    IF "%CurrentDate%" NEQ "!FDate!" (
        IF %%~zX GTR 0 (
            ECHO ARCHIVING "D:\logs\!FileName!" >> %logfile%
            7za.exe -tzip -y a "D:\Zips\%COMPUTERNAME%-Tomcat-!RegDate!-compressed.zip" "D:\logs\!FileName!" && (
            DEL /Q "D:\logs\!FileName!"
            ) || (
                if "%ERRORLEVEL%" == "2" (
                    echo Zipping failed ^(exit status %ERRORLEVEL%^).  Trying again in 5 seconds...
                ) else (
                    echo Zip completed with warnings ^(most likely because a file was locked by another
                    echo process and had to be skipped^).  Trying again in 5 seconds...
                )
                del "D:\Zips\%COMPUTERNAME%-Tomcat-!RegDate!-compressed.zip" >NUL 2>&1
                PING 0.0.0.0 -n 6 -w 1000 >NUL
            )
        )
    )
    REM END OF UNLOCKED ZONE
    ) || (
    ECHO FILE IS LOCKED
    )
)
醉城メ夜风 2024-08-23 17:41:03

这是旧的,但我没有在这里找到它:

lock.bat

(
  echo.123
  pause
) > "1.txt"

test.bat

move /Y "1.txt" "1.txt" >nul 2>nul
echo.%ERRORLEVEL%

move 不会更改文件创建/更改/访问次数。

https://www.dostips.com/forum/viewtopic.php?t= 5542


要检查目录的访问权限,您可以使用 rename 命令以相同的方式进行操作:

rename "path\1.txt" "1.txt" >nul 2>nul
echo.%ERRORLEVEL%

要测试它,只需添加 path\ - 拒绝所有人的所有

This is old but I didn't found it here:

lock.bat

(
  echo.123
  pause
) > "1.txt"

test.bat

move /Y "1.txt" "1.txt" >nul 2>nul
echo.%ERRORLEVEL%

The move does not change a file create/change/access times.

https://www.dostips.com/forum/viewtopic.php?t=5542


To check the access rights to a directory you can go the same way with the rename command:

rename "path\1.txt" "1.txt" >nul 2>nul
echo.%ERRORLEVEL%

To test it just add for path\ - deny all for Everyone.

心凉 2024-08-23 17:41:03

这样做

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.GetFile(strFile)
If Not objFile.Attributes And 1 Then
   WScript.Echo "The file is Read/Write."
Else
   WScript.Echo "The file is Read-only."
End If

你可以使用 vbscript save as check.vbs 并在命令行上

c:\test> cscript //nologo check.vbs myfile

you can do it like this using vbscript

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.GetFile(strFile)
If Not objFile.Attributes And 1 Then
   WScript.Echo "The file is Read/Write."
Else
   WScript.Echo "The file is Read-only."
End If

save as check.vbs and on command line

c:\test> cscript //nologo check.vbs myfile
此刻的回忆 2024-08-23 17:41:03

我发现的最可靠的方法是将 NUL 复制到目标目录中的文件中。

如果该目录不存在或者当前用户不可写入该目录,则将以 1 退出。

我使用 /B 因为 NUL 是二进制文件,而 /Y 则覆盖现有文件。

copy /Y /B NUL "c:\test\test.txt"
echo %ERRORLEVEL%

The most reliable method I have found is to copy NUL to a file in the target directory.

This will exit with 1 if the directory does not exist or if the directory is not writable by the current user.

I use /B as NUL is binary and /Y to overwrite the existing file.

copy /Y /B NUL "c:\test\test.txt"
echo %ERRORLEVEL%
不美如何 2024-08-23 17:41:03
ls -l foo.txt

输出 -r--r--r-- 表示不可写文件
输出 -rw-r--r-- 对于可写文件,

您可以存储该值并检查第三个字符是否为“w”(表示可写)或“-”(表示不可写)。

在条件语句中使用诸如 %myVar:~2,1% 之类的语法。

不确定这对操作系统有何影响。

ls -l foo.txt

outputs -r--r--r-- for a not writable file
outputs -rw-r--r-- for a writable file

you could store the value and check if the 3rd character is "w" for writable or "-" for not writable.

using some syntax like %myVar:~2,1% in a conditional statement.

not sure how OS dependent this would be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文