• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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?  
0
blnukem
Asked:
blnukem
  • 3
  • 2
  • 2
  • +1
1 Solution
 
kanduraCommented:
You can't if they're locked. Otherwise "unlink $file" should do.
0
 
xDamoxCommented:
You can also do:

system("del $file");

0
 
TintinCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
kanduraCommented:
you should try one of those insidious Office applications.......
0
 
TintinCommented:
Hmm.

Ok, now gets a permission denied.

Mind you, it begs the question, why would you want to delete an open file?
0
 
sstoukCommented:
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
 
sstoukCommented:
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
 
sstoukCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now