批量转换为vbs进行循环转换

时间:2016-03-25 17:17:41

标签: batch-file vbscript

我有可执行文件的目录,如

tool-7.0.20.exe
tool-7.0.23.exe
tool-7.0.24.exe
tool-7.0.25.exe
tool-7.0.26.exe

我在批处理文件中使用for循环来获取变量中的最新版本并运行它 我需要将此for循环转换为vbs脚本

for /f "tokens=*" %%a in ('dir /ON /B %~dps0tool*.exe') do set latest=%%a

然后使用objShell.Exec或objShell.Run运行最新的文件变量

由于

1 个答案:

答案 0 :(得分:2)

您可以使用Shell.Application ActiveX:

来执行此操作
Option Explicit

Const SHCONTF_NONFOLDERS = &H40
Const SHCONTF_INCLUDEHIDDEN = &H80
Dim strCurDir, strPath, objWshShell

strCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
With CreateObject("Shell.Application").Namespace(strCurDir).Items
    .Filter SHCONTF_NONFOLDERS + SHCONTF_INCLUDEHIDDEN, "tool-*.exe"
    strPath = .Item(.Count - 1).Path
End With
WScript.Echo strPath
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.Run strPath

或者使用Scripting.FileSystemObject检索文件并使用VBScript.RegExp过滤它们:

Option Explicit

Dim strFiles, objMatches, strPath, objWshShell

With CreateObject("Scripting.FileSystemObject")
    With .GetFolder(.GetParentFolderName(WScript.ScriptFullName))
        strFiles = ""
        For Each strPath In .Files
            strFiles = strFiles & strPath & vbCrLf
        Next
    End With
End With
With CreateObject("VBScript.RegExp")
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = "^.+?\\tool-.*?\.exe$"
    Set objMatches = .Execute(strFiles)
    strPath = objMatches(objMatches.Count - 1).Value
End With
WScript.Echo strPath
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.Run strPath

甚至运行你的cmd代码(cmd指令稍作修改,以便在一个衬管命令行模式下执行):

Option Explicit

Dim objWshShell, strCurDir, strCmd, strRes

Set objWshShell = CreateObject("WScript.Shell")
strCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
strCmd = "%comspec% /v /c (for /f %a in ('dir /ON /B /S " & strCurDir & "tool-*.exe') do @set latest=%a)&echo !latest!"
strRes = objWshShell.Exec(strCmd).StdOut.ReadAll()
strRes = Replace(strRes, vbCrLf, "")
WScript.Echo strRes
objWshShell.Run strRes

如果你想摆脱闪烁的控制台窗口,可以按如下方式修改上面的代码,以隐藏模式启动脚本的第二个实例,并在其中执行cmd指令:

Option Explicit

Dim objWshShell, strCurDir, strCmd, strRes, objWnd, objParent, strSignature

Set objWshShell = CreateObject("WScript.Shell")
If WScript.Arguments.Named.Exists("signature") Then WshShellExecCmd
strCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
strCmd = "%comspec% /v /c (for /f %a in ('dir /ON /B /S " & strCurDir & "tool-*.exe') do @set latest=%a)&echo !latest!"
RunCScriptHidden
strRes = Replace(strRes, vbCrLf, "")
WScript.Echo strRes
objWshShell.Run strRes

Sub RunCScriptHidden()
    strSignature = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
    GetObject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}").putProperty strSignature, Me
    objWshShell.Run ("""" & Replace(LCase(WScript.FullName), "wscript", "cscript") & """ //nologo """ & WScript.ScriptFullName & """ ""/signature:" & strSignature & """"), 0, True
End Sub

Sub WshShellExecCmd()
    For Each objWnd In CreateObject("Shell.Application").Windows
        If IsObject(objWnd.getProperty(WScript.Arguments.Named("signature"))) Then Exit For
    Next
    Set objParent = objWnd.getProperty(WScript.Arguments.Named("signature"))
    objWnd.Quit
    objParent.strRes = objWshShell.Exec(objParent.strCmd).StdOut.ReadAll()
    WScript.Quit
End Sub