Dos下如何判断某个文件或者字符串中包含某个子串

例子:

       我们读取网卡信息,并判断是否存在虚拟ip地址为“10.8.0.6”的网卡,如果存在,则生成一个叫做status.txt的文件,并写入Ok字符串。在cmd下我们可以直接如下操作:

ipconfig | find "10.8.0.6" /N  >nul && echo  "Ok" > status.txt

 那么引申使用,在应用程序中判断是否存在“10.8.0.6”这个字符串,可以这样做:

以Qt程序为例.

#define APP_DIR QApplication::applicationDirPath()
bool checkVsAdapters(){
    QString filename = APP_DIR  + "/status.txt";
    QString cmdstr = QString("cmd /c cd /d %1 && ipconfig | find \"10.8.0.\" /N  >nul && echo Ok > %2").arg(APP_DIR).arg(filename);
    WinExec(cmdstr.toLatin1().data(), SW_HIDE);
    Sleep(2000);
    return  QFile::exists(filename);
}

ipconfig : 读取网卡信息


USAGE:
    ipconfig [/allcompartments] [/? | /all |
                                 /renew [adapter] | /release [adapter] |
                                 /renew6 [adapter] | /release6 [adapter] |
                                 /flushdns | /displaydns | /registerdns |
                                 /showclassid adapter |
                                 /setclassid adapter [classid] |
                                 /showclassid6 adapter |
                                 /setclassid6 adapter [classid] ]

where
    adapter             Connection name
                       (wildcard characters * and ? allowed, see examples)

    Options:
       /?               Display this help message
       /all             Display full configuration information.
       /release         Release the IPv4 address for the specified adapter.
       /release6        Release the IPv6 address for the specified adapter.
       /renew           Renew the IPv4 address for the specified adapter.
       /renew6          Renew the IPv6 address for the specified adapter.
       /flushdns        Purges the DNS Resolver cache.
       /registerdns     Refreshes all DHCP leases and re-registers DNS names
       /displaydns      Display the contents of the DNS Resolver Cache.
       /showclassid     Displays all the dhcp class IDs allowed for adapter.
       /setclassid      Modifies the dhcp class id.
       /showclassid6    Displays all the IPv6 DHCP class IDs allowed for adapter.
       /setclassid6     Modifies the IPv6 DHCP class id.


The default is to display only the IP address, subnet mask and
default gateway for each adapter bound to TCP/IP.

For Release and Renew, if no adapter name is specified, then the IP address
leases for all adapters bound to TCP/IP will be released or renewed.

For Setclassid and Setclassid6, if no ClassId is specified, then the ClassId is removed.

Examples:
    > ipconfig                       ... Show information
    > ipconfig /all                  ... Show detailed information
    > ipconfig /renew                ... renew all adapters
    > ipconfig /renew EL*            ... renew any connection that has its
                                         name starting with EL
    > ipconfig /release *Con*        ... release all matching connections,
                                         eg. "Wired Ethernet Connection 1" or
                                             "Wired Ethernet Connection 2"
    > ipconfig /allcompartments      ... Show information about all
                                         compartments
    > ipconfig /allcompartments /all ... Show detailed information about all
                                         compartments

 find: 在单个或多个文件中查找字符串

Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]

  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.

If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.

发布了99 篇原创文章 · 获赞 25 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_39568531/article/details/103969120