# Perl script to gather pdf versions of a bibtex database # # Usage: gatherbib.pl -l -r -a source.bib pdfdir # # source.bib defines the bibtex file that lists the PDF files to look for # If this argument is omitted, then all bib files in the local directory # will be scanned. # pdfdir specifies the root directory containing the pdf files. Multiple # directories may be given in the form dir1+;dir2;dir3+ where appending # a + to a directory name means that all subdirectories should be searched # A default directory specification is embedded in this file and should # be edited to match your local system # # Switches: -l enables the log file "gatherbib.log" in the current directory # -r put references in a subdirectory called "refs" # -a puts references in subdirectories given by the # initial letter of the bibtex key # # # NOTE: YOU NEED TO DEFINE THE SYSTEM-DEPENDENT DEFAULT LOCATION FOR PDF FILES BELOW # # To enable right-clicking, you can create an association with a .BIB file called "Gather" having # an action "...\perl\bin\perl.exe" "...\gatherbib.pl" -ra "%1" [where the ... are system dependent] # You might want several different associations that use different switches #% Copyright (C) Mike Brookes 2006 #% Version: $Id: gatherbib.pl,v 1.3 2006/01/24 10:03:21 dmb Exp $ #% #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #% This program is free software; you can redistribute it and/or modify #% it under the terms of the GNU General Public License as published by #% the Free Software Foundation; either version 2 of the License, or #% (at your option) any later version. #% #% This program is distributed in the hope that it will be useful, #% but WITHOUT ANY WARRANTY; without even the implied warranty of #% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #% GNU General Public License for more details. #% #% You can obtain a copy of the GNU General Public License from #% ftp://prep.ai.mit.edu/pub/gnu/COPYING-2.0 or by writing to #% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% use File::Find; use File::Copy; # THIS IS THE SYSTEM-DEPENDENT DEFAULT LOCATION FOR PDF FILES # YOU CAN HAVE MULTIPLE LOCATIONS. # + on the end means recurse, ; between paths, use / or \\ as path separators $basedir = "E:/dmb/refs/refs+"; $logging = 0; # set to 1 to force logging $rdir = 0; # set to 1 to create a "refs" subdirectory $adir = 0; # set to 1 to create a,b,...,z subdirectories if (@ARGV) { foreach (@ARGV) { $logging and print LOGFILE "Argument: $_\n"; if (/^-/) { $rdir = $rdir || /r/; $adir = $adir || /a/; $logging = $logging || /l/; } else { if ( defined($infile) ) { $basedir = $_; } else { $infile = $_; } } } } else { $rdir = 1; # use "refs" subdirectory $adir = 0; # but no a,b,...,z subdirectories } $logging and $logfile = "gatherbib.log"; $logging and open( LOGFILE, ">$logfile" ) || die "Can't open $logfile\a\n"; if ( !defined($infile) ) { $infile = join( ";", <*.bib> ); } $logging and print LOGFILE "Bib file: $infile, refs dir=$rdir, abc... dir=$adir, PDFs at $basedir\n"; foreach ( split( /;/, $basedir ) ) { print "basedir = $_\n"; if (s/\+$//) { find( \&fcat, $_ ); } else { while (<$_/*.pdf>) { # $logging and print LOGFILE "$_\n"; m$.*/(.*)$; $pdf{ lc($1) } = $_; } } } # while ( ( $tail, $full ) = each %pdf ) { # $logging and print LOGFILE "$tail = $full\n"; # } $mkrdir = $rdir; # need to make refs directory when required # now search the bib files foreach ( split( /;/, $infile ) ) { open( BIB, $_ ) || die "Cannot open bibfile: $_"; if (m$(.*)[/\\]$) { $predir = "$1"; } else { $predir = "."; } if ($rdir) { $predir = $predir . "/refs"; } while () { if (/^\s*@.*{(.*?)\s*,/) { $bibkey = $1; $pkey = lc("$bibkey.pdf"); if ( exists( $pdf{$pkey} ) ) { if ($mkrdir) { if ( !-d $predir ) { mkdir($predir); $logging and print LOGFILE "created $predir\n"; $mkrdir = 0; } } if ($adir) { $flet = substr( $pkey, 0, 1 ); # get initial letter $hdir = "$predir/$flet"; if ( !exists $mkadir{$flet} ) { if ( !-d $hdir ) { mkdir($hdir); $logging and print LOGFILE "created $hdir\n"; $mkadir{$flet} = 1; } } } else { $hdir = $predir; } if ( !-f "$hdir/$bibkey.pdf" ) { copy( "$pdf{$pkey}", "$hdir/$bibkey.pdf" ); $logging and print LOGFILE "copied $pdf{$pkey} to $hdir/$bibkey.pdf\n"; } else { $logging and print LOGFILE "$hdir/$bibkey.pdf already exists\n"; } } else { $logging and print LOGFILE "$bibkey not found\n"; } } } } $logging and close LOGFILE; sub fcat() { # /\.pdf$/ and $logging and print LOGFILE "$File::Find::name\n"; /\.pdf$/ and $pdf{ lc($_) } = $File::Find::name; }