Solved

delete files that are either in use

Posted on 2004-10-17
8
207 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
 
LVL 18

Expert Comment

by:kandura
ID: 12334263
you should try one of those insidious Office applications.......
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now