首页 > 解决方案 > 无法将内容流式传输到文件

问题描述

我制作了以下代码:

  1. 检查 YouTube 这个词是否在计算机的 HOSTS 文件中
  2. 如果不是 - 将 YouTube 这个词插入到文件中
  3. 如果有 - 从文件中删除 YouTube 一词。并运行一个计时器,用于返回 YouTube 这个词在每个进程之后 - 停止 Chrome.exe 任务由于某种原因它不起作用。
@echo off
setlocal EnableDelayedExpansion
(reg query "HKU\S-1-5-19" || (powershell start-process -FilePath '%0' -verb runas&exit /B)) >NUL 2>&1
:start
FIND /i "www.youtube.com" "%WINDIR%\System32\Drivers\Etc\Hosts" >nul 2>&1 
if errorlevel 1 (
    echo.
    echo 127.0.0.1 www.youtube.com >> %WINDIR%\System32\Drivers\Etc\Hosts
    Taskkill /F /IM chrome.exe
)  ELSE (
    set /p "OPEN_TIME=Enter time: "
    echo.
    echo # Copyright (c) 1993-2009 Microsoft Corp.> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # This file contains the mappings of IP addresses to host names. Each>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # entry should be kept on an individual line. The IP address should>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # be placed in the first column followed by the corresponding host name.>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # The IP address and the host name should be separated by at least one>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # space.>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # Additionally, comments (such as these) may be inserted on individual>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # lines or following the machine name denoted by a '#' symbol.>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # For example:>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #      102.54.94.97     rhino.acme.com          # source server>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #       38.25.63.10     x.acme.com              # x client host>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo: >> %WINDIR%\System32\Drivers\Etc\Hosts
    echo # localhost name resolution is handled within DNS itself.>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #  127.0.0.1       localhost>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo #  ::1             localhost>> %WINDIR%\System32\Drivers\Etc\Hosts
    echo 127.0.0.1 view-localhost # view localhost server>> %WINDIR%\System32\Drivers\Etc\Hosts
    Taskkill /F /IM chrome.exe
    timeout /T !OPEN_TIME!
    GOTO start
)

如果我删除整个 ELSE 命令并仅保留此代码,它确实有效:

@echo off
setlocal EnableDelayedExpansion
(reg query "HKU\S-1-5-19" || (powershell start-process -FilePath '%0' -verb runas&exit /B)) >NUL 2>&1
:start
FIND /i "www.youtube.com" "%WINDIR%\System32\Drivers\Etc\Hosts" >nul 2>&1 
if errorlevel 1 (
   echo.
   echo 127.0.0.1 www.youtube.com >> %WINDIR%\System32\Drivers\Etc\Hosts
   Taskkill /F /IM chrome.exe
)

有谁知道为什么?

标签: batch-filecmd

解决方案


使行可写的 EZ 方法是在 IF 的外括号之外。

这很容易通过将 IF 的所有内部改组到一个被调用的函数中来完成,这也使测试变得相当简单,并且代码可能更容易理解。

我想指出,因为您正在用自己的文件内容替换文件内容,所以您永远无法确定您没有覆盖您自己或其他程序添加的任何其他条目(例如,众所周知,反恶意软件会放入许多!)

您可以通过编写包含所有现有行的主机副本来对此进行调整,除了您希望删除的行,并添加了附加行,然后在此过程中移动它以替换正常的主机文件。

但是,现在,让我们只使用您的代码并使其更易于处理。

另外,我认为无论是否找到该项目,您都希望每 30 分钟循环一次,不是吗?您似乎在说代码应该始终循环。出于这个原因,我移动了它。

确保您在命令行中运行此表单来测试它,而不是双击它。

此外,请确保命令提示符以管理员身份运行,因为如果不是,您可能无法更改 hosts 文件。

@(setlocal EnableDelayedExpansion
  echo off
  (reg query "HKU\S-1-5-19" || (powershell start-process -FilePath '%0' -verb runas&exit /B)) >NUL 2>&1
  SET "Write-Hosts=CALL :Write_Hosts "
  SET "_HostsFilePath=%WINDIR%\System32\Drivers\Etc\Hosts"
)

CALL :Main

( ENDLOCAL
  Exit /b
)

:Main
  FIND /i "www.youtube.com" "%_HostsFilePath%" >nul 2>&1 
  if errorlevel 1 (
    CALL :Missing
  )  ELSE (
    CALL :Found
  )
  Taskkill /F /IM chrome.exe
  FOR /F "Tokens=2-3 delims=:." %%A IN ("%TIME%") DO (SET /A "Timer=(30-((1%%A-(2%%A-1%%A))%%30))*60 - (1%%B-(2%%B-1%%B))")
  Echo.Waiting %OPEN_TIME% Seconds before Running again.
  timeout /T %OPEN_TIME%
 GOTO :Main
GOTO :EOF

:Missing
  echo. Adding 127.0.0.1 www.youtube.com
  %Write-Hosts%127.0.0.1 www.youtube.com
GOTO :EOF

:Found
  REM set /p "OPEN_TIME=Enter time: "
  REM echo.
  echo. Resetting Hosts
  SET "Output=>"
  %Write-Hosts%# Copyright (c) 1993-2009 Microsoft Corp.
  %Write-Hosts%#
  %Write-Hosts%# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
  %Write-Hosts%#
  %Write-Hosts%# This file contains the mappings of IP addresses to host names. Each
  %Write-Hosts%# entry should be kept on an individual line. The IP address should
  %Write-Hosts%# be placed in the first column followed by the corresponding host name.
  %Write-Hosts%# The IP address and the host name should be separated by at least one
  %Write-Hosts%# space.
  %Write-Hosts%#
  %Write-Hosts%# Additionally, comments (such as these) may be inserted on individual
  %Write-Hosts%# lines or following the machine name denoted by a '#' symbol.
  %Write-Hosts%#
  %Write-Hosts%# For example:
  %Write-Hosts%#
  %Write-Hosts%#     102.54.94.97    rhino.acme.com        # source server
  %Write-Hosts%#      38.25.63.10    x.acme.com            # x client host
  %Write-Hosts%# localhost name resolution is handled within DNS itself.
  %Write-Hosts%#  127.0.0.1      localhost
  %Write-Hosts%#  ::1           localhost
  %Write-Hosts% 127.0.0.1 view-localhost # view localhost server
GOTO :EOF

:Write_Hosts
  ECHO.%*
  ECHO.%*%Output% "%_HostsFilePath%"
  SET "Output=>>"
GOTO :EOF

推荐阅读