2016-07-26 313 views
1

我有2行,我必须在cmd每天运行多次。这些线类似于下面的那些:从VBA运行多行Shell命令/批处理文件

set_someclasspaths.bat 
java -Xmx1024m foo.bar.stuff.Dashboard -var varone -from 20160701 -to 20160731 -outputdir c:\stuff -ir @ -dashboard 

我想过两种方法来自动执行此操作。第一个是批处理文件,第二个是发送密钥。我已经做了一个文件标记它的东西。巴特和粘贴在上述两行。这很快就会在cmd中打开一个窗口并再次关闭(即使我在最后添加了暂停),并且不会做我期望的事情 - 事实上,只要我能够告诉它运行第一个命令,那么什么都没有。

的发送键的方法似乎只运行第一线,我曾尝试:

Sub testcreate() 
Dim wsh As Object 
Set wsh = VBA.CreateObject("WScript.Shell") 
Dim waitOnReturn As Boolean: waitOnReturn = True 
Dim windowStyle As Integer: windowStyle = 1 

wsh.Run "cmd.exe /K C:\a\b\set_someclasspaths.bat", windowStyle, waitOnReturn 
wsh.sendkeys "java -Xmx1024m foo.bar.stuff.Dashboard -var varone -from 20160701 -to 20160731 -outputdir c:\stuff -ir @ -dashboard" 
End Sub 

我的批处理文件和Java的能力是值得怀疑的最好的,这似乎像整齐的方法来解决,这是批处理文件,所以我宁愿这样做。

我想我将参数传递给第二行的java脚本,即时通讯不知道是否有语法,也没有学习一些Java和批处理文件的东西,我已经停下来。

为了澄清,我的问题有两个:首先是批处理文件更好的方法来做到这一点?其次,为什么这不起作用?

非常感谢。

回答

3

当你从另一个调用一个批处理文件,控制传递到第二,并没有返回。

如果你想它返回时,需要在call通过,那么你的批处理文件将是这样的:

call set_someclasspaths.bat 
java -Xmx1024m foo.bar.stuff.Dashboard -var varone -from 20160701 -to 20160731 -outputdir c:\stuff -ir @ -dashboard 
1

关于什么:

Sub CreateAndRun() 

Dim batchContents As String 
Dim batchFile  As String 
Dim FF   As Byte 

batchFile = Environ$("USERPROFILE") & "\temp.bat" 

batchContents = "CALL set_someclasspaths.bat" & vbCrLf & _ 
       "java -Xmx1024m foo.bar.stuff.Dashboard -var varone -from 20160701 -to 20160731 -outputdir c:\stuff -ir @ -dashboard" 

FF = FreeFile 

Open batchFile For Output As #FF 
    Print #FF, batchContents 
Close #FF 

CreateObject("WScript.Shell").Run batchFile, 1, True 

DoEvents 

Kill batchFile 

End Sub 
1

创建像wrapper.bat包装批处理文件并粘贴两个命令在那里:

call set_someclasspaths.bat 
java -Xmx1024m foo.bar.stuff.Dashboard -var varone -from 20160701 -to 20160731 -outputdir c:\stuff -ir @ -dashboard 
pause 

这应该执行这两个批处理文件和你的Java语句,然后保持窗口打开。

不必一直通过VBA创建一个批处理文件,只需单击按钮或其他东西就可以执行它。