以Groovy或BAT脚本为窗口程序,以编程方式将钥匙笔触发送到窗口程序



backstory:我需要以编程方式找到两个文件之间的差异。我想使用winmerge生成一个报告(工具 ->生成报告),我可以解析以获取两个文件之间的差异。我需要使用Groovy脚本或BAT脚本完成此操作。

我希望winmerge能提供命令行选项来生成报告,然后我只能使用groovy Process对象来执行winmergeu.exe,并使用参数。根据我为Winmerge找到的命令选项,没有这种运气。

接下来,我希望能够启动winmerge并发送击键以逐步浏览菜单以生成报告(alt t,r,diff.html,[enter])。我看不到从一个令人讨厌的过程中做到这一点的方法,也没有找到在蝙蝠脚本中做到这一点的方法。我正在寻找类似于VB中的wshshell.sendkeys的东西。这是一个野生的追逐吗?

在BAT文件中使用PowerShell进行更新/答案:我对Knuckle-Dragger关于在BAT文件中使用PowerShell脚本的评论很感兴趣。

$folder = "C:DevToolsWinMergeWinMergeU.exe"
ii $folder
Start-Sleep -m 1000
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[Microsoft.VisualBasic.Interaction]::AppActivate("WinMerge")
Start-Sleep -m 100
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
Start-Sleep -m 100
[System.Windows.Forms.SendKeys]::SendWait("%F")
[System.Windows.Forms.SendKeys]::SendWait("o")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("%T")
[System.Windows.Forms.SendKeys]::SendWait("r")
Start-Sleep -m 1000
[Microsoft.VisualBasic.Interaction]::AppActivate("Save As")
Start-Sleep -m 1000
[System.Windows.Forms.SendKeys]::SendWait("Diff.txt")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

要将其封装在命令窗口中,请将其保存到文件 powershellscript.ps1

start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta *PowerShellScript.ps1*

这是激活Winmerge并发送一些键的PowerShell示例。

编辑:使用一些.NET变量减少复制面食。$ sk = sendkeys $ aa = appAcivate $ lrA =反射.net。

$startapp = "C:DevToolsWinMergeWinMergeU.exe"
ii $startapp
$SK = "[System.Windows.Forms.SendKeys]::SendWait"
$AA = "[Microsoft.VisualBasic.Interaction]::AppActivate"
$LRA = "[void][System.Reflection.Assembly]::LoadWithPartialName"
Start-Sleep 1
$LRA+'("Microsoft.VisualBasic")'
$AA+'("WinMerge")'
Start-Sleep -m 100
$LRA+'("System.Windows.Forms")'
Start-Sleep -m 100
$SK+'("%F")'
$SK+'("o")'
$SK+'("{ENTER}")'
$SK+'("%T")'
$SK+'("r")'
Start-Sleep 1
$AA+'("Save As")'
Start-Sleep 1
$SK+'("Diff.txt")'
$SK+'("{ENTER}")'

要将其封装在命令窗口中,请将其保存到文件 powershellscript.ps1 :注意,如果使用&{。 dot source path}

start /b /wait powershell.exe  -nologo -WindowStyle Hidden -sta -Command "& {.PowerShellScript.ps1}"

这是我在不进行测试的情况下扔的东西,而且我没有Winmerge,因此测试并不是真正的选择。

runme.bat

start "" "C:PathWinMerge.exe" "file1" "file2"
GenerateDiffFile.vbs

生成fffile.vbs

Set s = CreateObject("WScript.Shell")
wscript.sleep(1000) ' Sleep for 1 second to allow time for WinMerge to finish loading and merge the files
s.SendKeys("%tr")
wscript.sleep(250) ' Give time for dialog box to appear
s.SendKeys("Diff.html{Enter}")

这是完全未经测试的,但是我认为它非常接近您所需的内容...您可以看到批处理文件运行winmerge,传递两个文件以在命令行上合并。然后,它启动了VBS脚本,该脚本的停顿足够长,以允许启动的Winmerge能够接受键盘输入。我使用了Microsoft的此页面来知道要发送的密钥。

我多年来一直将钥匙发送到程序。只要您要发送键以保持焦点。

,它的工作非常可靠。

我要做的就是这样: 回声y |"需要击键的批处理"

最新更新