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: 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:

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

On Nov 23, 12:08am, Fengfeng Li [email protected] wrote:

: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.

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:)

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

Luis L. wrote in post #963322:

On Nov 23, 12:08am, Fengfeng Li [email protected] wrote:

: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 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…

On Wed, Nov 24, 2010 at 9:54 AM, Arturo G.
[email protected] wrote:

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

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


Phillip G.

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.

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:)

Arturo G. 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()?

Charles R. wrote in post #963646:

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

MS-DOS and Windows Command Line Cmd Command
MS-DOS and Windows Command Line 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^^

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 Thu, Nov 25, 2010 at 3:13 AM, Fengfeng Li [email protected]
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:


Phillip G.

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.

On 25 November 2010 09:45, Phillip G.
[email protected] wrote:

On Thu, Nov 25, 2010 at 3:13 AM, Fengfeng Li [email protected] 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:

Yes, I’d definitely go with that.

Charles

“Arturo G.” [email protected] schrieb im Newsbeitrag
news:[email protected]

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”.