Posts Tagged ‘Perl’

FTP uploading a directory of files using perl

Monday, February 18th, 2008

Here is a simple script to upload the files which are in a particular directory. I use something similar to automate the file uploading for my tube walking blog.

Whenever I take photos for the blog I put them into a directory with the current date as the directory name. So I have a structure like this:

c:\website\080115\
c:\website\080203\
c:\website\080205\
c:\website\080218\

I want the images in these directories to be uploaded to a new directory on the FTP server where the FTP server directory is named with the date e.g. ‘080218’.

The script is run with a command such as:

perl imageupload.pl c:\website\080218\

The script will then:

  1. Extract the date part of the directory – ‘080218’.
  2. Connect to the FTP server.
  3. Create a directory called ‘080218’ on the FTP server.
  4. Get a list of all the local files in the directory that you passed in as the argument.
  5. Upload each of these files to the FTP server.

Here’s the script. You’ll need to modify the FTP server address, username and password if you want to use it.

use File::Basename;
use Net::FTP;
my $directory = $ARGV[0];
my @parts = split(/\\/, $directory);
# get leaf directory name from whole path
my $length = $parts;
my $dateDir = $parts[$length-1];
$ftp = Net::FTP->new("ftp.yourwebsite.com", Debug => 1)
    or die "Cannot connect to hostname: $@";
$ftp->login("username", "password")
    or die "Cannot login ", $ftp->message;
$ftp->cwd("/website/data")
    or die "Cannot change working directory ", $ftp->message;
# create 'date named' directory on FTP server
$ftp->mkdir($dateDir);
$ftp->cwd($dateDir);
# set binary mode which is needed for image upload
$ftp->binary();
opendir(DIR, "$directory");
my @files = readdir(DIR);
foreach my $file (@files)
    {
    if (not -d $file)
        {
        # make sure the names are lower case on the server
        $file = lc($file);
        # upload the file
        $ftp->put("$directory\\$file");
        }
    }
$ftp->quit();

The script is simple, but it can easily be integrated into a more complex set of scripts to help you manage your web site.

Backup / restore of last modified and created file time attributes on Windows

Saturday, January 12th, 2008

Last month I found that I needed to be able to backup and restore the last modified and created file times of a load of files in Windows XP. This is probably a bit of an obscure thing to need to do so naturally I couldn’t find any existing tools to do it.

I’ve therefore made a Perl script to allow this to be done. Just so you are clear, this isn’t backing up the files, it is backing up the last modified and created file time attributes.

This allows changes to be made to the files whilst keeping these attributes intact. In theory Perl’s utime function should allow me to change these attributes. However on my Windows XP machine it just didn’t work. I’ve therefore used the rather excellent nircmd tool to handle the file attribute modifications. You’ll need to download it and modify backupLastModified.pl to point to it.

Usage is simple:

perl backupLastModified.pl c:\yourdir -backup
perl backupLastModified.pl c:\yourdir -restore

Make sure you do a test of this script before you use it for real. It has only ever been tested on one machine.

  1. Download backupLastModified.zip
  2. View backupLastModified.pl

This script will create a file ‘backuplist.txt’ in the directory that you pass in as the first argument. If you run -backup multiple times then any new files will be appended to the backuplist.txt. The stored last modified / created times of any existing files will be preserved.