Deleting unwanted Vim swap files using Perl

Published by at 29th October 2010 8:49 pm

Yesterday I realised that I had somehow managed to scatter Vim swap files all across the Dropbox folder I use to share Perl and Python scripts I'd written between several computers, and it would be a good idea to clear them up. I didn't like the idea of using grep to search for them and manually deleting them, so I decided this was the ideal opportunity to write a Perl script to do it for me!

I came up with the following:

1#!/usr/bin/perl -w
2
3use strict;
4use Cwd;
5
6sub searchDir
7{
8 # Subroutine to scan a directory looking for Vim swap files
9 # Get directory to read and current directory
10 my $readdir = shift;
11 my $startdir = cwd();
12
13 # Change directory to the target one
14 chdir($readdir) or die "Unable to open $readdir! $!\n";
15 print "Scanning contents of directory $startdir\n";
16
17 # Open the directory and grab the names of all the files and folders in it
18 opendir(DIR, ".") or die "Unable to open current directory! $!\n";
19 my @entries = readdir(DIR) or die "Unable to read directory! $!\n";
20 closedir(DIR);
21
22 # Loop through the files and folders in the directory
23 foreach my $entry (@entries)
24 {
25 # Skip this one and the one above it in the filesystem hierarchy
26 next if($entry eq ".");
27 next if($entry eq "..");
28
29 # If a file is a directory, call the searchDir subroutine recursively in order to scan it
30 if(-d $entry)
31 {
32 searchDir($entry);
33 next;
34 }
35
36 # Use a regular expression to check to see if the current file starts with a period, and ends with .swp - if it does, it's a Vim swap file
37 if($entry =~ m/^\..*\.swp$/)
38 {
39 # Inform the user that a Vim swap file has been found and print out the path to it
40 print "Found a Vim swap file!\n";
41 my $swppath = cwd();
42 print "It's the file $entry in $swppath.\n";
43 my $fullpath = $swppath . "/" . $entry;
44 print "The full path is $fullpath.\n";
45
46 # Prompt the user to delete the file
47 print "Do you wish to delete this file? (Y/N)\t";
48 chomp(my $reply = );
49 if($reply =~ m/y/i)
50 {
51 print "Deleting $fullpath...\n";
52 unlink($fullpath);
53 }
54 }
55 }
56
57 chdir($startdir);
58}
59
60# Get directory to begin the search
61print "Enter directory to start search: ";
62chomp(my $beginSearch = );
63
64# call searchDir to start the search
65searchDir($beginSearch);

Thankfully, I've now discovered the Preserve Code Formatting plugin for WordPress, which seems to do a good job at making the code look presentable!

This isn't perfect - it uses recursion to examine subdirectories, and when I ran it on my /home folder it somehow wound up in /sys on my Ubuntu machine and I ended up getting a deep recursion warning (a little research suggests this happens when it goes over 100 directories in). However, it seems to work fine for scanning individual folders in my /home directory, and that's all I really wanted anyway.

I love how Perl makes writing this kind of simple script so easy. It's a great language for that kind of systems administration task.