Creating files: Creating folders

  1. Creating files
  2. Replacing text

It’s easy enough to change directory when exporting files in order to ensure that the new files go into a specific folder, but if you’re using this as part of a cron job it will be easier if you can tell the script which folder you want the export files to go to.

} elsif ($switch eq "folder") {
if ($exportFolder = shift) {
if (-e $exportFolder) {
#if the folder exists, it needs to be a folder
help("$exportFolder already exists and is not a folder.") if !-d $exportFolder;
}
} else {
help("The folder option requires a folder name.");
}

What’s with the new use of help()? Every single time we use help, we also exit. Every time except one, we print out an error message above the call to help. It’s about time we automated this. It can often be difficult to make the decision to change a function when only a minor change is needed; every time we’ve called help so far, it’s been a simple effort to add the extra line above and below the help call. But for simplicity’s sake it is time we combined those two to three lines into a single subroutine call.

At the top of the help subroutine, just below “sub help {“, add:

#if there is an error message, print it out separated from the rest
if (my($message) = shift) {
print "\n$message\n\n";
}

And at the very end, just before “}”, add:

exit;

At some point, you’ll want to go through and find every use of:

print "some text";
help();
exit;

and replace it with:

help("some text");

If you don’t do it now, add a comment to the top of your script reminding you to do it later.

The other part that’s new is “-e $exportFolder”. There are several tests you can perform on filenames. They all begin with a dash. This is the exists test. It is “true” if there is a file with that name. Remember that in Unix, folders are also files.

If that name is already being used, that’s fine if it’s a folder. So we need to make sure it’s a folder. The “-d” file test tests for that. So we call help if not “-d $exportFolder”. If $exportFolder is not a directory, the script will print the help, which with the above change will automatically exit the script.

Okay, add this option to our help subroutine:

print "\t--folder <foldername>: export files are created in the specified folder\n";

Now it’s time to implement it. Between “@matches = sort byCustom @matches if $sortby;” and “foreach $match (@matches) {” add:

#create a folder if necessary, and move into it
if ($exportField && $exportFolder) {
if (!-e $exportFolder) {
if (!mkdir($exportFolder)) {
print "Unable to create $exportFolder: $!\n";
exit;
}
}
if (!chdir($exportFolder)) {
print "Unable to move into $exportFolder: $!\n";
exit;
}
}

If there is something in $exportField (that is, if we are exporting into some files) and if there is something in $exportFolder (that is, if we are doing this into a specific folder), we need to ensure that the folder exists and that we are in it.

Step one checks to see if $exportFolder already exists, using the -e file test. If it doesn’t, the script tries to create it using “mkdir()”. This stands for make directory. If that works, fine, but if not (watch the exclamation point) the script prints the error and exits.

Whether we just created the folder or it already existed, the next step is to get into it. This is the “chdir()” function. It stands for change directory. It doesn’t mean change a directory, but change into a directory. It moves the script into that directory so that any files the script creates from now on will be created in that directory.

If chdir is not successful, the script prints an error and exits.

We can now export the foreigner albums into their own folder:

./show --exact --artist foreigner --export album --folder Foreigner songs.txt

This makes it easier for us to export to multiple files without cluttering up the current directory.

  1. Creating files
  2. Replacing text