Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien
Toon posts:

[CMD] Process opzoeken zonodig starten

Pagina: 1
Acties:

Verwijderd

Topicstarter
Goedemorgen,

Momenteel kamp ik nog met een klein probleem. Ik probeer een batch te schrijven die kijkt of java.exe al gestart is in de Tasklist, als deze niet gestart is word hij gestart als hij al gestart is geeft hij de melding "Is al gestart".

In plaats van dat ik een melding krijg zou hij het process wat al actief is naar de voorgrond moeten verplaatsen. Alleen kom ik er momenteel niet meer uit.


Het onderstaande heb ik nu:

@echo off
tasklist|find /i /c "java.exe" > nul
set INSTANCE=%errorlevel%
if "%INSTANCE%"=="0" goto already
start C:\Windows\System32\java.exe
:: «%1» is the name and path of the file you want to run.
goto end
:already
echo. & echo Is al gestart
pause>nul
:end
set INSTANCE=
exit

  • Meekoh
  • Registratie: April 2005
  • Laatst online: 17-11 22:19
Volgens mij kon dat niet vanuit een batchfile...
Daarvoor zul je iets in een andere taal moeten doen.
Wellicht dat je dit met VBScript kunt bereiken, maar ik weet zeker dat het met Powershell moet kunnen.
Dit aangezien Powershell het hele .NET framework tot zijn beschikking heeft en ik weet dat C# het sowieso kan.
Dus het zit in ieder geval in .NET.

edit:
In Onderstaande VB.NET code wordt de windows API gebruikt om de focus te zetten op een Notepad scherm.
Dit zou je kunnen ombouwen naar Powershell, of VBscript.
Visual Basic .NET:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
' Used to get access to Win API calling attributes
Imports System.Runtime.InteropServices

Public Class Form1
    ' This is 2 functions from user32.dll (1 for finding the application and 1 to set it to foreground with focus)
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Long
    End Function

    ' Here we are looking for notepad by class name and caption
    Dim lpszParentClass As String = "Notepad"
    Dim lpszParentWindow As String = "Untitled - Notepad"

    Dim ParenthWnd As New IntPtr(0)


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Find the window and get a pointer to it (IntPtr in VB.NET)
        ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow)

        If ParenthWnd.Equals(IntPtr.Zero) Then
            Debug.WriteLine("Notepad Not Running!")
        Else
            ' Found it, so echo that we found it to debug window
            ' Then set it to foreground
            Debug.WriteLine("Notepad Window: " & ParenthWnd.ToString())
            SetForegroundWindow(ParenthWnd)
        End If
    End Sub
End Class

[ Voor 70% gewijzigd door Meekoh op 12-09-2011 11:26 ]

Computer says no


Verwijderd

Topicstarter
Maar na de check kan je d.m.v. een echo vertellen dat het proces al gestart is. Is het dan niet mogelijk om het gestarte proces na de voorgrond te zetten?