Scripts for enforcing naming conventions?
May 1, 2011 6:33 PM   Subscribe

I'm trying to find scripts to help my studio follow naming conventions.

In my design studio, we're supposed to save work in a folder with a few pre-set sub-folders. And within those subfolders, files are supposed to follow a certain naming convention.

I'd love to run a script that gives a sense of what folders/files aren't following the naming convention but I'm not great at shell scripts so I can't just write one myself.

Anyone have any pointers?
posted by jragon to Computers & Internet (12 answers total) 1 user marked this as a favorite
 
For new projects, Structurer for Mac is a nice utility to create file/folder structures. There's also a Pro version.
posted by clearlydemon at 7:08 PM on May 1, 2011


At a minimum, you need to provide a description of the naming convention(s) and a few examples. Also helpful would be the platform you will run the script(s) on.
posted by secret about box at 7:10 PM on May 1, 2011


Response by poster: I'll be on a Mac, querying a fileserver, so a unix shell script would be great.

Assume at the top level, for each directory this is pointed at, you need three folders:

Apple
Orange
Plum

And if there are any others, they're flagged as being incorrect.

Within each of these folders, let's say some rules for the filenames are:

Underscores not spaces
No uppercase
PNG or PSD extension

(I'd be adding more/different ones, but that's enough to give a sense of the direction)
posted by jragon at 7:15 PM on May 1, 2011


Here's a stab at something. That's been lightly tested but not thoroughly so there may be bugs. It will test every subdirectory of whatever you set 'toplevel' to for each of the example criteria you listed.
posted by Rhomboid at 7:34 PM on May 1, 2011


Best answer: Oops, couple of quoting bugs and I missed the requirement to detect subdirs not on the approved names list. Give this one a shot.
posted by Rhomboid at 7:48 PM on May 1, 2011


Silly me, it would be more efficient to use -printf "Filename rule violation: %p\n" in place of -exec echo ... \; but that's only a performance thing.
posted by Rhomboid at 8:01 PM on May 1, 2011


Best answer: Perl! example.
Pretty much there are a bazillion ways to do this depending on how complex your requirements are. I would probably think a bit longer and write something more Perlish for my own use (like using File::Find, or building file_must_match/file_must_not_match regex arrays). I usually start output with X: (Ok, Error, Warn) for these types of things for fast grepability of output, and usually just say bad_x instead of verbose error messages. Depends on your needs.
posted by zengargoyle at 8:59 PM on May 1, 2011


Response by poster: I really appreciate the help, guys. Thanks :)
posted by jragon at 10:35 PM on May 1, 2011


I'd do it this way. The script accepts any number of directories to validate as command line arguments.
#!/bin/sh
tmp=/tmp/validate-$$
mkdir "$tmp"

sort <<-. >"$tmp/wanted"
	Orange
	Apple
	Plum
.

for dir in "$@"
do
	find "$dir" -maxdepth 1 -type d -not -name . -printf '%f\n' | sort >"$tmp/found"
	comm -2 -3 "$tmp/wanted" "$tmp/found" | sed "s!.*!Missing dir: $dir/&!"
	comm -1 -3 "$tmp/wanted" "$tmp/found" | sed "s!.*!Extra dir: $dir/&!"
	find "$dir" -maxdepth 1 -type f -printf "Extra file: $dir/%f\n"
	find "$dir" -type f -name '* *' -printf "Space in name: $dir/%f\n"
	find "$dir" -type f -name '*[A-Z]*' -printf "Uppercase in name: $dir/%f\n"
	find "$dir" -type f -not -name '*.png' -not -name '*.psd' -printf "Bad extension: $dir/%f\n"
done

rm -r "$tmp"

posted by flabdablet at 12:16 AM on May 2, 2011


Best answer: Revised version, using Rhomboid's %p suggestion, and including a bug fix (the first version might fail to notice a missing subdirectory if the directory being scanned is itself named Apple, Orange or Plum):
#!/bin/sh
tmp=/tmp/validate-$$
mkdir "$tmp"

sort <<-. >"$tmp/wanted"
	Orange
	Apple
	Plum
.

for dir in "$@"
do
	find "$dir" -mindepth 1 -maxdepth 1 -type d -not -name . -printf '%f\n' | sort >"$tmp/found"
	comm -2 -3 "$tmp/wanted" "$tmp/found" | sed "s!.*!Missing dir: $dir/&!"
	comm -1 -3 "$tmp/wanted" "$tmp/found" | sed "s!.*!Extra dir: $dir/&!"
	find "$dir" -maxdepth 1 -type f -printf 'Extra file: %p\n'
	find "$dir" -type f -name '* *' -printf 'Space in name: %p\n'
	find "$dir" -type f -name '*[A-Z]*' -printf 'Uppercase in name: %p\n'
	find "$dir" -type f -not -name '*.png' -not -name '*.psd' -printf 'Bad extension: %p\n'
done

rm -r "$tmp"

posted by flabdablet at 12:30 AM on May 2, 2011


Further fiddling reveals that with the -mindepth 1 option in place, the -not -name . expression achieves nothing and can be left out.
posted by flabdablet at 1:14 AM on May 2, 2011


In case you weren't aware, Macs ship standard with a number of interpreters for scripting languages besides straight shell script, so if you don't want to wade through trying to make a shell script do what you want, you also have the aforementioned Perl, plus Python and Ruby at your disposal.
posted by andrewpendleton at 11:18 AM on May 2, 2011


« Older Computer memory   |   Why can't I get over this when I was over it... Newer »
This thread is closed to new comments.