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

问题描述 投票:1回答:1

当前,我有一个程序要我的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,然后使用所做的更改重新启动它。

powershell batch-file edit ini
1个回答
0
投票

由于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之前处理Windows命令行时,重定向操作符|必须使用FOR命令行上的脱字符号^进行转义,以将其解释为文字字符ipconfigfindstr的命令行在后台以%ComSpec% /c开头的命令行中附加了两个'之间的命令行。

© www.soinside.com 2019 - 2024. All rights reserved.