How to delete a file in Win XP

Greetings,

How to delete a file in Win XP ?

code:

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

does not working.

THX

Mateusz Winiarski wrote:

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

Mateusz Winiarski wrote:

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.

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

  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

MAwiniarski wrote:

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?

Another solution could be use a system call:

DEL #{file}
or
system(“DEL #{file}”)

it returns true or false.

2008/7/9 Luis L. [email protected]:

On Jul 9, 8:12 pm, MAwiniarski [email protected] wrote:

  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,

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 ??

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/

Or, perhabs unlocker shows, which process/application locks the
file. Maybe, even this helps.

If unlocker does not do the job, you can try mst IsUsedBy:

But I never used it.

  • Axel

On Jul 10, 7:51 am, MAwiniarski [email protected] wrote:

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,

On Jul 10, 12:11 pm, MAwiniarski [email protected] wrote:

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.

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,

On Jul 10, 9:39 am, Luis L. [email protected] wrote:

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.

deleting in windows explorer works fine in the same account

Mateusz Winiarski wrote:

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!

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… :slight_smile:

Greetz!

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.

maybe the file name contains “” instead of “/” ?