Просто дать попробовать и сказать мне результат:
Option Explicit
Dim Title,colItems,objItem,FilePath,ws
Dim MyProcess,LogFile,fso,Contents
MyProcess = "Notepad.exe"
Title = "Merge all open instances of "& DblQuote(MyProcess) &" and save it to a text file"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("WScript.Shell")
LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "txt"
If fso.FileExists(LogFile) Then
fso.DeleteFile(LogFile)
End If
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like '%" & wsh.scriptname & "%'",,48)
For Each objItem in colItems
FilePath = Mid(objItem.CommandLine,InStr(objItem.CommandLine,chr(34)) + 33)
FilePath = Replace(FilePath,chr(34),"")
FilePath = Trim(FilePath)
If Len(FilePath) > 0 Then
Contents = ReadFile(FilePath,"all")
Call WriteLog(Contents,LogFile)
End If
Next
If fso.FileExists(LogFile) Then
ws.run DblQuote(LogFile)
Else
MsgBox "No running instances found for this process " &_
DblQuote(MyProcess),vbExclamation,Title
End If
'**************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************
Function ReadFile(path,mode)
Const ForReading = 1
Dim objFSO,objFile,i,strLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path,ForReading)
If mode = "byline" then
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
strLine = objFile.ReadLine
strLine = Trim(strLine)
If Len(strLine) > 0 Then
arrFileLines(i) = strLine
i = i + 1
ReadFile = arrFileLines
End If
Loop
objFile.Close
End If
If mode = "all" then
ReadFile = objFile.ReadAll
objFile.Close
End If
End Function
'***************************************************
Sub WriteLog(strText,LogFile)
Dim fso,ts
Const ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(LogFile,ForAppending,True,-1)
ts.WriteLine strText
ts.Close
End Sub
'***************************************************
Редактировать на 31/03/2016 @ 10: 45
Я думаю, что это второй код может быть используется для обнаружения и редактирования любого вида vbscript, работающего на фоне!
Представьте, что VBScript работает на фоне является вирус, поэтому мы можем найти свой путь, редактировать и копировать его источник (-_ °)
Option Explicit
Dim Title,colItems,objItem,FilePath,ws
Dim MyProcess,LogFile,fso,Contents
MyProcess = "wscript.exe"
Title = "Search for all instances of "& DblQuote(MyProcess) &" and save it to a text file"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("WScript.Shell")
LogFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "txt"
If fso.FileExists(LogFile) Then
fso.DeleteFile(LogFile)
End If
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like '%" & wsh.scriptname & "%'",,48)
For Each objItem in colItems
FilePath = Mid(objItem.CommandLine,InStr(objItem.CommandLine,chr(34)) + 33)
FilePath = Replace(FilePath,chr(34),"")
FilePath = Trim(FilePath)
If Len(FilePath) > 0 Then
Contents = ReadFile(FilePath,"all")
Call WriteLog(DblQuote(FilePath) & vbCrlf & String(100,"*") & vbCrlf &_
Contents & vbCrlf & String(100,"*") & vbCrlf,LogFile)
End If
Next
If fso.FileExists(LogFile) Then
ws.run DblQuote(LogFile)
Else
MsgBox "No running instances found for this process " &_
DblQuote(MyProcess),vbExclamation,Title
End If
'**************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************
Function ReadFile(path,mode)
Const ForReading = 1
Dim objFSO,objFile,i,strLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path,ForReading)
If mode = "byline" then
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
strLine = objFile.ReadLine
strLine = Trim(strLine)
If Len(strLine) > 0 Then
arrFileLines(i) = strLine
i = i + 1
ReadFile = arrFileLines
End If
Loop
objFile.Close
End If
If mode = "all" then
ReadFile = objFile.ReadAll
objFile.Close
End If
End Function
'***************************************************
Sub WriteLog(strText,LogFile)
Dim fso,ts
Const ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(LogFile,ForAppending,True,-1)
ts.WriteLine strText
ts.Close
End Sub
'***************************************************
Использование 'SendKeys' всегда плохой подход к вещам, но с VBScript нет лучшего для того, что вы хотите сделать (хотя вы могли бы использовать '^ a','^c' вместо использования меню). –
Я сделал это изменение, и он, безусловно, работает намного гладко! – Andrzej