system() or process.create?

F

Fengfeng Li

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
 
L

Luis Lavena

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:

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

Eleanor McHugh

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
 
F

Fengfeng Li

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} )
 
A

Arturo Garcia

[... ... ...]
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....
 
P

Phillip Gawlowski

Is "del" is an actual executable? =A0It used to be in old DOS but Mr. Win= dows
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.

--=20
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.
 
F

Fengfeng Li

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

Thanks, now it's ok:)
 
F

Fengfeng Li

Arturo Garcia wrote in post #963497:
[... ... ...]
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()?
 
C

Charles Roper

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

http://www.computerhope.com/cmd.htm
http://www.computerhope.com/delhlp.htm

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

Arturo Garcia wrote in post #963497:
[... ... ...]
cmd =3D "del *.zip"
Process.create(

=C2=A0 :app_name =3D> cmd,
=C2=A0 :startup_info=3D> =C2=A0{:sw_flags=3D>Process::SW_HIDE , :startf= _flags =3D>

Process::STARTF_USESHOWWINDOW} =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0)
Is "del" is an actual executable? =C2=A0It 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()?
 
F

Fengfeng Li

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:

http://www.computerhope.com/cmd.htm
http://www.computerhope.com/delhlp.htm

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^^
 
P

Phillip Gawlowski

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. ;)

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

Charles Roper

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. ;)

Yes, I'd definitely go with that.

Charles
 
M

Michael Bruschkewitz

Arturo Garcia said:
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".
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top