Skip to content

Instantly share code, notes, and snippets.

@dginev
Created August 15, 2013 03:39
Show Gist options
  • Save dginev/6238070 to your computer and use it in GitHub Desktop.
Save dginev/6238070 to your computer and use it in GitHub Desktop.
A simple Perl mover that reorganizes a flat directory of TeX files into a CorTeX-compatible subdirectory organization.
# Mover.pl -- moves all foo.tex files in an input directory to
# a foo/foo.tex subdirectory
# 0. Dependencies
# Ensure sanity
use strict;
use warnings;
# File utilities
use File::Path qw/make_path/;
use File::Spec;
use File::Copy qw(move);
# I. Input
# Move the .tex files from a specified input $path
my $path = shift;
# Read in the @files we want to move
opendir DIR, $path;
my @files = grep {/\.tex$/} readdir(DIR);
closedir DIR;
# II. Move
# Compute the names of the new subdirs
my @subdirs = map {s/\.tex$//; $_;} @files;
# For each subdirectory, perform the move
foreach my $subdir(@subdirs) {
# The current file to move
my $current_file = File::Spec->catfile($path,"$subdir.tex");
# The new subdirectory
my $subdir_path = File::Spec->catdir($path,$subdir);
# The newly moved file
my $subdir_file = File::Spec->catfile($subdir_path,"$subdir.tex");
# Create subdirectory
make_path($subdir_path);
# Move the file in it
move $current_file, $subdir_file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment