?
Solved

delete files that are either in use

Posted on 2004-10-17
8
Medium Priority
?
219 Views
Last Modified: 2010-05-18
How can i delete a folder or file thats is in use on a windows PC using perl?  
0
Comment
Question by:blnukem
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Expert Comment

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

Expert Comment

by:xDamox
ID: 12333824
You can also do:

system("del $file");

0
 
LVL 48

Expert Comment

by:Tintin
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.
0
Independent Software Vendors: 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!

 
LVL 18

Expert Comment

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

Expert Comment

by:Tintin
ID: 12334283
Hmm.

Ok, now gets a permission denied.

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

Accepted Solution

by:
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.

0
 
LVL 6

Expert Comment

by:sstouk
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.
0
 
LVL 6

Expert Comment

by:sstouk
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.

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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
Suggested Courses

840 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