System() or process.create?

Hi everyone,

I'm a beginer of ruby, I want to know how to call external applications
in ruby, without the cmd window poping up.
I leanrt this link: http://www.ruby-forum.com/topic/213521#new, and
tried all the below ways:
result = `#{cmd}` #I tried this but the window still poped up
result = IO.popen("#{cmd}").read #and the same with this one
Process.create(
  :app_name => cmd,
  :startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW} )
#at last, I use process.create and the black window disappeared.

It worked good when I set cmd as "\"#{winrar_path}\\winrar.exe\" e -y
-o+ -ibck \"#{para[:dst_path]}\\*.zip\" \"#{para[:dst_path]}\\\"", but
when I changed it to "del *.zip", it always said "CreateProcess()
failed: ", as below:

FileUtils.chdir("#{para[:dst_path]}")
cmd = "del *.zip"
Process.create(:app_name => cmd,:startup_info=>
{:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW} )

The other problem is, if I use Process.create to unzip some file
firstly, and then use Process.create to do some other things to the
new-unzip files secondly, the second application sometimes failed, I
think maybe because the first unzip process is still processing? How to
avoid this? Thanks:)

Regards

···

--
Posted via http://www.ruby-forum.com/.

WinRAR will pop up a Windows during extraction.

You need a command line version tool like 7-zip (7za) that works in
the command line and can be hidden or look in WinRAR documentation
about silent command line extraction arguments.

···

On Nov 23, 12:08 am, Fengfeng Li <lifengf...@huawei.com> wrote:

Hi everyone,

I'm a beginer of ruby, I want to know how to call external applications
in ruby, without the cmd window poping up.
I leanrt this link:How to avoid black-windows using system()? - Ruby - Ruby-Forum, and
tried all the below ways:
result = `#{cmd}` #I tried this but the window still poped up
result = IO.popen("#{cmd}").read #and the same with this one
Process.create(
:app_name => cmd,
:startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW} )
#at last, I use process.create and the black window disappeared.

It worked good when I set cmd as "\"#{winrar_path}\\winrar.exe\" e -y
-o+ -ibck \"#{para[:dst_path]}\\*.zip\" \"#{para[:dst_path]}\\\"", but
when I changed it to "del *.zip", it always said "CreateProcess()
failed: ", as below:

--
Luis Lavena

If you want to force several processes to run sequentially then you should issue waitpid calls after launching each one so that they're only launched as the previous process is completed. A more elegant alternative would be to launch a single process which piped the output of each command into the next and let the OS work out the details for you, but that's dependent on the command you're running being pipe friendly.

Ellie

Eleanor McHugh
Games With Brains

···

On 23 Nov 2010, at 03:08, Fengfeng Li wrote:

The other problem is, if I use Process.create to unzip some file
firstly, and then use Process.create to do some other things to the
new-unzip files secondly, the second application sometimes failed, I
think maybe because the first unzip process is still processing? How to
avoid this? Thanks:)

----
raise ArgumentError unless @reality.responds_to? :reason

Luis Lavena wrote in post #963322:

:startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW} )
#at last, I use process.create and the black window disappeared.

It worked good when I set cmd as "\"#{winrar_path}\\winrar.exe\" e -y
-o+ -ibck \"#{para[:dst_path]}\\*.zip\" \"#{para[:dst_path]}\\\"", but
when I changed it to "del *.zip", it always said "CreateProcess()
failed: ", as below:

WinRAR will pop up a Windows during extraction.

You need a command line version tool like 7-zip (7za) that works in
the command line and can be hidden or look in WinRAR documentation
about silent command line extraction arguments.

Thanks for your reply. Maybe I didn't explain my problem clearly. My
problem is the process.create works fine with winrar, but when I change
the cmd as "del *.zip", it says "CreateProcess() failed", I wonder why
the "del *.zip" cann't work with process.create. as below:

cmd = "del *.zip"
Process.create(
  :app_name => cmd,
  :startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW} )

···

On Nov 23, 12:08am, Fengfeng Li <lifengf...@huawei.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

Eleanor McHugh wrote in post #963332:>

If you want to force several processes to run sequentially then you
should issue waitpid calls after launching each one so that they're only
launched as the previous process is completed. A more elegant
alternative would be to launch a single process which piped the output
of each command into the next and let the OS work out the details for
you, but that's dependent on the command you're running being pipe
friendly.

Ellie

Eleanor McHugh
Games With Brains
----
raise ArgumentError unless @reality.responds_to? :reason

Thanks, now it's ok:)

···

--
Posted via http://www.ruby-forum.com/\.

Is "del" is an actual executable? It used to be in old DOS but Mr. Windows
has a history of change....

···

On Wednesday 24 November 2010 08:03:59 Fengfeng Li wrote:

[... ... ...]
cmd = "del *.zip"
Process.create(

  :app_name => cmd,
  :startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>

Process::STARTF_USESHOWWINDOW} )

Easy test: Hast it been documented? Then it exists until the end of
time, Windows, or Microsoft, whichever comes first.

···

On Wed, Nov 24, 2010 at 9:54 AM, Arturo Garcia <arturo.g.arturo@gmail.com> wrote:

Is "del" is an actual executable? It used to be in old DOS but Mr. Windows
has a history of change....

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

Arturo Garcia wrote in post #963497:

···

On Wednesday 24 November 2010 08:03:59 Fengfeng Li wrote:

[... ... ...]
cmd = "del *.zip"
Process.create(

  :app_name => cmd,
  :startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>

Process::STARTF_USESHOWWINDOW} )

Is "del" is an actual executable? It used to be in old DOS but Mr.
Windows
has a history of change....

I use fileUtils.chdir(dir) to change to a dir in which .zip files exist,
then I can use system("del *.zip") to del them, but Process.create()
will report an error. Maybe because fileUtils.chdir(dir) doesn't work
for Process.create()?

--
Posted via http://www.ruby-forum.com/\.

"Arturo Garcia" <arturo.g.arturo@gmail.com> schrieb im Newsbeitrag news:201011240854.17939.arturo.g.arturo@gmail.com...

Is "del" is an actual executable? It used to be in old DOS but Mr. Windows
has a history of change....

I can't remember it being an "external" command.
It's internal, as "cd" is, there is no "del.exe".

Del is part of cmd.exe, which is possibly why Process.create is not
working. Have a look here:

You could try something like Process.create('cmd.exe /c del *.zip')

Alternatively, why not use FileUtils.rm to delete files? Like this:

FileUtils.rm Dir.glob('*.zip')

Docs: http://ruby-doc.org/core/classes/FileUtils.html#M004332

Charles

···

On 24 November 2010 09:52, Fengfeng Li <lifengfeng@huawei.com> wrote:

Arturo Garcia wrote in post #963497:

On Wednesday 24 November 2010 08:03:59 Fengfeng Li wrote:

[... ... ...]
cmd = "del *.zip"
Process.create(

:app_name => cmd,
:startup_info=> {:sw_flags=>Process::SW_HIDE , :startf_flags =>

Process::STARTF_USESHOWWINDOW} )

Is "del" is an actual executable? It used to be in old DOS but Mr.
Windows
has a history of change....

I use fileUtils.chdir(dir) to change to a dir in which .zip files exist,
then I can use system("del *.zip") to del them, but Process.create()
will report an error. Maybe because fileUtils.chdir(dir) doesn't work
for Process.create()?

--
Posted via http://www.ruby-forum.com/\.

Charles Roper wrote in post #963646:

Del is part of cmd.exe, which is possibly why Process.create is not
working. Have a look here:

Cmd Command
Del Command

You could try something like Process.create('cmd.exe /c del *.zip')

Alternatively, why not use FileUtils.rm to delete files? Like this:

FileUtils.rm Dir.glob('*.zip')

Docs: http://ruby-doc.org/core/classes/FileUtils.html#M004332

Charles

Thanks for your help, both 'cmd.exe /c del *.zip' and FileUtils.rm
Dir.glob('*.zip') can solve my problem^^

···

--
Posted via http://www.ruby-forum.com/\.

Personally, I'd go with FileUtils.rm( Dir.glob('*.zip') ), just
because it is more portable, and doesn't incur the cost (negligible,
but still :P) of spinning off a new process. Additionally, I have to
deal only with Ruby's idiosyncrasies. No need to pile cmd.exe's
baggage on top of that. :wink:

···

On Thu, Nov 25, 2010 at 3:13 AM, Fengfeng Li <lifengfeng@huawei.com> wrote:

Thanks for your help, both 'cmd.exe /c del *.zip' and FileUtils.rm
Dir.glob('*.zip') can solve my problem^^

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

Yes, I'd definitely go with that.

Charles

···

On 25 November 2010 09:45, Phillip Gawlowski <cmdjackryan@googlemail.com> wrote:

On Thu, Nov 25, 2010 at 3:13 AM, Fengfeng Li <lifengfeng@huawei.com> wrote:

Thanks for your help, both 'cmd.exe /c del *.zip' and FileUtils.rm
Dir.glob('*.zip') can solve my problem^^

Personally, I'd go with FileUtils.rm( Dir.glob('*.zip') ), just
because it is more portable, and doesn't incur the cost (negligible,
but still :P) of spinning off a new process. Additionally, I have to
deal only with Ruby's idiosyncrasies. No need to pile cmd.exe's
baggage on top of that. :wink: