【问题标题】:Batch: Checking if a file is locked does not work批处理:检查文件是否被锁定不起作用
【发布时间】:2014-03-24 08:56:11
【问题描述】:

我编写了以下批处理来执行以下步骤:

  • 检查服务器上的文件是否被其他用户打开
  • 备份文件
  • 打开文件

2>nul ( >>test.xlsx (call )) if %errorlevel% == 1 goto end

@echo off
rem get date, make if file name friendly
FOR /F "tokens=1-4 delims=/ " %%i in ('date/t') do set d=%%j-%%k-%%l@%%i@
rem get time, make if file name friendly
FOR /F "tokens=1-9 delims=:. " %%i in ('time/t') do set t=%%i_%%j_%%k%%l

set XLSX=%d%%t%.xlsx
ren Test.xlsx %xlsx%
xcopy *.xlsx J:\Test\Backup

ren %xlsx% Test.xlsx

call Test.xlsx

:end

问题是,试图检查文件是否被锁定的行在服务器上不起作用。

谁能帮我找出批次中的错误?

【问题讨论】:

    标签: batch-file copy filelock


    【解决方案1】:

    如果你写

    2>nul ( >>test.xlsx (call )) if %errorlevel% == 1 goto end
    

    你得到一个错误。 if is not expected。两条指令在同一行,没有分隔。

    如果转换成

    2>nul ( >>test.xlsx (call )) & if %errorlevel% == 1 goto end
    

    那么问题是延迟扩展。 %errorlevel% 变量在解析行时被替换为它的值,此时第一部分还没有执行,所以没有设置错误级别。

    如果你改成

    2>nul ( >>test.xlsx (call )) 
    if %errorlevel% == 1 goto end
    

    会有用的

    为了更简洁的构造,你可以试试这个

    (>>test.xlsx call;) 2>nul || goto end
    

    相同的功能,更少的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2013-04-27
      相关资源
      最近更新 更多