How to delete a file in Win XP

M

MAwiniarski

Greetings,

How to delete a file in Win XP ?

code:

File.chmod(0644, file)
File.delete(file)

does not working.

THX
 
D

Dave Bass

Mateusz said:
How to delete a file in Win XP ?

In what way doesn't it work? What error message are you getting?

I've had a problem with deleting opened files on WinXP (using
File.open). They're supposed to be closed automatically but that doesn't
necessarily happen immediately, but when Ruby or the OS gets around to
it. The answer is to close the file explicitly.

Another thing to look at is IO.sync.

Does that help at all?

Dave
 
P

phlip

MAwiniarski said:
Greetings,

How to delete a file in Win XP ?

code:

File.chmod(0644, file)
File.delete(file)

does not working.

unlink ?

like the File documentation says?
 
R

Roberto Casadei

Mateusz said:
Greetings,

How to delete a file in Win XP ?

code:

File.chmod(0644, file)
File.delete(file)

does not working.

THX

That works to me.

In both function calls you should get the number of processed files or
an exception..
The 'file' var must be a file path, not a File object.
 
S

Serg Koren

When all else fails, try using the full path to the file. You're
probably in the wrong directory.
This works for me.
 
M

MAwiniarski

1. I'm in a proper directory (using: puts Dir.entries(Dir.pwd)
2. File do not need to be closed, because it haven't been open (it's
just file in a folder)
3. Error message:
Errno::EACCES: Permission denied - file
4. I've used chmod in previous line:
File.chmod(0644, file)
chmod return flag: 1 (one)
5. Using File.delete or File.unlink gives same error message
 
L

Luis Lavena

1. I'm in a proper directory (using: puts Dir.entries(Dir.pwd)
2. File do not need to be closed, because it haven't been open (it's
just file in a folder)
3. Error message:
   Errno::EACCES: Permission denied - file
4. I've used chmod in previous line:
   File.chmod(0644, file)
   chmod return flag: 1 (one)
5. Using File.delete or File.unlink gives same error message

File.chmod has no effect on Windows.

Can you ensure that, from the command line, safely remove that file?
Even if not you the one that openned the file, maybe other part of the
OS are locking it so you can't perform the delete operation safely.

HTH,
 
P

Pablo Q.

[Note: parts of this message were removed to make it a legal post.]

Another solution could be use a system call:


`DEL #{file}`
or
system("DEL #{file}")

it returns true or false.
 
A

Axel

- Maybe, the "rights" of your ruby application are not set
appropriate. For example, if you have different user accounts on your
machine (usually, the users have restricted rights), they can only
delete files in certain directories. This is only a simple example,
there are more difficult possibilies.

- Can you delete the files using the Windows Explorer? (Starting it
from within the same user account as you run your ruby application.

Axel
 
M

MAwiniarski

using: system('del ' + filename)

output is: .The process cannot access the file because it is being
used by another process.

How to unlink file from all processes ??
 
L

Luis Lavena

using: system('del ' + filename)

output is: .The process cannot access the file because it is being
used by another process.

How to unlink file from all processes ??

Short answer: you can.

Windows don't let you remove files that remain open or captured by
other process. There is also no easy way to access the unlocking API
to release those file handlers.

May I ask what are you trying to do?

What you're expecting that ruby accomplish you can't accomplish even
using Windows Explorer, both will show the same error since this is
happening on a lower level in the OS.

If you desperately need to release that file handler to remove the
file (dunno the reason, use at your own risk), look for Unlocker:

http://ccollomb.free.fr/unlocker/

HTH,
 
M

MAwiniarski

Short answer: you can.

Windows don't let you remove files that remain open or captured by
other process. There is also no easy way to access the unlocking API
to release those file handlers.
I'm trying to delete normal file which I CAN delete with windows
explorer.
And file is not opened by another application. It is simple 0 bytes
file
created earlier as a mock file for application development testing.
 
L

Luis Lavena

I'm trying to delete normal file which I CAN delete with windows
explorer.
And file is not opened by another application. It is simple 0 bytes
file
created earlier as a mock file for application development testing.

Ok, you're trying to delete the file from the same process?

If the file was created with this syntax:

f = File.open('foo', 'w')

then you need to close it (f.close)

sometimes that doesn't work, so better if you do the file operations
in a block:

File.open('foo', 'w') do |f|
f.puts "xxx"
end

That will release the file description and let you unlink it.

If, by contrary, you fold the file descriptor, nothing can be done
(use Unlocker to see who is holding the file).

HTH,
 
S

Szymon Drejewicz

Mateusz said:
deleting in windows explorer works fine in the same account

Yes, it is true. However, if you run your script and forget to close the
file, the unlink/delete/rm fails!

In Windows Explorer deleting always succeeds because your script is --
probably -- not active. (So, all of your files are closed by Ruby
engine, even if you doesn't closed them explicite.)

You cannot delete a file that is already open. You have to close it
before deletion.

WRONG:
f = File.open('jadzia','w+')
File.unlink('jadzia') =====> ERROR

RIGHT:
f = File.open('jadzia','w+')
f.close
File.unlink('jadzia') =====> OK!
 
F

Fabian Streitel

[Note: parts of this message were removed to make it a legal post.]
WRONG:
f = File.open('jadzia','w+')
File.unlink('jadzia') =====> ERROR

RIGHT:
f = File.open('jadzia','w+')
f.close
File.unlink('jadzia') =====> OK!

EVEN BETTER:

open('jadzia', 'w+') { |file|
# write to the file
} # here ruby auto-closes the file

File.unlink('jadzia') =====> OK!

I like the blockform better, since it prevents me from forgetting
to call close, which i do almost all the time... :)

Greetz!
 
G

Greg Halsey

How to delete a file in Win XP ?#another method:
yourtarget="C:/Documents and Settings/greg/My Documents/targetfile.pdf"
require 'win32ole'
WIN32OLE.new("Scripting.FileSystemObject").deleteFile(yourtarget)

# I didn't address file lock.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top