From: Chris Morris Date: 2003-09-03T21:56:34+09:00 Subject: Re: [windows 9*] how to get the output of commands which contain backslashes? I've got the following routines in a win.rb file that I always require when I need to do shell stuffs, and I've yet to have any problems. The excerpt is part of a collection of misc. stuffs you can get here: http://www.clabs.org/dl/clutil/. Works with 1.6/1.8. (I thought I'd read somewhere that these substitute routines wouldn't be needed anymore in 1.8, but a brief attempt without them failed, so I just stuck to it -- it's habitual now for me to install this on each box I've got Ruby on and I always require it). # MENON Jean-Francois [Jean-Francois.MENON@meteo.fr] # http://ruby-talk.com/41583 # But I see two potential problems that make not consistent with the # original ruby command: # 1) it takes only one argument # code by Hee-Sob Park - posted here: # http://ruby-talk.com/10006 def system(command) # http://msdn.microsoft.com/library/en-us/vccore98/html/_crt_system.2c_._wsystem.asp Win32API.new("crtdll", "system", ['P'], 'L').Call(command) end # MENON Jean-Francois [Jean-Francois.MENON@meteo.fr] # http://ruby-talk.com/41583 # But I see two potential problems that make not consistent with the # original ruby command: # 1) it always set "$?" variable to false # code by Hee-Sob Park - posted here: # http://ruby-talk.com/10006 def `(command) # http://msdn.microsoft.com/library/en-us/vccore98/HTML/_crt__popen.2c_._wpopen.asp popen = Win32API.new("crtdll", "_popen", ['P','P'], 'L') pclose = Win32API.new("crtdll", "_pclose", ['L'], 'L') fread = Win32API.new("crtdll", "fread", ['P','L','L','L'], 'L') feof = Win32API.new("crtdll", "feof", ['L'], 'L') saved_stdout = $stdout.clone psBuffer = " " * 128 rBuffer = "" f = popen.Call(command,"r") while feof.Call( f )==0 l = fread.Call( psBuffer,1,128,f ) rBuffer += psBuffer[0..l] end pclose.Call f $stdout.reopen(saved_stdout) rBuffer end -- Chris http://clabs.org/blogki