本文介绍了如何在Windows批处理文件中使用WMIC将DLL或EXE的文件版本转换为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Windows批处理文件获取文件版本.此命令将版本成功打印到控制台.

I try to get the file version using a Windows batch file. This command successfully prints the version to console.

WMIC DATAFILE WHERE name="Z:\\bin\\My_project.dll" get Version /format:Textvaluelist

但是当我尝试使用以下方法将此输出输出到变量时,Windows命令处理器输出:

But when I try to get this output to a variable using the below method, Windows command processor outputs:

此命令行有什么问题?

for /f %%a in ('WMIC DATAFILE WHERE name="Z:\\bin\\My_project.dll" get Version /format:Textvaluelist') do set val=%%a

推荐答案

这是我使用WMIC尝试此任务的方式:

This is how I'd attempt this task using WMIC:

For /F "Tokens=1* Delims==" %%A In (
    'WMIC DataFile Where "Name='Z:\\bin\\My_project.dll'" Get Version /Value 2^>Nul'
) Do For /F "Tokens=*" %%C In ("%%B") Do Set "val=%%C"

第二个For循环旨在克服Unicode WMIC结果导致的不必要输出.

The second For loop is designed to overcome the unwanted output resulting from the unicode WMIC result.

修改

如果未指定WMIC,则可以从批处理文件中使用VBScript:

If WMIC wasn't stipulated, you could utilise VBScript from your batch file:

使用Windows脚本宿主:

Using Windows Scripting Host:

<!-- :
@Echo Off
For /F %%A In ('CScript //NoLogo "%~f0?.wsf"') Do Set "val=%%A"
Set val 2>Nul
Pause
Exit /B

-->
<Job><Script Language="VBScript">
  WScript.Echo CreateObject("Scripting.FileSystemObject").GetFileVersion("Z:\bin\My_project.dll")
</Script></Job>

或通过mshta.exe:

@Echo Off
For /F %%A In ('
    MsHTA VBScript:Execute("Set o=CreateObject(""Scripting.FileSystemObject""):o.GetStandardStream(1).Write(o.GetFileVersion(""Z:\bin\My_project.dll"")):Close"^)
') Do Set "val=%%A"
Set val 2>Nul
Pause

这篇关于如何在Windows批处理文件中使用WMIC将DLL或EXE的文件版本转换为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:56