使用VBScript合并Notepad.exe的所有打开实例并保存到txt文件

时间:2016-03-30 09:41:57

标签: vbscript wsh

我正在寻找vbscript中的一种方法来查找notepad.exe的任何打开实例,从中复制文本并创建一个包含所有内容的新文件并保存。

我已经找到了实际找到正在运行的实例的代码,只是无法找到一种方法来复制文本!

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

2 个答案:

答案 0 :(得分:1)

试试看结果:

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
'***************************************************

编辑于2016年3月31日@ 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
'***************************************************

答案 1 :(得分:0)

我创造了一些有用的东西 - 它有点粗糙,但是能完成这项工作!它主要使用sendkeys依次恢复每个记事本,复制文本,关闭文件而不保存,然后将内容粘贴到新创建的" master"文本文件。必须有一个更好的方法!

class CustomCell: UITableViewCell {
@IBOutlet weak var imgPost1: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    imgPost1.tag=1
    let tap = UITapGestureRecognizer(target: self, action: #selector(CustomCell.tappedMe))
    imgPost1.addGestureRecognizer(tap)
    imgPost1.userInteractionEnabled = true
}
func tappedMe(xTag:Int) {
    print(xTag)
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}
相关问题