windows命令批处理
我想知道/f命令的行为。 我正在对一个文件执行windows命令批处理,windows,batch-file,cmd,Windows,Batch File,Cmd,我想知道/f命令的行为。 我正在对一个文件执行ipconfig/all;将具有IPv4地址的行拉到另一个文件。然后我想读取此文件并隔离IPv4地址xxx.xxx.xxx.xxx 然后我想重读这个文件,并根据前两个八位字节选择一个IP地址 然后我想使用所选的ip来构建一条附加了ip的线路。 在我看来,for/f命令的行为方式我不理解 : ADDResses.cmd 5/8 :: :: turn on echo :: clear screen :: ipconfig into a find look
ipconfig/all
;将具有IPv4地址的行拉到另一个文件。然后我想读取此文件并隔离IPv4地址xxx.xxx.xxx.xxx
然后我想重读这个文件,并根据前两个八位字节选择一个IP地址
然后我想使用所选的ip来构建一条附加了ip的线路。
在我看来,for/f
命令的行为方式我不理解
: ADDResses.cmd 5/8
::
:: turn on echo
:: clear screen
:: ipconfig into a find looking of the string IP address. and send to a file
:: cat out the file to check
::
::
:: output of the file is listed below
::
::C:\Scripts>type c:\scripts\address.txt
::
:: IPv4 Address. . . . . . . . . . . : XX.XX.XX.XX
:: IPv4 Address. . . . . . . . . . . : XX.XX.XX.XX
::C:\Users\william.reagan\Scripts>
::
::echo Usage: print_head COUNT FILENAME
echo on
del ipAddrLine.txt
cls
:: following line takes output of ipconfig and sends lines with ip address to the address.txt file
echo "::select IP address ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
ipconfig | findstr /r "IPv4 Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" > %~dp0address.txt
call :print_head 10 %~dp0address.txt
goto :after
::::::::::::::::
::
:: print_head
:: skip first blank line
:: send the line to ipAddrLine.txt
:: Prints the first non-blank (10) %1 lines in the file %2.
::
::
:print_head
echo "::starting print head ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
setlocal EnableDelayedExpansion
set /a counter=0
for /f ^"usebackq^ eol^=^
^ delims^=^" %%a in (%2) do (
if "!counter!"=="%%a" goto :after
echo %%a >> %~dp0ipAddrLine.txt
set /a counter+=1
)
:after
::
::
echo "::checking just the ip address:::type ipaddrline file to make sure it closes:::::::::::::::::::::::::::::::::::::::::::::"
::fouteenth token is ip address which occurs after the 13th space
::
type %~dp0ipAddrLine.txt
FOR /F "tokens=14* delims= " %%f in (%~dp0ipAddrLine.txt) DO (
set @IPaddr=%%f
echo %@IPaddr%
::
echo.%@IPaddr% | findstr /C:"10.32" 1>nul
if errorlevel 1 (
echo. pattern not found
) ELSE (
echo %@IPaddr%
goto createAgentLines
)
)
goto :doNotinstall
:createAgentLines
::create lines for the agent.properties file
set multiNICline1=tw.rpc.interfaceAddr=
set multiNICline2=java.rmi.server.hostname=
set completeLine1=%multiNICline1%%@IPaddr
在CMD窗口中键入
HELP FOR
。这里有一个FOR循环,它将从ipconfig/all自动为您提供IP地址:
for /f "tokens=13 delims=(: " %%p in ('ipconfig /all ^| findstr "IPv4"') do echo %%p
下面是一个for循环,它将以块的形式为您提供IP地址,因此您可以获得前两个八位字节的IP地址,以及使用这两个八位字节的IP地址:
for /f "tokens=3-6 delims=(:. " %%P in ('ipconfig /all ^| findstr "IPv4"') do (
echo IP ADDRESS: %%P.%%Q.%%R.%%S
echo Two Octets: %%P.%%Q
echo New IPADDR: %%P.%%Q.0.1
)
有关For循环的详细信息,请在命令窗口中键入:
for /?
编辑:
for/f
-解析循环
“tokens=3-6”
-表示仅使用模式的第三、第四、第五和第六次出现
“delims=(:”
-表示我们使用(
:
和
进行拆分(这是模式)
%%p
-是我们正在使用的起始变量。它们区分大小写并按字母顺序排列。对于这样的单个循环,使用哪个范围并不重要,也就是说,我们可以使用“%%a”
在
中-表示:从以下命令行的输出
('ipconfig/all^ | findstr“IPv4”)
-这是我们正在运行的命令行;因为我们只对IPv4值感兴趣,所以我们使用findstr
do(
-对于输出(每行),执行以下操作
回显IP地址:%%p.%Q.%R.%S
-打印到屏幕上的第三、第四、第五和第六个事件
回显两个八位字节:%%p.%Q
-打印到屏幕上的第三个和第四个八位字节
echo New IPADDR:%%p.%Q.0.1
-打印到屏幕上的第三个和第四个事件以及其他值
)
-告诉执行(
结束,停止执行
查看非常好的文档代码可以工作;当你一天过得非常慢时,解释一下会非常有帮助,同时我会尝试解释它是如何工作的,再次感谢………这里有一个链接,解释了for循环的工作方式和周围的注意事项(相当好):