Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 用.bat文件更新主机文件_Windows_Batch File - Fatal编程技术网

Windows 用.bat文件更新主机文件

Windows 用.bat文件更新主机文件,windows,batch-file,Windows,Batch File,我需要更新我的主机文件在我们的网站与2条规则 Ip地址应更改为我们保存在主机文件中的当前Ip地址: 111.111.111.111->111.111.111.222 如果没有与111.111.111.222匹配的IP地址,则将其添加到该行的末尾 第一部分很好,但我似乎无法理解第二条规则 目前我所拥有的 我应该怎么做 @echo off setlocal set "oldip=111.111.111.111" set "newip=111.111.111.222" pushd "%syste

我需要更新我的主机文件在我们的网站与2条规则

  • Ip地址应更改为我们保存在主机文件中的当前Ip地址: 111.111.111.111->111.111.111.222
  • 如果没有与111.111.111.222匹配的IP地址,则将其添加到该行的末尾 第一部分很好,但我似乎无法理解第二条规则

    目前我所拥有的

    我应该怎么做

    @echo off
    setlocal
    
    set "oldip=111.111.111.111"
    set "newip=111.111.111.222"
    
    pushd "%systemroot%\System32\drivers\etc" || (
        >&2 echo Failed to change directory.
        exit /b 1
    )
    
    if not exist "hosts" (
        >&2 echo Failed to find file named hosts.
        exit /b 1
    )
    
    (
        set "updated_entry="
    
        for /f "tokens=1*" %%A in (hosts) do @(
            if "%%~A" == "%oldip%" (
                echo %newip% %%B
                set "updated_entry=defined"
            ) else (
                if "%%~A" == "%newip%" set "updated_entry=defined"
                echo %%A %%B
            )
        )
    
        if not defined updated_entry echo %newip% ?domain?
    ) > "hosts.tmp"
    
    move /y "hosts.tmp" "hosts"
    if exist "hosts.tmp" del "hosts.tmp"
    
    更改为
    %systemroot%\System32\drivers\etc
    目录,以便 主机的
    文件是当前目录。检查主机文件是否正确
    它的存在是为了继续

    tokens=1*
    for
    选项将把ip地址存储在
    %%A
    中,并且
    %%B
    中当前行的其余部分。比较新的ip地址 使用
    “%%~A”==%oldip%”读取ip地址

    如果未完成ip地址的更新,则
    updated_条目
    已定义,如果已定义,则将在 文件的结尾。不确定要插入哪个域,所以只需使用
    ?域?

    带有更改的新文件是
    hosts.tmp
    ,它被重命名为
    hosts
    hosts.tmp
    如果出现故障,将被删除,只是为了清理

    更改为
    %systemroot%\System32\drivers\etc
    目录,以便 主机的
    文件是当前目录。检查主机文件是否正确
    它的存在是为了继续

    tokens=1*
    for
    选项将把ip地址存储在
    %%A
    中,并且
    %%B
    中当前行的其余部分。比较新的ip地址 使用
    “%%~A”==%oldip%”读取ip地址

    如果未完成ip地址的更新,则
    updated_条目
    已定义,如果已定义,则将在 文件的结尾。不确定要插入哪个域,所以只需使用
    ?域?

    带有更改的新文件是
    hosts.tmp
    ,它被重命名为
    hosts

    hosts.tmp
    如果出现故障,将被删除,只是为了清理。

    如果有人感兴趣,我找到了答案。如果“%errorlevel%”为“0”,则需要在endlocal FINDSTR/I“111.111.111.222”C:\Windows\System32\drivers\hosts之后添加这些行转到:sub_命令echo 111.111.111.222>>C:\Windows\System32\drivers\hosts:sub_命令move/y C:\Windows\System32\drivers\hosts C:\Windows\System32\drivers\etcIf任何人都会感兴趣我找到了答案。如果“%errorlevel%”=“0”转到:sub_命令echo 111.111.111.222>>C:\Windows\System32\drivers\hosts移动/y C:\Windows\System32\drivers\hosts C:\Windows\System32\drivers\etc感谢您的代码,它看起来更有效。我将在我们的下一次部署中进行尝试。更新后的版本将符合规则2,即在不替换ip地址的情况下,将ip地址添加到文件末尾。很抱歉,我最初忽略了这个条件。谢谢你的代码,它看起来更有效。我将在我们的下一次部署中进行尝试。更新后的版本将符合规则2,即在不替换ip地址的情况下,将ip地址添加到文件末尾。对不起,我最初没有注意到这个情况。
    @echo off
    setlocal
    
    set "oldip=111.111.111.111"
    set "newip=111.111.111.222"
    
    pushd "%systemroot%\System32\drivers\etc" || (
        >&2 echo Failed to change directory.
        exit /b 1
    )
    
    if not exist "hosts" (
        >&2 echo Failed to find file named hosts.
        exit /b 1
    )
    
    (
        set "updated_entry="
    
        for /f "tokens=1*" %%A in (hosts) do @(
            if "%%~A" == "%oldip%" (
                echo %newip% %%B
                set "updated_entry=defined"
            ) else (
                if "%%~A" == "%newip%" set "updated_entry=defined"
                echo %%A %%B
            )
        )
    
        if not defined updated_entry echo %newip% ?domain?
    ) > "hosts.tmp"
    
    move /y "hosts.tmp" "hosts"
    if exist "hosts.tmp" del "hosts.tmp"