Batch Batch Replace hosts

hosts file to replace

Work needs to be modified many computer hosts file, using the bat batch completed
to solve the problem:
1.PC work too much trouble in a non-administrator privileges, right-administrator privileges, so a way to perform the application administrator privileges
2.hosts and replace script on a machine table pc, pc to other ways to access the shared folder, but cmd command line can not be a UNC path as the current directory, so as not to facilitate the identification and hosts file with the script directory.
3. before performing a backup replacement ., first and whether the new hosts file Batch file in the same directory, prevent erroneous operation
4. Does regardless of the file to complete the update, you need to give prompt and automatically shut down the terminal.
for question 1, reference https: //blog.csdn. net / lijialong1313 / article / details /
54171535 to question 2, the reference https://www.jianshu.com/p/2d3190f592c5
for problem 3, reference https://blog.csdn.net/qq_39720249/article/details/85067931
for question 4, by the following method
alternative successful green text on black background, the window automatically close after 7 seconds
red text on black background is not successful, does not automatically close the window

There are several methods bat file on the application administrator privileges Baidu, and I can choose a

Attach the code I use

@echo off
title hosts文件替换
REM ________________________________________________________________
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo 请求管理员权限...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B

:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd %~dp0
echo %cd%

REM ________________________________________________________________
echo.
echo.
SET SourceFile=%cd%\hosts
SET GenFile=C:\Windows\System32\drivers\etc\hosts
if exist %SourceFile% (
    echo.
    echo.
    color 0A
    echo hosts文件存在-备份-更新
    move %GenFile% %GenFile%.old
    copy %SourceFile% %GenFile%
    popd
    echo 更新完成
    >nul TIMEOUT /T 7
) else (
    color 0C
    echo.
    echo.
    echo hosts文件不存在
    pause>nul
)
echo.
echo.

UNC path problem, the key parts of the code as follows
pushd% ~ dp0 mapped network drive
popd release mapping

cd /d %~dp0
echo %cd%

pushd %~dp0
echo %cd%
popd
pause

CSDN blogger "so three of Shame" I refer to the documentation part, will be used after a certain sense, therefore reproduced over

CSDN blogger "so three of Shame" original articles
original link: https://blog.csdn.net/qq_39720249/article/details/85067931

Create a file or directory by copying method

@echo off
echo result.jtl文件判断
SET SourceFile=G:\Jmeter\apache-jmeter-5.0\report\backup\result.jtl
SET GenFile=G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl

if not exist %GenFile% (
        rem 复制文件result.jtl
        copy %SourceFile% %GenFile%
        echo %GenFile% 文件不存在,已创建该文件!
    ) else (
        echo %GenFile% 文件已存在,无需创建!
    )

Copy the catalog (including an empty directory):

@echo off
echo OutReport目录判断
SET SourceFolder=G:\Jmeter\apache-jmeter-5.0\report\backup\OutReport
SET GenFolder=G:\Jmeter\apache-jmeter-5.0\report\OutReport\

if not exist %GenFolder% (
        rem 复制目录G:\Jmeter\apache-jmeter-5.0\report\backup\OutReport到G:\Jmeter\apache-jmeter-5.0\report路径下
        Xcopy %SourceFolder% %GenFolder%  /s/e/h/i
        echo %GenFolder%目录不存在,已创建该目录!
    ) else (
        rem 目录G:\Jmeter\apache-jmeter-5.0\report\OutReport已存在,无需创建
        echo %GenFolder%目录已存在,无需创建!
    )

Copy files and directories (including empty)

@echo off
echo result.jtl文件判断
SET SourceFile=G:\Jmeter\apache-jmeter-5.0\report\backup\result.jtl
SET GenFile=G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl

if not exist %GenFile% (
        rem 复制文件result.jtl
        copy %SourceFile% %GenFile%
        echo %GenFile% 文件不存在,已创建该文件!
    ) else (
        echo %GenFile% 文件已存在,无需创建!
    )

echo OutReport目录判断
SET SourceFolder=G:\Jmeter\apache-jmeter-5.0\report\backup\OutReport
SET GenFolder=G:\Jmeter\apache-jmeter-5.0\report\OutReport\

if not exist %GenFolder% (
        rem 复制目录G:\Jmeter\apache-jmeter-5.0\report\backup\OutReport到G:\Jmeter\apache-jmeter-5.0\report路径下
        Xcopy %SourceFolder% %GenFolder%  /s/e/h/i
        echo %GenFolder%目录不存在,已创建该目录!
    ) else (
        rem 目录G:\Jmeter\apache-jmeter-5.0\report\OutReport已存在,无需创建
        echo %GenFolder%目录已存在,无需创建!
    )

Create files and directories by new method

create a new file

@echo off
echo result.jtl文件判断
SET GenFile=G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl

if not exist %GenFile% (
        rem 创建空文件G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl
        rem 如果需要添加内容,少量的,可以用内容把“nul”替换掉,比如:type 这是文件内容>%GenFile%;多的话则使用复制方法
        type nul>%GenFile%
        echo %GenFile%文件不存在,已创建该文件!
    ) else (
        rem 文件G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl已存在,无需创建
        echo %GenFile%文件已存在,无需创建!
    )

Create an empty directory

@echo off
echo OutReport目录判断
SET GenFolder=G:\Jmeter\apache-jmeter-5.0\report\OutReport

if not exist %GenFolder% (
        rem 创建空目录G:\Jmeter\apache-jmeter-5.0\report\OutReport
        rem 创建非空目录,使用复制方法
        echo %GenFolder%目录不存在,已创建该目录!
        md %GenFolder%
    ) else (
        rem 目录G:\Jmeter\apache-jmeter-5.0\report\OutReport已存在,无需创建
        echo 目录%GenFolder%目录已存在,无需创建!
    )

Whether there .jtl file extension judge directory

@echo off
echo .jtl后缀名文件判断
SET GenFile="G:\Jmeter\apache-jmeter-5.0\report\jtl"\***.jtl

if not exist %GenFile% (
        rem 创建空文件G:\Jmeter\apache-jmeter-5.0\report\jtl\newfile.jtl
        rem 如果需要添加内容,少量的,可以用内容把“nul”替换掉,比如:type 这是文件内容>文件路径\新文件名;多的话则使用复制方法
        type nul>G:\Jmeter\apache-jmeter-5.0\report\jtl\newfile.jtl
        echo %GenFile%文件不存在,已创建该文件!
    ) else (
        rem 文件"G:\Jmeter\apache-jmeter-5.0\report\jtl"\***.jtl已存在,无需创建
        echo %GenFile%文件已存在,无需创建!
    )

New files and directories

@echo off
echo result.jtl文件判断
SET GenFile=G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl

if not exist %GenFile% (
        rem 创建空文件G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl
        rem 如果需要添加内容,少量的,可以用内容把“nul”替换掉,比如:type 这是文件内容>%GenFile%;多的话则使用复制方法
        type nul>%GenFile%
        echo %GenFile%文件不存在,已创建该文件!
    ) else (
        rem 文件G:\Jmeter\apache-jmeter-5.0\report\jtl-report-output\result.jtl已存在,无需创建
        echo %GenFile%文件已存在,无需创建!
    )


echo OutReport目录判断
SET GenFolder=G:\Jmeter\apache-jmeter-5.0\report\OutReport

if not exist %GenFolder% (
        rem 创建空目录G:\Jmeter\apache-jmeter-5.0\report\OutReport
        rem 创建非空目录,使用复制方法
        echo %GenFolder%目录不存在,已创建该目录!
        md %GenFolder%
    ) else (
        rem 目录G:\Jmeter\apache-jmeter-5.0\report\OutReport已存在,无需创建
        echo 目录%GenFolder%目录已存在,无需创建!
    )


echo 所有.jtl后缀名文件判断
SET GenFile="G:\Jmeter\apache-jmeter-5.0\report\jtl"\***.jtl

if not exist %GenFile% (
        rem 创建空文件G:\Jmeter\apache-jmeter-5.0\report\jtl\newfile.jtl
        rem 如果需要添加内容,少量的,可以用内容把“nul”替换掉,比如:type 这是文件内容>文件路径\新文件名;多的话则使用复制方法
        type nul>G:\Jmeter\apache-jmeter-5.0\report\jtl\newfile.jtl
        echo %GenFile%文件不存在,已创建该文件!
    ) else (
        rem 文件"G:\Jmeter\apache-jmeter-5.0\report\jtl"\***.jtl已存在,无需创建
        echo %GenFile%文件已存在,无需创建!
    )

Guess you like

Origin www.cnblogs.com/guyk/p/11588327.html