在多个服务器上制作带文件检查的批处理文件

时间:2014-01-21 12:19:02

标签: batch-file

向大家致以问候!

我的主管已经责成我制作批处理文件,用于检查多个服务器上的文件存在。任务是检查网络共享上的文件是否存在(共享的路径是“通用”,唯一的区别是服务器的名称) - \ servername \ share \ tools。

我觉得它看起来像

@echo off
@echo %time%
FOR %%G IN (server1,server2,server3) DO (
    :test
    if exist "\\%%G\share\Tools\test1.txt" goto end
    timeout /T 1
    goto test
    :end
    @echo %time%
)

但是询问有关50台服务器的东西会使我的大脑崩溃 - 它需要(基于%time%输出)约为0,02-0,1秒。这是不真实的,因为这些服务器来自分支机构。

我的错误在哪里?我应该如何更正我的脚本?

另一个问题是 - 如何在列表中标记每个服务器的开始和结束时间?我应该给出什么输出命令?

提前致谢。

2 个答案:

答案 0 :(得分:0)

尝试其中一些:

@echo off

setlocal enableDelayedExpansion
set retries_limit=10
FOR %%G IN (server1,server2,server3) DO (
    echo testing %%G
    @echo %time% 
    for /l %%l in (1,1,%retries_limit%) do (
        set "flag="
        if exist "\\%%G\share\Tools\test1.txt" (
            set "flag=1"
        )
        timeout /T 1
    )
    if defined flag echo %%G is ok else echo something wrong with %%G
    @echo %time% end of %%G testing
)
endlocal

@echo off

setlocal enableDelayedExpansion
set retries_limit=10
FOR %%F "usebackq delims=" IN ("serverlist.txt") DO (
    echo testing %%G
    @echo %time% 
    for /l %%l in (1,1,%retries_limit%) do (
        set "flag="
        if exist "\\%%G\share\Tools\test1.txt" (
            set "flag=1"
        )
        timeout /T 1
    )
    if defined flag echo %%G is ok else echo something wrong with %%G
    @echo %time% end of %%G testing
)
endlocal

@echo off

setlocal enableDelayedExpansion

FOR %%F "usebackq delims=" IN ("serverlist.txt") DO (
    echo testing %%G
    @echo %time% 
    for /l %%l in (1,1,%retries_limit%) do (
        set "flag="
        if exist "\\%%G\share\Tools\test1.txt" (
            set "flag=1"
        )
        timeout /T 1
    )
    if defined flag echo %%G is ok else echo something wrong with %%G
    @echo %time% end of %%G testing
)
endlocal
:checker
    setlocal enableDelayedExpansion
    set retries_limit=10
    @echo %time% 
    for /l %%l in (1,1,%retries_limit%) do (
        set "flag="
        if exist "\\%~1\share\Tools\test1.txt" (
            set "flag=1" & goto :break_loop
        )
        timeout /T 1
    )
    :break_loop
    if defined flag echo %%G is ok else echo something wrong with %%G
    @echo %time% end of %%G testing
    endlocal

答案 1 :(得分:0)

@echo off

    setlocal enableextensions enabledelayedexpansion

    (for %%s in (s1 s2 s3) do (
        pushd \\%%s\share\Tools 2> nul
        if errorlevel 1 (
            echo !time! %%s can not be accessed
        ) else (
            if exist test1.txt (
                echo !time! %%s hold key file
            ) else (
                echo !time! %%s does not hold key file
            )
            popd
        )
    )) > report.txt