如何在批处理文件中结束循环

时间:2016-10-11 13:01:43

标签: batch-file

我有批处理脚本检查:

  1. 使用日期和时间格式创建日志文件
  2. oracle listener check
  3. oracle db status check
  4. 磁盘空间检查
  5. @echo off
    set ORACLE_SID=equis01
    REM set result_file = %Date:~4,2%.%Date:~7,2%.%Date:~10,4%
    
    set HH=%TIME: =0%
    set HH=%HH:~0,2%
    set MI=%TIME:~3,2%
    set SS=%TIME:~6,2%
    
    REM for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set result_file="%%j"."%%i"."%%k"_%HH%.%MI%.%SS%_"DBXXX".log
    
    
    if exist %result_file% (
        del %result_file%
    )
    
    >%result_file% (
    
    echo Oracle Listener Status:
    echo ========================================
    echo.
    lsnrctl status
    
    echo.
    echo.
    echo.
    echo Oracle Database Status:
    echo ========================================
    echo.
    (
    echo @monitoring.sql
    ) | sqlplus / 
    
    )
    
    echo.
    echo.
    echo.
    echo Disk Space:
    echo ========================================
    for /f "skip=1 usebackq delims==" %%i in (`wmic logicaldisk where "mediatype='12'" get caption`) do (
    call :doit %%i >> "%result_file%"
    )
    
    goto :eof
    
    
    :doit
    set driveletter=%1
    if {%driveletter%}=={} goto :EOF
    for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%driveletter%'" get FreeSpace /format:value`) do set FreeSpace=%%x
    for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%driveletter%'" get Size /format:value`) do set Size=%%x
    set FreeMB=%FreeSpace:~0,-10%
    set SizeMB=%Size:~0,-10%
    set /a Percentage=100 * FreeMB / SizeMB
    echo %driveletter% %FreeMB% GB out of  %SizeMB% GB Total - %Percentage%%% free 
    
    
    rem echo exit
    for %%a in (*.log*) do "C:\Program Files\7-Zip\7z.exe" a -tzip "%result_file%" "%%a"
    

    问题描述:
    运行代码后 - 它必须存档日志文件。 但消息说:

    C: 25 GB out of  42 GB Total - 59% free 
    System ERROR:
    The process cannot access the file because it is being used by another process.
    ERROR: 11.10.2016_17.47.31_DBXXX.log
    Can not open the file as archive
    D: 88 GB out of  107 GB Total - 82% free 
    System ERROR:
    The process cannot access the file because it is being used by another process.
    ERROR: 11.10.2016_17.47.31_DBXXX.log
    Can not open the file as archive
    E: 415 GB out of  436 GB Total - 95% free 
    System ERROR:
    The process cannot access the file because it is being used by another process.
    ERROR: 11.10.2016_17.47.31_DBXXX.log
    Can not open the file as archive
    

    如何解决问题?

1 个答案:

答案 0 :(得分:0)

对于WMIC部分,您可以像这样简化代码:

@echo off
setlocal enabledelayedexpansion

for /f %%i in ('wmic logicaldisk where "mediatype=12" get caption ^| findstr ":"') do (
   for /f %%x in ('wmic logicaldisk where "DeviceID='%%i'" get Size^,FreeSpace ^/format:value ^| findstr [0-9]') do set %%x
   call:next %%i !Freespace:~0,-10! !Size:~0,-10!
)

exit/b

:next
   set /a percentage=(%2 * 100) / %3
   echo %1 - %2 Gb of %3 Gb  [!Percentage!^% Free]

然后只需添加其他元素,调试就会更容易。