I'm trying to find out what's the full path of a running bat file from an external application. The process is cmd.exe but I can't get the name for the actual running bat. In task manager it shows as cmd.exe If I get the process as an object, the closest property to the bat is in MainWindowTitle. Is there any way to get the full running bat path from it's running cmd process?
-
Easy as pie. echo %0 – Squashman Dec 03 '17 at 05:23
-
@Andrei: `tasklist /fi "imagename eq cmd.exe" /v` could help. Parse it with a `for /f` loop to get the window title. – Stephan Dec 03 '17 at 14:46
-
@Squashman: Please read my question again. Tried to make more clear what I'm after. – Andrei Dec 03 '17 at 15:30
2 Answers
The answer written by vtrz on question How to check if a process is running via a batch script? contains the command you are looking for:
%SystemRoot%\System32\wbem\wmic.exe PROCESS where (name="cmd.exe") GET CommandLine
Windows Management Instrumentation Command-line utility with those parameters lists all running cmd.exe
processes line by line with the command line used to start them.
But that means if a user opened a command prompt window and started from within this window a batch file executed by already started cmd.exe
, the command line output for this command process is just "C:\Windows\System32\cmd.exe"
. As far as I know it is not possible to get from an already running command process the information what this command process currently executes.
Well, if the executed batch files use the command title to give their console windows meaningful titles, it is also possible to use tasklist to get information about a command processes with a specific window title or use taskkill to terminate or kill a command process with a specific window title.

- 46,139
- 17
- 80
- 143
-
I can't belive I didn't think to turn on WMI! Duh... WIN32_Process -> CommandLine will do the trick. I'll have to querry WIN32_Process by ProcessID to make sure I get the right instance and pharse CommandLine from there. Thanks :) – Andrei Dec 03 '17 at 22:43
This is the function I ended up with (VB.NET), if anyone cares. It can retrieve the bat path from cmd.exe process and also I use it to get vbs files from wscript.exe. It receives as argument the ProcessID of cmd.exe or wscript.exe and returns a list of string because I needed to get also argument files passed to vbs. The parsing part works well at list in scenarios that I use it in.
Function GetArgFiles(PID As Integer) As List(Of String)
Dim Ret As New List(Of String)
Try
Dim MOS As New ManagementObjectSearcher("root\CIMV2", "SELECT Name, CommandLine FROM WIN32_Process where ProcessID = '" & PID & "'")
For Each MO As ManagementObject In MOS.[Get]()
Try
Dim name As String = MO.GetPropertyValue("Name")
Dim CommandLine As String = MO.GetPropertyValue("CommandLine")
If CommandLine Is Nothing Then Exit Try
For Each CLE As String In New List(Of String)(CommandLine.Split(Chr(34)))
Try
CLE = CLE.Trim
If CLE.Length < 5 Then Continue For
If CLE.ToLower Like "*" & name.Trim.ToLower & "*" Then Continue For
If CLE Like "*:\*" Then
CLE = CLE.Substring(CLE.LastIndexOf(":\") - 1)
Else
Continue For
End If
If CLE.Contains("/") Then CLE = CLE.Substring(0, CLE.LastIndexOf("/"))
If CLE.Substring(5).Contains(":") Then CLE = CLE.Substring(0, CLE.LastIndexOf(":"))
If File.Exists(CLE.Trim) Then Ret.Add(CLE.Trim)
Catch
End Try
Next
Catch
End Try
Next
Catch
End Try
Return Ret
End Function

- 33
- 9