如何保存变量值以文件并从文件中加载它们



到目前为止,我有一个批处理脚本,将IP地址更改为静态地址。目前,变量是在批处理文件中定义的。

我正在寻找的是添加一个IF语句,该语句检查是否不存在变量的值,在这种情况下,批处理文件询问变量的值,然后将变量的值保存为文件稍后再使用设置IP地址。

到目前为止,我有这个:

@ECHO OFF
SET IPAddress=10.10.3.5
SET Subnet=255.255.0.0
SET Gateway=10.10.1.1
SET Dns=10.10.11.87
@ECHO IP Address will now change to the static address of "%IPAddress%"
netsh int ipv4 set address name="Local Area Connection" source=static address="%IPAddress%" mask="%Subnet%"  gateway="%Gateway%"
@ECHO IP Address Changed...
timeout /T 2 /NOBREAK > nul
@ECHO Subnet Changed...
timeout /T 2 /NOBREAK > nul
@ECHO Gateway Changed...
timeout /T 2 /NOBREAK > nul
@ECHO DNS Changed...
netsh int ip set dnsservers name="Local Area Connection" source=static address="%Dns%" validate=no
@Echo IP address is now changed.  You can now close this window.
pause > nul

我不明白为什么您已经重新发明了已经存在了数十次。使用万维网搜索引擎搜索switch network settings,您会发现许多用于在网络接口的网络设置之间切换的工具。

也可以在网络接口上设置多个IP地址,子网掩码,网关和DNS服务器,从而在使用始终使用相同的网络(例如Office Network和Home Network(等网络时,不必始终切换网络设置。/p>

但是,这是一个简单的批量代码,用于加载和保存环境变量的值。

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SaveData="
set "IpMod_IPAddress="
set "IpMod_Subnet="
set "IpMod_Gateway="
set "IpMod_Dns="
if exist "IpMod_Data.txt" for /F "usebackq tokens=1,2* delims=_=" %%A in ("IpMod_Data.txt") do if "%%A" == "IpMod" set "%%A_%%B=%%C"
if not "!IpMod_IPAddress!" == "" goto ValidateIpAddress
:InputIpAddress
echo/
set "SaveData=1"
set "IpMod_IPAddress=10.10.3.5"
set /P "IpMod_IPAddress=Enter IP address (default: %IpMod_IPAddress%): "
:ValidateIpAddress
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputIpAddress on invalid input.
if not "!IpMod_Subnet!" == "" goto ValidateSubnet
:InputSubnet
echo/
set "SaveData=1"
set "IpMod_Subnet=255.255.0.0"
set /P "IpMod_Subnet=Enter subnet mask (default: %IpMod_Subnet%): "
:ValidateSubnet
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputSubnet on invalid input.
if not "!IpMod_Gateway!" == "" goto ValidateGateway
:InputGateway
echo/
set "SaveData=1"
set "IpMod_Gateway=10.10.1.1"
set /P "IpMod_Gateway=Enter gateway IP (default: %IpMod_Gateway%): "
:ValidateGateway
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputGateway on invalid input.
if not "!IpMod_Dns!" == "" goto ValidateDns
:InputDns
echo/
set "SaveData=1"
set "IpMod_Dns=10.10.1.1"
set /P "IpMod_Dns=Enter DNS IP (default: %IpMod_Dns%): "
:ValidateDns
rem Add here extra code to validate the user input using delayed
rem expansion with execution of goto InputDns on invalid input.
if "%SaveData%" == "1" set IpMod_ >"IpMod_Data.txt"
echo/
echo The network data are:
echo/
echo IP address:  !IpMod_IPAddress!
echo Subnet mask: !IpMod_Subnet!
echo Gateway IP:  !IpMod_Gateway!
echo DNS IP:      !IpMod_Dns!
echo IP Address will now change to the static address of "%IpMod_IPAddress%"
%SystemRoot%System32netsh.exe int ipv4 set address name="Local Area Connection" source=static address="%IpMod_IPAddress%" mask="%IpMod_Subnet%" gateway="%IpMod_Gateway%"
echo IP Address Changed...
%SystemRoot%System32timeout.exe /T 2 /NOBREAK >nul
echo Subnet Changed...
%SystemRoot%System32timeout.exe /T 2 /NOBREAK >nul
echo Gateway Changed...
%SystemRoot%System32timeout.exe /T 2 /NOBREAK >nul
echo Dns Changed...
%SystemRoot%System32netsh.exe int ip set dnsservers name="Local Area Connection" source=static address="%IpMod_Dns%" validate=no
echo IP address is now changed.  You can now close this window.
endlocal
pause >nul

我从C 代码中知道的是,我为公司编写的内容检查用户输入的网络设置以尽可能多地检测无效输入,我假设您使用此代码完成了整个任务的0.5%。祝您编码剩余的99.5%。

用于了解使用的命令及其工作方式,打开命令提示符窗口,执行以下命令,并完全仔细阅读所有命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • setlocal /?
  • timeout /?
  • netsh /?
  • netsh int /?
  • netsh int ip /?
  • netsh int ipv4 /?
  • 等等netsh

还请阅读有关使用命令重定向操作员的Microsoft文章。
还要查看从批处理脚本分配IP的答案。

最新更新