不需要创建日期
您可以调用Windows'tasklist https://learn.microsoft.com/es-es/windows-server/administration/windows-commands/tasklist来自 Matlab 的命令使用system https://www.mathworks.com/help/matlab/ref/system.html,然后解析结果:
name = 'notepad.exe';
[~, s] = system(['tasklist /FI "imagename eq ' name '"']);
result = regexp(s, ['(?<=' strrep(name, '.', '\.') '\s*)\d+'], 'match');
result = str2double(result); % convert to numbers if needed
的结果system
具有以下形式(打开两个记事本窗口;西班牙语 Windows 版本):
>> s
s =
'
Nombre de imagen PID Nombre de sesión Núm. de ses Uso de memor
========================= ======== ================ =========== ============
notepad.exe 12576 Console 1 13,488 KB
notepad.exe 13860 Console 1 13,484 KB
'
因此,正则表达式会搜索程序名称前面的数字和可选空格,以给出最终结果
>> result =
12576 13860
需要创建日期
如果你需要根据创建日期过滤,您可以使用Windows'wmic https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmic:
name = 'notepad.exe';
[~, s] = system(['wmic process where name=''' name ''' get ProcessId, CreationDate']);
这给出了一个字符串,例如
s =
'CreationDate ProcessId
20191015151243.876221+120 6656
20191015151246.092357+120 4004
'
The CreationDate
在里面format https://learn.microsoft.com/es-es/windows/win32/wmisdk/cim-datetime?redirectedfrom=MSDN yyyymmddHHMMSS+UUU
, where +UUU
or -UUU
是距 UTC 的分钟数。
你可以解析s
转换为字符串元胞数组,如下所示:
result = reshape(regexp(s, '[\d+\.]+', 'match').', 2, []).'; % 2 is the number of columns
这给出了
result =
2×2 cell array
{'20191015151243.876221+120'} {'6656'}
{'20191015151246.092357+120'} {'4004'}
然后您可以根据第一列进行过滤。