?
Solved

Write TASKLIST output to text file using VBA

Posted on 2011-10-26
9
Medium Priority
?
1,607 Views
Last Modified: 2012-05-12
Hello -
I am trying to execute the TASKLIST command using VBA, writing the output to a csv text file. It isn't working.

If I execute the following from a CMD window, if works:
TASKLIST /V /NH /FO CSV >C:\Temp\TaskList.txt

But if I try the following Sub, nothing happens:

Public Sub GetTaskList()
   Shell "TASKLIST /V /NH /FO CSV >C:\Temp\TaskList.txt"
   On Error GoTo 0
End Sub

What am I doing wrong? Can I even do this?
I am on an XP platform running Access 2007

0
Comment
Question by:shambalad
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 2

Expert Comment

by:jdc1944
ID: 37031164
i would save it as a batch file and then just call that file as...

 Shell "C:\MyFile.bat", vbNormalFocus
0
 
LVL 2

Expert Comment

by:jdc1944
ID: 37031189
just to be clear, you only need to add 'TASKLIST /V /NH /FO CSV >C:\Temp\TaskList.txt' to the batch file
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37031195
try it like this perhaps:

Dim varApp As Variant
varApp = Shell("cmd /c""TASKLIST /V /NH /FO CSV >C:\Temp\TaskList.txt", vbHide)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 2000 total points
ID: 37031228
...because this works fine for me as a test...

Dim varApp As Variant
varApp = Shell("cmd /c""Dir c: >c:\Dirlist.txt", vbHide)
0
 
LVL 12

Expert Comment

by:danishani
ID: 37031361
This function below in this thread does the trick:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=971

Simply use:
CommandLine ("TASKLIST /V /NH /FO CSV >C:\Temp\TaskList.txt")

Hope this helps,
Daniel
0
 
LVL 29

Expert Comment

by:Badotz
ID: 37031382
Right, the batch file seems to be the way to go. Put this:

Call Shell(Environ$("COMSPEC") & " /c  c:\listtask.bat", vbHide)

into file C:\ListTask.bat

Then you can use this to do the deed. I got the idea from here:

Call Shell(Environ$("COMSPEC") & " /c  c:\ListTask.bat", vbHide)

Environ$("COMSPEC") returns the path to Command.com on the machine and the "/c" argument makes sure that the Dos window is automatically closed when the batch file finishes executing.
0
 
LVL 7

Author Closing Comment

by:shambalad
ID: 37032130
Since I really do not want to create a file separate from the accdb to run this, Boag2000's suggestion works best for me.
i.e.:
Dim varApp As Variant
varApp = Shell("cmd /c""Dir c: >c:\Dirlist.txt", vbHide)

Thanks to all for you input.
Todd
0
 
LVL 7

Author Comment

by:shambalad
ID: 37032157
OK, the way I worded that sounds a little off. I don't mind creating the output file. Actually, I'm going to create it in the user's temp folder and delete it right after I'm done with it. I just didn't want to work with a .bat file for this one.
Thanks again,
Todd
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37033104
You could probably "Tighten Up" my code.

I just always used that syntax....
;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question