从ipconfig中获取未知IP,并使用BATCH脚本用该IP编辑未知.ini行。

当前,我有一个设置,程序要我的LAN IP将其数据附加到其中,并且当我重新启动系统时,此LAN IP总是随机的。我使用VPN时,我的LAN IP总是不同的,因此每次我更改网络或重新启动时都会感到困惑,因为我确实需要运行程序,进行更改,重新启动。

我确实有一个脚本,该脚本将使用正确的IP生成变量,因为它始终是第一个IPv4地址。我确实在此网站上找到它。

set ip_address_string="IPv4 Address"
rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem")
rem set ip_address_string="IP Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
    echo Your IP Address is: %%f
    goto :eof
)

这是金色的,但是我发现的每个.ini编辑帖子实际上对我都不起作用,因为它会精细打印行而不是编辑行。

由于.ini的大小未知,我确实需要一个powershell脚本才能使它运行,因为BATCH有局限性。

Stackoverflow上的一个用户具有以下代码:

(Get-Content C:\programdata\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace 'Default NAS Address=.*','Default NAS Address=BHPAPPDGN01V' } | Set-Content C:\programdata\Nuance\NaturallySpeaking12\nssystem.ini

需要更改的行大部分是随机的。嗯,这不是随机的,但是随着用户可以进行一些更改,它有时会移动,并且会根据设置向下或向上推IP线路。

在BATCH和powershell方面,我还是一个新手,但是我还没有找到将信息传输到powershell的方法。例如,BATCH将获取IP,创建变量,运行Powershell脚本以编辑.ini。我确实记得有一个代码可以将IP捕获到剪贴板,但此刻我找不到它。

我当前的进度是

@echo off
setlocal EnableExtensions
echo Exiting PROGRAM...
taskkill /IM PROGRAM.exe
timeout /t 1 /nobreak>nul
:check
for /F %%a in ('tasklist /NH /FI "IMAGENAME eq PROGRAM.exe"') do if %%a == PROGRAM.exe goto waiting
echo Restarting PROGRAM...
start "" "%ProgramFiles%\PROGRAM\PROGRAM.exe"
timeout /t 1 /nobreak>nul
exit
:waiting
echo PROGRAM is still running. retrying...
timeout /t 3 /nobreak>nul
goto check

我不需要setlocal EnableExtensions,但是我确实尝试过使其工作,然后需要它。 当前,脚本会查看程序是否正在运行,如果正在运行,请轻柔地杀死它(等待它自然退出),然后重新启动它。我的目标是轻柔地杀死它,编辑.ini,然后使用所做的更改重新启动它。

xmcl0712 回答:从ipconfig中获取未知IP,并使用BATCH脚本用该IP编辑未知.ini行。

由于Windows命令处理器cmd.exe不是为编辑文件而设计的,因此纯PowerShell解决方案很可能是此任务的最佳选择。

但这是一个纯批处理文件解决方案,用于确定当前的IPv4地址,如果当前包含另一个地址,则将其写入INI文件中。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "IniFile=C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini"
if exist "%IniFile%" goto GetIpAddress

echo ERROR: File missing: "%IniFile%"
echo/
pause
goto EndBatch

:GetIpAddress
set "ip_address_string=IPv4 Address"
rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem")
rem set "ip_address_string=IP Address"
rem The string above must be IP-Adresse on a German Windows.
for /F "tokens=2 delims=:" %%I in ('%SystemRoot%\System32\ipconfig.exe ^| %SystemRoot%\System32\findstr.exe /C:"%ip_address_string%"') do set "IpAddress=%%I" & goto IpAddressFound

echo ERROR: Could not find %ip_address_string% in output of ipconfig.
echo/
pause
goto EndBatch

:IpAddressFound
set "IniEntry=Default NAS Address"
rem Remove leading (and also trailing) spaces/tabs.
for /F %%I in ("%IpAddress%") do set "IpAddress=%%I"
rem Do not modify the INI file if it contains already the current IP address.
%SystemRoot%\System32\findstr.exe /L /X /C:"%IniEntry%=%IpAddress%" "%IniFile%" >nul
if errorlevel 1 goto UpdateIniFile

echo The file "%IniFile%"
echo contains already the line: %IniEntry%=%IpAddress%
echo/
goto EndBatch

:UpdateIniFile
for %%I in ("%IniFile%") do set "TempFile=%%~dpnI.tmp"
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%IniFile%" 2^>nul') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    set "Line=!Line:*:=!"
    if not defined Line echo(
    for /F delims^=^=^ eol^= %%J in ("!Line!") do (
        set "FirstToken=%%J"
        if "!FirstToken!" == "%IniEntry%" (
            echo %IniEntry%=%IpAddress%
        ) else echo(!Line!
    )
    endlocal
))>"%TempFile%"

rem Replace the INI file by the temporary file,except the temporary file is empty.
if exist "%TempFile%" (
    for %%I in ("%TempFile%") do if not %%~zI == 0 (
        move /Y "%TempFile%" "%IniFile%" >nul
        echo Updated the file "%IniFile%"
        echo with the %ip_address_string% "%IpAddress%" for INI entry "%IniEntry%".
        echo/
    )
    del "%TempFile%" 2>nul
)

:EndBatch
endlocal

请阅读我对How to read and print contents of text file line by line?的回答
它说明了仅使用Windows Commands来更新INI文件的糟糕的慢速代码。

要了解所使用的命令及其工作方式,请打开command prompt窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • ipconfig /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

有关运算符&的说明,另请参见Single line with multiple commands using Windows batch file

阅读有关Using command redirection operators的Microsoft文章,以获取>2>nul|的解释。当Windows命令解释器在执行命令之前处理此命令行时,重定向操作符|必须在 FOR 命令行上使用脱字符号^进行转义,才能被解释为文字字符。 FOR ,该命令在后台以ipconfig开始并在附加两个findstr之间添加命令行的单独命令过程中,使用%ComSpec% /c'执行嵌入式命令行。

,

Mofi有一个很棒的批处理解决方案,但是他提到CMD并不是真正为这样的文件输出而设计的。因此,这里有一个纯Powershell解决方案,可以满足您的需求。它确实从您在问题中包含的Powershell代码片段中借来了一点。

double update_weights_v2(LOGISTIC_MODEL hModel,FILE* data_input,FILE* data_output) {
    Model* pModel = (Model*)hModel;
    int status1,status2;
    double X,Y,error = 0,prediction,W1_deriv = 0,W2_deriv = 0;
    int n = 0;

    //X = parse_data_double(data_input,&status1);
    //Y = parse_data_double(data_output,&status2);

    status1 = fscanf_s(data_input,"%[^\n]lf",&X);
    status2 = fscanf_s(data_output,&Y);

    while (status1 != EOF && status2 != EOF) {
        prediction = pModel->activation(X,pModel->W1,pModel->W2);
        error = Y - prediction;

        // df/dW1 = 1/n SUM[1->n](-2*Xi * (error))
        W1_deriv += (X * error * pModel->rate);
        // df/dW2 = 1/n SUM[1->n](-2 * (error))
        W2_deriv += (error*pModel->rate);

        // Read next data entry
        //X = parse_data_double(data_input,&status1);
        //Y = parse_data_double(data_output,&status2);
        status1 = fscanf_s(data_input,&X);
        status2 = fscanf_s(data_output,&Y);
        n++;
        printf("Input: %.5lf\nOutput: %.5lf\nGuess: %.5lf\nError: %.5lf\nWeight[1]: %.5lf\nWeight[2]: %.5lf\n*************************************************************\n",X,error,pModel->W2);
    }
    rewind(data_input);
    rewind(data_output);
    return error;
}

显然,这取决于您知道要获取的接口别名,但是您可以使用Get-NetIPAddress轻松找到它,并且它不应更改。您还需要更新路径信息。

本文链接:https://www.f2er.com/3165030.html

大家都在问