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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read File list and Delete 2

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
I am new to perl and need to create a script that can do the following.

I have a text file(C:\temp\filelist_out.txt) that has a listing of file names. I need to compare the filelist_out to C:\temp and any file that isn't on the filelist_out that's in the dir, delete it from c:\temp.
 
One way of many:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Basename[/green] [red]qw([/red][purple]basename[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]Tie::File[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$datafile[/blue] = [red]'[/red][purple]C:/temp/filelist_out.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$datadir[/blue] = [red]'[/red][purple]C:/temp[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/tie.html][black][b]tie[/b][/black][/url] [black][b]my[/b][/black] [blue]@array[/blue], [red]'[/red][purple]Tie::File[/purple][red]'[/red], [blue]$datafile[/blue] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$datafile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]%files[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][maroon]basename[/maroon][red]([/red][blue]$_[/blue][red])[/red] => [fuchsia]1[/fuchsia][red]}[/red] [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red]! [red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red]}[/red] [blue]@array[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/untie.html][black][b]untie[/b][/black][/url] [blue]@array[/blue][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][black][b]grep[/b][/black] [red]{[/red]![blue]$files[/blue][red]{[/red][maroon]basename[/maroon][red]([/red][blue]$_[/blue][red])[/red][red]}[/red][red]}[/red] [url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]"[/red][purple][blue]$datadir[/blue]/*[/purple][red]"[/red][red])[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url] or [black][b]die[/b][/black] [red]"[/red][purple]Can't unlink [blue]$_[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Basename - Parse file paths into directory, filename and suffix.[/li]
[li]Tie::File - Access the lines of a disk file via a Perl array[/li]
[/ul]
[/tt]

Just note that hopefully your datafile links to itself, otherwise you may be slightly annoyed the first time you run the script.

- Miller
 
I get the following error. I take it i have to install a Tie/File Module... how do i do that ?

ERROR:
Can't locate Tie/File.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .)
 
If you are using activestate start the ppm and you can install it from there.
 
Well, technically speaking, Tie::File is not really needed. I simply chose to use it because it's a good module to be familiar with and it automatically takes care of the return character after every line.

Anyway, here's a version that just uses simple file processing. As I said, there are many ways to do this:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Basename[/green] [red]qw([/red][purple]basename[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$datafile[/blue] = [red]'[/red][purple]C:/temp/filelist_out.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$datadir[/blue] = [red]'[/red][purple]C:/temp[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE, [blue]$datafile[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$datafile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]%files[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red] [maroon]basename[/maroon][red]([/red][blue]$_[/blue][red])[/red] => [fuchsia]1[/fuchsia][red]}[/red] [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red]! [red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red]}[/red] <FILE>[red];[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]FILE[red])[/red][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][black][b]grep[/b][/black] [red]{[/red]![blue]$files[/blue][red]{[/red][maroon]basename[/maroon][red]([/red][blue]$_[/blue][red])[/red][red]}[/red][red]}[/red] [url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]"[/red][purple][blue]$datadir[/blue]/*[/purple][red]"[/red][red])[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url] or [black][b]die[/b][/black] [red]"[/red][purple]Can't unlink [blue]$_[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Basename - Parse file paths into directory, filename and suffix.[/li]
[/ul]
[/tt]

And here's a version that would use File::Slurp. This is actually how I personally would do it.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Basename[/green] [red]qw([/red][purple]basename[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Slurp[/green] [red]qw([/red][purple]read_file[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$datafile[/blue] = [red]'[/red][purple]C:/temp/filelist_out.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$datadir[/blue] = [red]'[/red][purple]C:/temp[/purple][red]'[/red][red];[/red]

[black][b]my[/b][/black] [blue]%files[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red] [maroon]basename[/maroon][red]([/red][blue]$_[/blue][red])[/red] => [fuchsia]1[/fuchsia][red]}[/red] [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red]! [red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red]}[/red] [maroon]read_file[/maroon][red]([/red][blue]$data_file[/blue][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][black][b]grep[/b][/black] [red]{[/red]![blue]$files[/blue][red]{[/red][maroon]basename[/maroon][red]([/red][blue]$_[/blue][red])[/red][red]}[/red][red]}[/red] [url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]"[/red][purple][blue]$datadir[/blue]/*[/purple][red]"[/red][red])[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't unlink [blue]$_[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Basename - Parse file paths into directory, filename and suffix.[/li]
[/ul]
Other Modules used :
[ul]
[li]File::Slurp[/li]
[/ul]
[/tt]

- Miller
 
Is there a way I can write to a log all the files that is in the directory that was deleted?
 
Something like:

open(OUTFILE, ">/path/to/outfile") or die "can't open outfile : $!\n";
for (grep {!$files{basename($_)}} glob("$datadir/*")) {
unlink or die "Can't unlink $_: $!";
print OUTFILE "$_\n";
}

close OUTFILE;



 
I hope that filelist_out.txt contains an entry for filelist_out.txt, or you are only going to be able to test this once...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hehe.. yeah .. make sure datafile doens't exist as part of datadir :)
 
It does supposedly exist in the same directory. But I stated that concern in my original post:

MillerH said:
Just note that hopefully your datafile links to itself, otherwise you may be slightly annoyed the first time you run the script.

- Miller
 
That is not an issue, they are in different dirs. One more thing how can i chage the script to not delete, just write to the log all the once that are not in the dir?
 
nevermind... i commented out the unlink line...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top