[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie - open file permission denied

Bob Smith

12/24/2008 3:38:00 PM

I am attempting to open a directory, open each file and send each line
of the file to a network host.
I can get the code to work for a single file, but the script fails with
a "permission denied" error when I attempt to do this for each file in a
directory.

*****CODE*****
IPADDR= "some ip address"
OUTPORT = "some ip port"
INDIR = "some directory"
dir = Dir.open(INDIR)
dir.each do |f|
file = File.open(f,"r")
streamSock = TCPSocket::new(IPADDR,OUTPORT)
while(line = file.gets)
streamSock.send(line,0)
end
streamSock.close
file.close
end
*****END CODE******
any help greatly appreciated

Thank you.
--
Posted via http://www.ruby-....

3 Answers

Tim Hunter

12/24/2008 3:54:00 PM

0

Bob Smith wrote:
> I am attempting to open a directory, open each file and send each line
> of the file to a network host.
> I can get the code to work for a single file, but the script fails with
> a "permission denied" error when I attempt to do this for each file in a
> directory.
>
> *****CODE*****
> IPADDR= "some ip address"
> OUTPORT = "some ip port"
> INDIR = "some directory"
> dir = Dir.open(INDIR)
> dir.each do |f|
> file = File.open(f,"r")
> streamSock = TCPSocket::new(IPADDR,OUTPORT)
> while(line = file.gets)
> streamSock.send(line,0)
> end
> streamSock.close
> file.close
> end
> *****END CODE******
> any help greatly appreciated
>
> Thank you.

The filenames returned by dir.each are just filenames, not the complete
paths. You may need to prepend the INDIR directory name to the filenames
to get a complete path to open.


--
RMagick: http://rmagick.ruby...

brabuhr

12/24/2008 4:16:00 PM

0

On Wed, Dec 24, 2008 at 10:53 AM, Tim Hunter <TimHunter@nc.rr.com> wrote:
> Bob Smith wrote:
>>
>> I am attempting to open a directory, open each file and send each line
>> of the file to a network host...
>>
>> dir = Dir.open(INDIR)
>> dir.each do |f|
>> file = File.open(f,"r")
>
> The filenames returned by dir.each are just filenames, not the complete
> paths. You may need to prepend the INDIR directory name to the filenames to
> get a complete path to open.

Another possible option might be something like:

Dir.glob("#{INDIR}/*").each do |filename|
File.open(filename, "r") do |file|
#...
end
end

glob does return the full path to the file.

Or, possibly something like this could work:

Dir.chdir(INDIR) do
Dir.foreach(".") do |filename|
#...
end
end

Bob Smith

12/24/2008 4:27:00 PM

0

unknown wrote:
>
> Dir.glob("#{INDIR}/*").each do |filename|
> File.open(filename, "r") do |file|
> #...
> end
> end
>
> glob does return the full path to the file.

Thank you, the Dir.glob gets rid of the error.
--
Posted via http://www.ruby-....