Click here to Skip to main content
16,007,277 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to check if the name.bat open or close but

What I have tried:

i try this with no success
VB
Dim CMDProc As Process() = Process.GetProcessesByName("window name")
    If CMDProc.Length > 0 Then
    Else
    '''''
  End If

the "Process.GetProcessesByName" not worked on batch file why?
Posted
Updated 22-Apr-19 21:37pm
Comments
Richard MacCutchan 22-Apr-19 13:15pm    
Maybe your process name is not correct. You can easily check by using Task Manager.
MasterGamerFX 22-Apr-19 13:23pm    
i cant see process name of the cmd window
is there a way to check if cmd windowtitle is running in VB?
[no name] 22-Apr-19 14:10pm    
"Process()" has a "Length"? Maybe you're not fast enough to catch any "batch windows". Maybe try a semaphore file and / or a file watcher.
MasterGamerFX 22-Apr-19 14:24pm    
you can get me example please

The "ProcessName" is not the title of the window. It's the name of the executable that's running without the ".exe" on the end. For any batch file, that would be "cmd".

Now, to find the CMD window that is running your batch file, you would look the MainWindowTitle of each of your CMD Process objects that you got back from Process.GetProcessesByName("cmd") and look for the text of the command line that you launched the batch file with.

For example, if your batch file is called "Test.bat" and you launched it with a command line of "test /someArgument 123", the MainWindowTitle property of the Process object for the CMD prompt window would be "C:\Windows\System32\cmd.exe - test /someArgument 123".
 
Share this answer
 
v2
Comments
MasterGamerFX 22-Apr-19 15:24pm    
explain me better ,please
i don't understand
Dave Kreskowiak 22-Apr-19 15:37pm    
I don't know how to simplify this. You're getting the list of processes using GetProcessesByName("window name"). You can NOT use a window title in that call. You have to pass "cmd" instead.

Once you have the list of Processes, you have to check the MainWindowTitle property of each of those for what I described.

This comes down to reading the documentation on the Process class and using the debugger. You could have easily figured this out.
MasterGamerFX 22-Apr-19 16:20pm    
Let me an example of that
Dave Kreskowiak 22-Apr-19 16:52pm    
FFS. YOU'RE ALREADY DOING IT!! Just change the "window name" to "cmd".
An open file is locked. Try opening it in a try/catch statement
 
Share this answer
 
Comments
Dave Kreskowiak 22-Apr-19 21:08pm    
I'm thinking he wants to know if it's running or not. Last I checked, a .BAT file can be written to while it's running.
MasterGamerFX 22-Apr-19 23:31pm    
Correct, how i can do this
I try ,no sucess from me
Can you get me example
Dave Kreskowiak 23-Apr-19 8:16am    
You can't, and won't succeed until you start using the debugger and start reading the documentation on the functions (methods) you're trying to use.

Right now, you're a copy'n'paste code hoping that whatever you copy into your own code will work. You have no understanding of anything at all.
Christian Graus 22-Apr-19 23:33pm    
Dave says that a batch file is not locked when it's running. In that case, I think you were taking the right approach. I'd first confirm I could get a long running process with this code by name. Then I'd run the batch file and see what it's process is called. If it's something generic, you're hosed. If it's the file name, you can use that. Of course, this code will be very fragile either way
MasterGamerFX 23-Apr-19 3:10am    
Ok but if i use get process by id the id of the batch file can changes everytime ,no ?
 
Share this answer
 
Comments
MasterGamerFX 23-Apr-19 4:43am    
Not working with .bat batch file
Christian Graus 23-Apr-19 4:51am    
It's an API. The code will work. You just need to work out what the name is. I'm concerned it's something generic. Have you checked?
MasterGamerFX 23-Apr-19 5:26am    
Process.GetProcessesByName Method (System.Diagnostics)

Not working i checked out
Can you try this for me please
MasterGamerFX 23-Apr-19 12:50pm    
i check it now this detect the cmd.exe but not window title i try cmd.exe -windowtitle eq test
Christian Graus 23-Apr-19 17:10pm    
OK, if it gets run as cmd.exe, which I expected, then you can't do what you want

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900