Batch file 批处理到vbs以进行循环转换
我有一个包含可执行文件的目录,比如Batch file 批处理到vbs以进行循环转换,batch-file,vbscript,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
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运行最新的文件变量
谢谢您可以使用
Shell。应用程序
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指令被稍微修改为在单行命令行模式下执行):
如果您想摆脱闪烁的控制台窗口,可以对上述代码进行如下修改,以在隐藏模式下启动脚本的第二个实例,并在其中执行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
这里有问题吗?或者这是我的脚本请求?您需要VBScript做什么?您似乎有一个for命令可以运行,并且您计划运行一个可执行文件,那么为什么不直接使用批处理文件呢?请不要只是随机添加标记,因为它们包含熟悉的单词或短语。我删除了Powershell和DOS标记,因为您的问题与这两个东西都无关。(Windows批处理文件不在DOS上运行,而且已经十年没有了。)Ansgar Wiechers,Ken White,Cindy Meister,greg-449,Tushar,你可以阅读@omegastripes Answer来理解我的问题谢谢,VBScript.RegExp方法最适合我
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