Link to home
Start Free TrialLog in
Avatar of blnukem
blnukem

asked on

delete files that are either in use

How can i delete a folder or file thats is in use on a windows PC using perl?  
Avatar of kandura
kandura

You can't if they're locked. Otherwise "unlink $file" should do.
You can also do:

system("del $file");

Avatar of Tintin
unlink should be fine.

I did a test under XP/ActiveState Perl

I created a file with notepad and left notepad running, then just did a

unlink "foobar.txt" or die '$!\n";

and the file was successfully deleted.
you should try one of those insidious Office applications.......
Hmm.

Ok, now gets a permission denied.

Mind you, it begs the question, why would you want to delete an open file?
ASKER CERTIFIED SOLUTION
Avatar of Sergy Stouk
Sergy Stouk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to delete locked dll files, then there is a way to mark the files for deletion upon the next reboot and delete them, before OS reads them into memory.
This is what Antivirus and Spy Removers do if they find locked files that are in memory, they ask you the permission to delete the files upon the restart, mark them to be deleted and the OS deletes them before the next startup.
The general strategy is - stop the application or service, that opened that locked this file, and then delete temporary file.

I all depends on what files are want to delete.

If it is still MS Office files and it is still on the Local PC - kill all office application forcefully (if you are not afraid of possible corruption of the opened documents).

SysInternals has ps toolkit which includes the pckill.exe application, that would allow you to kill applications locally and on remote PCs.