【问题标题】:Batch file Find wildcard's value批处理文件查找通配符的值
【发布时间】:2014-07-08 06:48:02
【问题描述】:

我有一个非常糟糕的脚本(我知道有很多更简单的方法可以实现这一点,但是哦。)计算你的 IP 地址。它使用了一种非常复杂且缓慢的迂回方法,但我被卡住了。我基本上有这个:我桌面上的一个巨大的 txt 文件,其中嵌入了一个 IP 地址。

代码如下:

@if (@CodeSection == @Batch) @then

@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

cd\
cd users\%username%\desktop\
if exist "what is my ip address - Google Search.htm" del "what is my ip address -     Google Search.htm"
if exist ip.txt del ip.txt

:: this link is just a shortened url to a google search of "what is my ip address"
start http://goo.gl/L66owU
timeout 10 >nul /nobreak

%sendkeys% "(^s)"
timeout 1 >nul /nobreak
%sendkeys% "{enter}"
timeout 5 >nul /nobreak

rename "what is my ip address - Google Search.htm" "ip.txt"
findstr /c "Your public IP address is <em>.............</em>"  ip.txt
pause
goto :EOF


@end


// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

我看着它就畏缩了。

无论如何,我的错误在这一行 findstr /c "Your public IP address is &lt;em&gt;.............&lt;/em&gt;" ip.txt 我需要过滤掉所有内容,并获取设置为通配符的任何值(.......s)

如果你尝试这个脚本,你基本上会让你的默认浏览器打开一个谷歌页面,然后它会将该页面保存为一个 HTM 文件(如果你至少使用 chrome),然后我重命名@987654324 @到.txt。我只需要一个更好的(和有效的)方法来最终显示 IP 地址。

我知道这是可能的,我只是不太擅长 findfindstr 命令。

【问题讨论】:

    标签: batch-file ip-address findstr


    【解决方案1】:

    试试这个

    for /f "tokens=6 delims=(" %%a in ('findstr /c:"Your public IP address is <em>"  ip.txt') do set ip="%%a"
    set ip="%ip:*Your public IP address is <em>=%
    set ip=%ip:~0,12%"
    

    问题在于 Findstr 返回包​​含指定字符串的 完整行,因此您需要一个 for 循环来完成此操作。
    但是,for 循环的 limitation 有 31 个标记,因此我选择了“(”作为分隔符。

    记住:只要最近的保存位置在桌面上,此脚本就可以工作。

    【讨论】:

      【解决方案2】:

      使用 VBS 或其他脚本语言可以很容易地做到这一点。 您也可以使用 cURL。像这样:

      @echo off 
      for /f  "delims=" %%a in ('curl ifconfig.me/ip') do echo Your External IP adress is : %%a
      pause
      

      如果你想使用你的解决方案,最好使用这个 URL 来获得一个更小的 html 文件:

      http://checkip.dyndns.org/
      

      所以这里你的代码用这个 URL 修改了:

      @if (@CodeSection == @Batch) @then
      
      @echo off
      
      rem Use %SendKeys% to send keys to the keyboard buffer
      set SendKeys=CScript //nologo //E:JScript "%~F0"
      
      cd\
      cd users\%username%\desktop\
      
      :: this link is just a shortened url to a google search of "what is my ip address"
      start http://checkip.dyndns.org/
      timeout 10 >nul /nobreak
      
      %sendkeys% "(^s)"
      timeout 1 >nul /nobreak
      %sendkeys% "{enter}"
      timeout 5 >nul /nobreak
      setlocal enabledelayedexpansion
      for /f "delims=" %%a in ('type "Current IP Check.htm" ^| find /i "Address"') do set $line=!%%a%!
      echo Your external IP is : %$line:~1%
      goto :EOF
      
      
      @end
      
      
      // JScript section
      
      var WshShell = WScript.CreateObject("WScript.Shell");
      WshShell.SendKeys(WScript.Arguments(0))
      

      ;

      【讨论】:

        猜你喜欢
        • 2011-05-15
        • 2016-01-04
        • 2011-12-18
        • 2018-02-18
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多