Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to correctly run a command with pipe?

2,458 views
Skip to first unread message

Why Tea

unread,
Jun 30, 2009, 9:01:26 PM6/30/09
to
I tried the following and it failed to return the
expected result:

set ws = WScript.CreateObject("WScript.Shell")
cmd = "schtasks /Query | findstr /i My_Task"
retcode = ws.run(cmd,0,True)

I expect retcode = 0 when My_Task exists. The
same command actually worked when I ran it in
cmd.exe. Perhaps there is some oddity with
vbscript's handling of the pipe?

Would appreciate any suggestion.

/Why Tea

T Lavedas

unread,
Jul 1, 2009, 7:57:36 PM7/1/09
to

Pipes are supported by the command processor, so it needs to be
invokes ...

retcode = ws.run("%comspec% /c " & cmd,0,True)

Tom Lavedas

Why Tea

unread,
Jul 1, 2009, 9:10:24 PM7/1/09
to

Thanks Tom. It works perfectly now.

Al Dunbar

unread,
Jul 17, 2009, 11:11:35 PM7/17/09
to

"Why Tea" <ytl...@gmail.com> wrote in message
news:dd7c6bab-e250-4cc1...@b14g2000yqd.googlegroups.com...

> I tried the following and it failed to return the
> expected result:
>
> set ws = WScript.CreateObject("WScript.Shell")
> cmd = "schtasks /Query | findstr /i My_Task"
> retcode = ws.run(cmd,0,True)
>
> I expect retcode = 0 when My_Task exists. The
> same command actually worked when I ran it in
> cmd.exe. Perhaps there is some oddity with
> vbscript's handling of the pipe?

Close.

As far as VBScript is concerned, the pipe character is just a text character
in a string being passed to the .Run method. The .Run method then gets the
system to execute the SCHTASKS.EXE program, which also knows nothing about
pipes.

If you want to execute a command that works when run with cmd.exe, you
generally have to run cmd.exe with something like:

> set ws = WScript.CreateObject("WScript.Shell")

> cmd = "cmd.exe /c schtasks.exe /Query | findstr /i My_Task"
> retcode = ws.run(cmd,0,True)

Alternately, it can be simpler to get the correct syntax by .Running a batch
file created to do precisely what you want.

/Al

0 new messages