Question about FILE::delete

Hi there,

I want to delete file(s) and to catch the error if the file(s) don’t exist.
For that I can write the following code:

pattern = "c:/WinNt/*"
num = File.delete(pattern)
if (0 == num) then
    printf("%s: could not delete file(s) %s\n", ARGV[0], pattern)
end

The problem is that there is a runtime error, if there are no valid files.
In the online documentation I can’t find a hint about the File::delete()
method throws an exception or not. If not how can I find out, if the files
given by “pattern” do exist ?

Thanks in advance,

Daniel.

    pattern = "c:/WinNt/*"
    num = File.delete(pattern)

File::delete don't take a pattern but just a filename

pigeon% ls
a b c
pigeon%

pigeon% ruby -e 'File.delete("*")'
-e:1:in `delete': No such file or directory - "*" (Errno::ENOENT)
        from -e:1
pigeon%

Now to catch the error

pigeon% ruby -e 'File.delete("*") rescue p "ERROR #$!"'
"ERROR No such file or directory - \"*\""
pigeon%

and if you want to delete all files

pigeon% ruby -e 'Dir["*"].each {|f| File.delete(f) }'
pigeon%

pigeon% ls
pigeon%

Just protect File::delete in the block {} if you want catch the error

Guy Decoux

Hello,

File.delete(filename) if FileTest.exists?(filename);

hth,
Alia

Daniel Schnell wrote:

···

Hi there,

I want to delete file(s) and to catch the error if the file(s) don’t exist.
For that I can write the following code:

pattern = “c:/WinNt/*”
num = File.delete(pattern)
if (0 == num) then
printf(“%s: could not delete file(s) %s\n”, ARGV[0], pattern)
end

The problem is that there is a runtime error, if there are no valid files.
In the online documentation I can’t find a hint about the File::delete()
method throws an exception or not. If not how can I find out, if the files
given by “pattern” do exist ?

Thanks in advance,

Daniel.