use File::Spec; use File::Copy; # Here's where some of your code is that sets some variables, gets the timestamp in array context... $year += 1900; # Y2K-compliant my $dir_prefix = sprintf("%04d%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec); my $create_dir = File::Spec->catfile($filedir, $dir_prefix); if (!-e $create_dir) { if (!mkdir($create_dir)) { # Do something. } else { if (!move($IWlogs, $create_dir)) { # Do something. } } }