Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


delete files that are either in use

Posted on 2004-10-17
Medium Priority
Last Modified: 2010-05-18
How can i delete a folder or file thats is in use on a windows PC using perl?  
Question by:blnukem
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 18

Expert Comment

ID: 12332240
You can't if they're locked. Otherwise "unlink $file" should do.
LVL 16

Expert Comment

ID: 12333824
You can also do:

system("del $file");

LVL 48

Expert Comment

ID: 12334145
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 18

Expert Comment

ID: 12334263
you should try one of those insidious Office applications.......
LVL 48

Expert Comment

ID: 12334283

Ok, now gets a permission denied.

Mind you, it begs the question, why would you want to delete an open file?

Accepted Solution

sstouk earned 2000 total points
ID: 12337941
If you run the script on the File Server where the files are located and the file is opened by a remote PC, mapping a drive to shared files, then you can simply stop the Server Service on the Server (which would disconnect all the connected clients and unlock the files) and then delete the files.
Files are protected by the operating system when they are locked - you cannot delete them.

If the files are located on Windows NT (Not 2000 or 2003 or XP), then to delete the file you only need to map a directory where the file is with the administrative mapping to a root share and the file could be deleted.
e.g. map to the \\SERVER\c$ then go deeper into the tree to the location where the file is, rename the file first and delete  the renamed file.
Windows NT as compared to Windows 2000 and XP has a bug which is not fixed that will allow you to do it.

On Other NT platforms - only my first suggestion will work.
You will not be able to delete the files locked by the local application. Only by remote.

So, if you need to do it (and I understand the reasons why you might want to do it, e.g. for a clean backup purpose when users forget to close opened files and you want to delete temporaries that are created in same directory, or when network disconnection occured and temporary files are not deleted by the original application, so they appear to be locked by the local subsystem and similar reasons), you might want to design a business requirement and IT process to  open files using mapped drives, and not locally located on the PC.

This way, you can disconnect the drives by stopping Server service on the File Server and unlock the files, and then delete them.


Expert Comment

ID: 12337974
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.

Expert Comment

ID: 12338014
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.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question