How to not display output of a system call.

J

Jerry Mr

Lets say I have a Windows command line program that runs the following:

system("del *.txt")

How do I NOT display the output from the del command?

Thank you.
 
R

Robert Klemme

Lets say I have a Windows command line program that runs the following:

system("del *.txt")

How do I NOT display the output from the del command?

Thank you.

In this case:

Dir["*.txt"].each {|f| File.delete f}

:)

robert
 
T

Tim Hunter

Jerry said:
Lets say I have a Windows command line program that runs the following:

system("del *.txt")

How do I NOT display the output from the del command?

Thank you.

`del *.txt`

That's backticks, not apostrophes. Also, this won't prevent the display
of output to stderr, only stdout.
 
T

Tim Hunter

Jerry said:
Also, how do you make the program pause for a few seconds.

$ ri Kernel#sleep
----------------------------------------------------------- Kernel#sleep
sleep([duration]) => fixnum

From Ruby 1.9.1
------------------------------------------------------------------------
Suspends the current thread for duration seconds (which may be any
number, including a Float with fractional seconds). Returns the
actual number of seconds slept (rounded), which may be less than
that asked for if another thread calls Thread#run. Zero arguments
causes sleep to sleep forever.

Time.new #=> 2008-03-08 19:56:19 +0900
sleep 1.2 #=> 1
Time.new #=> 2008-03-08 19:56:20 +0900
sleep 1.9 #=> 2
Time.new #=> 2008-03-08 19:56:22 +0900
 
J

Jerry Piazza

Tim said:
`del *.txt`

That's backticks, not apostrophes. Also, this won't prevent the display
of output to stderr, only stdout.

Hrmm, backticks don't seem to work on windows boxes.

Tries to point to a CMD variable instead.


@Robert Klemme

I agree that doing it entirely in ruby would be the better way to go
about it.
Just sometimes it is quicker to use a command that is already available
(or run a utility I added to %path%) to speed things up at work.

Plus, now that I am wondering, it will haunt me until I find the answer.
 
J

Jerry Piazza

I am now trying to impliment this.

I created this:
def spinner spin_trigger
while (spin_trigger == true)
print "\\\r"
sleep 1
print "|\r"
sleep 1
print "/\r"
sleep 1
print "-\r"
sleep 1
end
end

spin = true
Thread.new do
spinner spin
end
files = Dir.glob("c:/**/*.txt")
spin = false

But all I get is a "\" stuck at the beginning of the line until the Dir
command finishes.

Maybe I am confused about the usage of Thread?
 
7

7stud --

Jerry said:
I am now trying to impliment this.

I created this:
def spinner spin_trigger
while (spin_trigger == true)
print "\\\r"
sleep 1
print "|\r"
sleep 1
print "/\r"
sleep 1
print "-\r"
sleep 1
end
end

spin = true
Thread.new do
spinner spin
end
files = Dir.glob("c:/**/*.txt")
spin = false

But all I get is a "\" stuck at the beginning of the line until the Dir
command finishes.

Maybe I am confused about the usage of Thread?

I think you have some more basic issues to deal with first. What do you
think the output of the following code will be:

def test(x)
while x == 20
sleep 3
puts x
end
end

x = 10
test(20)
x = 30
 
R

Robert Klemme

Comparing with "true" or "false" to obtain a boolean value is a very bad
idea - especially in Ruby which has two false values and unlimited true
values.
I think you have some more basic issues to deal with first. What do you
think the output of the following code will be:

def test(x)
while x == 20
sleep 3
puts x
end
end

x = 10
test(20)
x = 30

Absolutely!

robert
 
B

Brian Candler

Jerry said:
Lets say I have a Windows command line program that runs the following:

system("del *.txt")

How do I NOT display the output from the del command?

You could try something like this:

system("del *.txt >NUL")

Otherwise try IO.popen, which can capture the output of the command, so
you can throw it away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top