使用 VBScript 合并所有打开的 Notepad.exe 实例并保存到 txt 文件 [英] Use VBScript to Merge all open instances of Notepad.exe and save to txt file
问题描述
我正在 vbscript 中寻找一种方法来查找 notepad.exe 的任何打开实例,从中复制文本并创建一个包含所有内容的新文件并保存.
I am looking for a way in vbscript to find any open instances of notepad.exe, copy the text from them and create a new file that contains the contents of all of them and save it.
我已经编写了代码来实际找到正在运行的实例,只是无法找到一种方法来从中复制文本!
I have worked out the code to actually find the running instances, just can't work out a way to copy the text out of them!
Dim objWMIService, objProcess, colProcess,WshShell
Dim strComputer, strList
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process")
For Each objProcess in colProcess
if objProcess.Name = "notepad.exe" then
msgbox objProcess.processID
WshShell.AppActivate (objProcess.processID)
'copy the text from notepad into a new file....
end if
Next
推荐答案
我已经创建了一些有用的东西 - 它有点粗糙,但可以完成工作!它本质上使用发送键依次恢复每个记事本,复制文本,关闭文件而不保存,然后将内容粘贴到新创建的主"文本文件中.不过一定有更好的方法!
I have created something that works - it's a bit rough, but does the job! It essentially uses sendkeys to restore each notepad in turn, copy the text, close the file without saving then paste the contents into a newly created "master" text file. There must be a better way though!
Dim objWMIService, objProcess, colProcess,WshShell
Dim strComputer, strList
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process")
dim counter
counter=0
dim newid
For Each objProcess in colProcess
if objProcess.Name = "notepad.exe" and objProcess.processID<>newid then
counter=counter+1
if counter=1 then
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = WshShell.SpecialFolders("Desktop")&"\"&serial&".txt"
Set MyFile = fso.CreateTextFile(strPath, True)
MyFile.Close
Set EngineRun = WshShell.exec("notepad.exe " & strPath)
newid=EngineRun.ProcessID
WshShell.AppActivate(newid)
end if
'msgbox objProcess.processID
WshShell.AppActivate (objProcess.processID)
WScript.Sleep 500
WshShell.sendkeys "% r"
WScript.Sleep 500
WshShell.sendkeys "%E" ' edit
WScript.sleep 500
WshShell.sendkeys "a"
WScript.sleep 500
WshShell.sendkeys "%E" ' edit
WScript.sleep 500
WshShell.sendkeys "c"
WScript.sleep 500
WScript.sleep 500
WshShell.sendkeys "%F" ' edit
WScript.sleep 500
WshShell.sendkeys "x"
WScript.sleep 500
WshShell.sendkeys "n"
WScript.sleep 500
WshShell.AppActivate (newid)
WScript.sleep 500
WshShell.sendkeys vbNewLine & " --- " & objProcess.CommandLine & " --- " & vbNewLine
WScript.sleep 500
WshShell.sendkeys "%E" ' edit
WScript.sleep 500
WshShell.sendkeys "p"
end if
Next
WshShell.AppActivate (newid)
WScript.sleep 500
WshShell.sendkeys "%F" ' edit
WScript.sleep 500
WshShell.sendkeys "s"
WScript.sleep 500
WScript.sleep 500
WshShell.sendkeys "%F" ' edit
WScript.sleep 500
WshShell.sendkeys "x"
WScript.sleep 500
function serial()
strSafeDate = DatePart("yyyy",Date) & Right("0" & DatePart("m",Date), 2) & Right("0" & DatePart("d",Date), 2)
strSafeTime = Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)
strDateTime = strSafeDate & strSafeTime
serial=strDateTime
end function
这篇关于使用 VBScript 合并所有打开的 Notepad.exe 实例并保存到 txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!