Search code examples
windowsbatch-filesubstringfindstrtasklist

Cant get tasklist batch file to output substring


Not really a batch file user (more of a python guy here) wanting to get an output to just show the video title in a MS Edge YouTube tab (ultimately into a txt file, but I am going one step at a time and don't wanna waste too much of peoples time asking things on here. But I hit like 5 hours of debugging and searching how substrings work)

The batch I have made for testing is

@ECHO OFF
SET LIST=tasklist /fi "imagename eq msedge.exe" /fo list /v 
SET TITLE=%LIST%|findstr YouTube
SET OUTPUT=%TITLE:~14,-39%
ECHO %OUTPUT%

gives

~14,-39%


For a reference example

tasklist /fi "imagename eq msedge.exe" /fo list /v | findstr YouTube

in cmd the prompt gives me

Window Title: Skyline Drive & The Polaris Airship - Sunrise Over Sterling [Silk Music] - YouTube - Personal - Microsoft? Edge

But if someone knows how to grab the Windows 10 music toast notification area and plop it into a text file instead of having to use the WINDOWTITLE part of tasklist, that would be amazing. Even in like Node.js, Python, or really anything low intensity I can launch in Win 10.


Solution

  • In batch set variable=command doesn't work. It assigns the literal string to the variable, not the output of the command. To do that, you need a for /f loop:

    for /f "delims=" %%a in ('tasklist /fi "imagename eq msedge.exe" /fo list /v ^| findstr YouTube') do set "title=%%a"
    echo %title:~14,-39% >> /Desktop/title.txt
    

    Usually, you can define which part of the output you return with proper tokens and delims (see for /?), but in this case, it's better to get the whole line and post-process it, as the tokens differ in different languages (English: Window title (two tokens), German: Fenstertitel (one token))