Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Files From Directory 3

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
What is the most efficent and simplest way to delete files from a directory. Control whats deleted by a text file that list files that can be deleted.
 
I would think the unlink is the most efficient command;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
is there a simple comand to unlink specific files or reference everything in a text file?
 
No error checking in this code...
Code:
open FH, "list_of_files.txt";
unlink <FH>;
close FH;

unlink expects a list, and <FH> in a list context returns an array, 1 element per line. In this case, list_of_files.txt would contain 1 filename to delete per line.
 
It seems rather odd that you have to open a file to delete it. That would offer the possibility to delete a file that is in use. That would cdrtainly conflict on a windows system. In fact Larry Wall warns Programming Perl p.625
Don't unlink or rename an open file

Code:
unlink <*.txt>;
will do.



_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
rvBasic,

I think you missed something. unlink(), like some perl functions, expects a list. In this case the OP has a file with a list of filenames to delete in it. The file is opened and the unlink() function reads all the lines of the file, potentially deleting the corresponding file in each line of the file. It's not unlinking the file that is open for reading.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
yes Kevin, I missed that. Thanks for clarifying.

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
In my list, do i need the full path to the file if i use it this way?

open FH, "list_of_files.txt";
unlink <FH>;
close FH;
 
If the files are in a different directory than the working directory, then you will need to include the path somehow (either relative or full).

If all the files have a common root, then you could put that into the script.

Code:
open FH, "list_of_files.txt";
unlink map { "/usr/home/brigmar/" . $_ } <FH>;
close FH;

In this case, all the files in list_of_files.txt MUST be in the /usr/home/brigmar directory or below.
 
if it's windows, i can simple change that to "C:\test\" , correct?
 
No, change it to:

'c:/test/'

windows (but not DOS) is perfectly happy with forward slashes in path statements. Perl on the other hand sees backslashes as either an effort to escape what follows it or as a meta character, such as \t, which in a double-quoted string is a tab.

Even if you use:

'c:\test\'

it will not work because the trailing backslash needs to be escaped:

'c:\test\\'

otherwise perl thinks you are trying to escape the ' which is the only character that needs to be escaped in a single-quoted string.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
heh... and the funny thing is, that despite my example, I perl almost exclusively on Windows w/ Activestate.
 
I tried the following and it still not deleting the file i have listed in the my deletelist.

#!/usr/bin/perl

open FH, "deletelist.dat";
unlink map { "C:/Temp/" . $_ } <FH>;
close FH;
 
What are the contents of deletelist.dat?
What are the contents of c:\temp\? (output of DIR command)
 
try this:

Code:
open FH, "deletelist.dat";
unlink map {[b]chomp[/b]; "C:/Temp/$_" } <FH>;
close FH;



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You can add in some error checking too:

Code:
open FH, "deletelist.dat";
while (<FH>) {
   chomp;
   print  unlink "C:/Temp/$_" ? "File '$_' deleted\n" : File '$_' not deleted: $!\n";
}
close FH;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top