【学习笔记】通过windows自带命令WMIC,查询浏览器版本号信息,对比Selenium驱动的版本号信息

【简单介绍】

WMIC 是 Windows Management Instrumentation Command-line 的缩写,是一个功能强大的 Windows 命令行工具,可以用于访问和管理 Windows Management Instrumentation (WMI) 提供的各种信息和功能。

WMIC datafile 是 WMIC 的一个子命令,用于管理和查询文件系统中的文件信息。以下是 WMIC datafile 的主要管理命令:

【管理命令】

  1. **get**: 用于获取文件的属性信息,如文件名、路径、版本、大小、创建时间和修改时间等。

示例:

```

C:\> wmic datafile where "name='notepad.exe'" get Name,Path,Version,Size,CreationDate,LastModified

```

  1. **list**: 列出符合条件的文件信息。

示例:

```

C:\> wmic datafile list brief

```

  1. **where**: 用于过滤查询结果,支持多个条件。

示例:

```

C:\> wmic datafile where "name='notepad.exe'" get Name,Path,Version

```

  1. **assoc**: 用于关联文件扩展名与文件类型。

示例:

```

C:\> wmic datafile where Extension=".txt" assoc

```

  1. **call**: 用于执行 WMI 方法。

示例:

```

C:\> wmic datafile where Name="notepad.exe" call Rename NewName="notepad2.exe"

```

【扩展命令】

下面是 WMIC datafile 扩展指令的详细介绍:

  1. **Name**: 文件名,包括扩展名。例如: "notepad.exe"。

  2. **Path**: 文件的完整路径。例如: "C:\Windows\System32\notepad.exe"。

  3. **Version**: 文件的版本信息。例如: "6.1.7601.17514"。

  4. **Size**: 文件的大小,以字节为单位。例如: "69632"。

  5. **CreationDate**: 文件的创建日期和时间。例如: "20090713225622.000000+000"。

  6. **LastModified**: 文件的最后修改日期和时间。例如: "20090713225622.000000+000"。

  7. **Drive**: 文件所在的驱动器器盘符。例如: "C"。

  8. **Directory**: 文件所在的目录路径。例如: "\Windows\System32"。

  9. **FileName**: 文件名,不包括扩展名。例如: "notepad"。

  10. **Extension**: 文件扩展名。例如: ".exe"。

下面是一个示例,演示如何使用 WMIC datafile 命令查询 notepad.exe 文件的所有信息:

```

【官方举例】

C:\> wmic datafile where "name='notepad.exe'" get Name,Path,Version,Size,CreationDate,LastModified,Drive,Directory,FileName,Extension

Name Path Version Size CreationDate LastModified Drive Directory FileName Extension

notepad.exe C:\Windows\System32\notepad.exe 6.1.7601.1 69632 20090713225622.000000+000 20090713225622.000000+000 C \Windows\System32 notepad .exe

```

从上面的输出可以看到,notepad.exe 文件位于 C:\Windows\System32 目录下,版本号为 6.1.7601.1,文件大小为 69,632 字节,创建时间为 2009-07-13 22:56:22,最后修改时间也是 2009-07-13 22:56:22。

【举例】

wmic datafile /?

动词有ASSOC,CALL,CREATE,DELETE,GET,LIST 这几个

命令:

wmic datafile where "filename='dsc04059' and extension='jpg' and drive='f:'" list /format:value

结果:

AccessMask=18809343

Archive=TRUE

Caption=f:\dsc04059.jpg

Compressed=FALSE

CompressionMethod=

CreationClassName=CIM_LogicalFile

CreationDate=20091012174149.578125+480

CSCreationClassName=Win32_ComputerSystem

CSName=20090621-1240

Description=f:\dsc04059.jpg

Drive=f:

EightDotThreeFileName=f:\dsc04059.jpg

Encrypted=FALSE

EncryptionMethod=

Extension=jpg

FileName=dsc04059

FileSize=86092

FileType=Kankan JPEG 图像

FSCreationClassName=Win32_FileSystem

FSName=NTFS

Hidden=FALSE

InstallDate=20091012174149.578125+480

InUseCount=

LastAccessed=20100412155336.781250+480

LastModified=20091009140702.000000+480

Manufacturer=

Name=f:\dsc04059.jpg

Path=\

Readable=TRUE

Status=OK

System=FALSE

Version=

Writeable=TRUE

实例:

DATAFILE -- DataFile 管理

::查找e盘下test目录(不包括子目录)下的cc.cmd文件

wmic datafile where "drive='e:' and path='\\test\\' and FileName='cc' and Extension='cmd'" list

::查找e盘下所有目录和子目录下的cc.cmd文件,且文件大小大于1K

wmic datafile where "drive='e:' and FileName='cc' and Extension='cmd' and FileSize>'1000′" list

::删除e盘下文件大小大于10M的.cmd文件

wmic datafile where "drive='e:' and Extension='cmd' and FileSize>'10000000′" call delete

::删除e盘下test目录(不包括子目录)下的非.cmd文件

wmic datafile where "drive='e:' and Extension<>'cmd' and path='test'" call delete

::复制e盘下test目录(不包括子目录)下的cc.cmd文件到e:\,并改名为aa.bat

wmic datafile where "drive='e:' and path='\\test\\' and FileName='cc' and Extension='cmd'" call copy "e:\aa.bat"

::改名c:\hello.txt为c:\test.txt

wmic datafile "c:\\hello.txt" call rename c:\test.txt

::查找h盘下目录含有test,文件名含有perl,后缀为txt的文件

wmic datafile where "drive='h:' and extension='txt' and path like '%\\test\\%' and filename like '%perl%'" get name

【常用命令】

获取版本号信息:

wmic datafile where name="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" get version / value
wmic datafile where name="C:\\Program Files\\Microsoft\Edge\\Application\\msedge.exe" get version / value
wmic datafile where name="D:\\Program Files\\FireFox\\firefox.exe" get Manufacturer,Version,Filename / value

python 复制代码
#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
import os
import subprocess

# wmic datafile where name="C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" get version / value
# wmic datafile where name="C:\Program Files\Google\Chrome\Application\chrome.exe" get version / value
# wmic datafile where name="D:\\Program Files\\FireFox\\firefox.exe" get version / value

code = r"""
wmic datafile where name="C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" get version / value
wmic datafile where name="D:\Test Files\Python385\msedgedriver.exe" get version / value
"""

codelist = code.split("\n")
for cmd in codelist[0:]:
    ''' ①os.system(),没有返回值,改为用os.open();
        ②通过文件路径,获取文件版本号信息,赋值为list'''
    if cmd:
        cmd = cmd.replace("\\","\\\\")
        cmd_get_version = cmd
        result_list_str = "".join([c for c in os.popen(cmd_get_version).readlines() if len(c) > 10])
        print(cmd)
        print(result_list_str)  # Version=121.0.6167.85
相关推荐
程序视点3 小时前
Window 10文件拷贝总是卡很久?快来试试这款小工具,榨干硬盘速度!
windows
wuk9984 小时前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
烛阴5 小时前
简单入门Python装饰器
前端·python
lzb_kkk5 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼5 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
面朝大海,春不暖,花不开5 小时前
使用 Python 实现 ETL 流程:从文本文件提取到数据处理的全面指南
python·etl·原型模式
2301_805054566 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python
万千思绪7 小时前
【PyCharm 2025.1.2配置debug】
ide·python·pycharm
微风粼粼8 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
云天徽上9 小时前
【PaddleOCR】OCR表格识别数据集介绍,包含PubTabNet、好未来表格识别、WTW中文场景表格等数据,持续更新中......
python·ocr·文字识别·表格识别·paddleocr·pp-ocrv5