获取文件夹中的所有文件版本

时间:2017-04-25 15:12:11

标签: batch-file command-line

我正在尝试获取我设法做的文件夹中的所有文件的文件版本(但不是很好)但现在我想将文件夹名称与版本一起粘贴,以便我知道哪个版本是哪个文件夹。 我不是很擅长命令行,只在我需要的时候用它来完成一些小任务,所以我事先道歉。 这就是我所做的:

For /d %%a in (C:\high\low\*) Do (For /d %%* in (%%a) Do wmic datafile where name="%%~da\\high\\low\\%%~nx*\\bin\\Services.dll" get Version /value) 我输出为:

  

`Version = 2.2.0.1 Version = 2.2.0.4 Version = 2.2.0.4 .... Version = 2.2.0.4

C:\high\low下有20个文件夹,我想进入每个子文件夹的bin目录,这样我就可以看到哪个文件夹已经升级,哪个文件夹没有升级。

修改
有20多个文件夹和结构是这样的:
C:\high\low\office.Services.Bolton\bin\Services.dll
C:\high\low\office.Services.Slough\bin\Services.dll
C:\high\low\office.Services.Hull\bin\Services.dll
.
.
.
C:\high\low\office.Services.Cosham\bin\Services.dll

我想检查Services.dll的版本号,并需要输出为:
Bolton - 2.2.0.1
Slough - 2.3.0.1
Hull - 2.5.0.1
.
.
.
Cosham - 2.0.0.0

提前致谢..

1 个答案:

答案 0 :(得分:1)

而不是堆叠for /d,您可以执行dir /b/s查找所有Services.dll并使用for /f解析wmic的讨厌(cr,cr,lf)输出:

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=*" %%A in (
  'Dir /B/S C:\high\low\Services.dll ^|findstr /i "bin\\Services.dll$"'
) Do (
  Set "DLL=%%~fA"
  Set "DLL=!DLL:\=\\!"
  For /F "tokens=1* delims==" %%B in (
    'wmic datafile where name^="!DLL!" get Version /value^|findstr Version'
  ) Do For /f "delims=" %%D in ("%%C") Do Echo Version: %%D   %%~fA
)
  • 第一个For / f解析''内的commamd输出,迭代%% A中传递的每一行(我更喜欢大写变量,以更好地区分小写~修饰符。 / LI>
  • 由于Dir仅在最后一个元素中允许使用通配符,因此我无法执行Dir /B/S C:\high\low\*\bin\Services.dll
  • 为了使我在bin文件夹中只获得Services.dll,我将dir输出管道输出到findstr /i "bin\\Services.dll$(findstr默认使用有限的RegEx,因此\必须使用另一个$进行转义,{ {1}}将表达式锚定在行的末尾。)。
  • wmic命令需要路径中的反斜杠也可以使用字符串替换进行转义(仅适用于普通变量)
  • 在(代码块)中,我们需要延迟扩展以获取代码块中更改的变量的实际值,因此!代替%来构建变量名称
  • 第二个For / f解析等号的wmic输出分割,将内容分配给var %% C
  • 编辑为/ remove the wmic cr
  • 添加了/ f
相关问题