无需创建日期
您可以使用system
在Matlab中调用Windows的tasklist
命令,然后解析结果:
name = 'notepad.exe';
[~, s] = system(['tasklist /FI "imagename eq ' name '"']);
result = regexp(s, ['(?<=' strrep(name, '.', '\.') '\s*)\d+'], 'match');
result = str2double(result);
< p >
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
命令:
name = 'notepad.exe';
[~, s] = system(['wmic process where name=''' name ''' get ProcessId, CreationDate']);
这会产生一个字符串,例如:
s =
'CreationDate ProcessId
20191015151243.876221+120 6656
20191015151246.092357+120 4004
'
CreationDate
是以
格式 yyyymmddHHMMSS+UUU
表示的,其中
+UUU
或
-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'}
然后,您可以基于第一列进行过滤。
tasklist
不能实现此功能,但wmic
似乎可以。请参见编辑后的答案。 - Luis Mendo