Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

cmd - How to extract/display any two columns and their values from command output?

I need the TaskName and the Status columns displayed from schtasks command.

I tried the following:

for /f "tokens=1,3 delims= " %i in ('schtasks') DO echo %i

The output is not pretty looking. Current output if I run schtasks :

C:Windowssystem32>schtasks
Folder: MicrosoftWindowsWwanSvc
TaskName                               Next Run Time          Status
NotificationTask                         N/A                    Ready
Folder: MicrosoftXblGameSave
TaskName                                 Next Run Time          Status
XblGameSaveTask                          N/A                    Ready

Sample desired output:

Folder: MicrosoftWindowsWwanSvc
TaskName                                      Status
NotificationTask                               Ready
Folder: MicrosoftXblGameSave
TaskName                                       Status
XblGameSaveTask                                Ready

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Whilst it doesn't provide exactly the same output as your desired example, the following, seems usable, (and better), to me:

schtasks /query /fo list | findstr /r "^Folder: ^TaskName: ^Status: ^$"

Which should provide output like this:

Folder: MicrosoftWindowsWwanSvc
TaskName:      MicrosoftWindowsWwanSvcNotificationTask
Status:        Ready

Folder: MicrosoftXblGameSave
TaskName:      MicrosoftXblGameSaveXblGameSaveTask
Status:        Ready

BTW, I'm unsure what version of Windows you're intending this for, but certainly from Windows Vista, through to Windows 10, this is the output format I'm used to seeing from schtasks:

Folder: MicrosoftWindowsWwanSvc
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
NotificationTask                         N/A                    Ready

Folder: MicrosoftXblGameSave
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
XblGameSaveTask                          N/A                    Ready

Which means that your output, from your provided code, would look like this:

Folder:
TaskName
========================================
NotificationTask
Folder:
TaskName
========================================
XblGameSaveTask

That also means to get the output you desire, you'd need to omit the table header borders, as well as the second table column.


Given that, here's a rather OTT method of potentially achieving that from a :

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "{=                                          "
For /F "Delims=" %%G In ('%SystemRoot%System32schtasks.exe /Query ^
 ^| %SystemRoot%System32findstr.exe /R "^[^=]"') Do (Set "}=%%G"
    SetLocal EnableDelayedExpansion & Set "}=!}:  =|!"
    For /F "Tokens=1,3 Delims=|" %%H In ("!}:| =||!") Do If "%%I" == "" (
        Echo %%H) Else Set "z=%%H%{%" & Echo=!z:~,41!%%I
    EndLocal)
Pause

This should output this for you:

Folder: MicrosoftWindowsWwanSvc
TaskName                                 Status
NotificationTask                         Ready
Folder: MicrosoftXblGameSave
TaskName                                 Status
XblGameSaveTask                          Ready

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...