Batch file 如何通过findstr命令从文本文件中找到的行中仅获取最后5个数字?
我想从显示的txt文件输出中获取FindStr命令的最后5个数字 这是我的命令:Batch file 如何通过findstr命令从文本文件中找到的行中仅获取最后5个数字?,batch-file,findstr,Batch File,Findstr,我想从显示的txt文件输出中获取FindStr命令的最后5个数字 这是我的命令: FindStr "lastServer" C:\Users\Defcon1\AppData\Roaming\.minecraft\.options.txt 显示输出的示例如下: lastServer:111.111.111.111:53680 如何从输出行仅获取5个数字(IP地址和端口号),而不使用字符串lastServer:?这是一项非常简单的任务,易于编码,例如: @echo off setlocal se
FindStr "lastServer" C:\Users\Defcon1\AppData\Roaming\.minecraft\.options.txt
显示输出的示例如下:
lastServer:111.111.111.111:53680
如何从输出行仅获取5个数字(IP地址和端口号),而不使用字符串
lastServer:
?这是一项非常简单的任务,易于编码,例如:
@echo off
setlocal
set "OptionsFile=%APPDATA%\.minecraft\.options.txt"
rem Does the file exist at all?
if not exist "%OptionsFile%" (
echo Error: File %OptionsFile% not found!
goto EndBatch
)
rem Search for last server line in file and get IP address and port number.
set "IP_and_Port="
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe "lastServer" "%OptionsFile%" 2^>nul') do set "IP_and_Port=%%J"
rem Was IP and port number found in file?
if "%IP_and_Port%" == "" (
echo Error: Found in file %OptionsFile%
echo no line with string "lastServer" with an IP address and a port number!
goto EndBatch
)
rem Output found data.
echo Found: %IP_and_Port%
:EndBatch
endlocal
pause
也可在根本不使用findstr实用程序的情况下工作:
@echo off
setlocal
set "OptionsFile=%APPDATA%\.minecraft\.options.txt"
rem Does the file exist at all?
if not exist "%OptionsFile%" (
echo Error: File %OptionsFile% not found!
goto EndBatch
)
rem Search for last server line in file and get IP address and port number.
for /F "usebackq tokens=1* delims=:" %%I in ("%OptionsFile%") do (
if /I "%%I" == "lastServer" (
set "IP_and_Port=%%J"
goto DataFound
)
)
echo Error: Found in file %OptionsFile%
echo no line with string "lastServer" with an IP address and a port number!
goto EndBatch
rem Output found data.
:DataFound
echo Found: %IP_and_Port%
:EndBatch
endlocal
pause
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面
echo/?
endlocal/?
findstr/?
获取/?
goto/?
如果/?
暂停/?
rem/?
设置/?
setlocal/?