Mundane Tasks — Perl to the Rescue Part 2

If a thing is worth doing, it’s worth doing well — unless doing it well takes so long that it isn’t worth doing any more. Then you just do it ‘good enough’.
“Programming perl” by Wall and Schwartz

I had stored a lot of ideas as text files in DEVONthink. Given the amount of files (a few thousand), I wanted to sort them and put them grouped by topic in OmniOutliner files. Sorting was quite easy by tagging the ideas (took a few days), but how to get them into OmniOutliner files?

I thought about simply adding them all into a TextWrangler window. Depending on the text editor — and TextWrangler is capable of doing so — the text files simply become added to a single text file. However, I also wanted to have the current file name (usually the title of the idea) and have the text of the file indented. Removing empty lines would be nice to.

So, using the usual suspects like perlmaven.com and the like, and akin to my other posting on using perl, I hacked together a script that did the work. The script is pretty basic, it just takes all the text file in the directory it is in and puts them together in a single text file with the file name and the content indented. OmniOutliner automatically indents the text, so you end up with the file name as parent cell and the content as child cells.

Of course, there are text files which use tabs that can be a problem, so, it’s quick and dirty but it does the job. It would be possible to expand the script to go through all sub-directories, but simply putting the ideas to one topic in the same directory (just showing all text files with the tag in DEVONthink and dragging it into a directory) worked as well. It would also be possible to replace indents in the content to avoid conflicts with indention in OmniOutliner (regex should do it), but yeah, it’s basic.

#!/usr/bin/perl

    use strict;
    use warnings;
    use Cwd qw(cwd);

    # my $dir = '/Users/danielwessel/Desktop/test';
    my $dir = cwd;

    opendir(DIR, $dir) or die $!;
    
    my $writefilename = '00_all.txt';
    open(my $fhw, '>', $writefilename) or die "Could not open file '$writefilename' $!";
	print $fhw "Content of $dir\n";
    
    while (my $file = readdir(DIR)) {

        # Use a regular expression to ignore files beginning with a period
        next unless ($file =~ m/\.txt$/);

		print "$file\n";

		my $filename = $file;
		open(my $fh, "<", $filename)
  			or die "Could not open file '$filename' $!";
 	
 		print $fhw "$file\n";
 	
		while (my $row = <$fh>) {
			chomp $row;
			# print "$row\n";
		
			if (length($row) > 0) {
    			print $fhw "\t$row\n";
			} # end if write if not empty line
			
		} # end while reading file

    } # end while (reading files in directory)

	close $fhw;
	print "done\n";

    closedir(DIR);
    exit 0;

If you want to execute the script, just use the terminal with perl FILENAME.pl, e.g.,

perl dome.pl

It will just use the text files in the current directory. As usual, no warranty.