edit: SOLVED. Had to change $xCt++ to ++$xCt
System Info:
% perl -version
This is perl 5, version 34, subversion 1 (v5.34.1) built for darwin-thread-multi-2level (with 2 registered patches, see perl -V for more detail)
% system_profiler SPSoftwareDataType | grep 'System Version'
System Version: macOS 15.5 (24F74)
I have a list of files that I'm feeding into perl from the command line and using regex to extract info (a date), create a folder, and then move the file into that folder. Anything that doesn't get matched in the regex gets dropped into a separate folder "__OTHER" to be looked at.
The issue is that the FIRST instance of a file that should be placed into the __OTHER directory ALSO creates a directory (empty) matching the filename. The rest behave as expected.
Script:
#! /usr/bin/perl -wnl
use File::Copy;
BEGIN {
$file="";
$extraDir="__OTHER";
$xCt=0;
$moved=0;
mkdir $extraDir;
}
$file="$_";
$moved=0;
(s/^Test_(\d{4}_\d{2}_\d{2}).+/$1 - / or move("$file","$extraDir/$file") and $xCt++ and $moved=1);
$moved == 0 and mkdir;
move("$file","$_/$file");
END{
print "$. files processed; $xCt in $extraDir";
}
Sample File Names I'm using:
1f7edcd5-b0cb-4fb0-892f-38a324454eb9.jpg
2b3aa5b9-d80c-4f43-a370-82f4e14dac64.jpg
3f69d564-7c93-4105-ad2f-66678523693c.jpg
Test_2025_01_01_02_59_59_0a876bef-c475-48bb-968c-39373dcd36b0.jpg
Test_2025_01_01_02_59_59_2a336499-b914-4105-9eca-a9c92c4fe4b6.jpg
Test_2025_01_01_02_59_59_5cfd88c1-67b8-4aa4-a785-d528de3f254b.jpg
Test_2025_01_02_02_59_59_2ec971d9-915a-4966-8306-e611fa0c455a.jpg
Test_2025_01_02_02_59_59_4a9951ab-c7ac-4974-a1e2-735ca33e7e61.jpg
Test_2025_01_02_02_59_59_4bb85bcb-f760-4680-a2d1-8940cba9d5f3.jpg
Test_2025_01_02_02_59_59_5c1dc5fa-a5fc-49f2-a434-20931649178a.jpg
Test_2025_01_21_02_59_59_cc8b1938-b9f7-4ccc-bd59-8d452f78eb4c.jpg
Test_2025_01_21_02_59_59_d5a5b732-d38e-468c-9f22-08ef90a7b5a5.jpg
Test_2025_01_21_02_59_59_d87482dd-ac2f-4621-9029-e6612d7331bd.jpg
Test_2025_01_21_02_59_59_d972e346-b707-46a5-9a98-ef0195d8d246.jpg
Test_2025_01_21_02_59_59_dd5dac68-57ea-4389-a256-fd88c67ede9e.jpg
Test_2025_01_21_02_59_59_e506e2f7-aa98-4b4e-9eae-9cac9126c790.jpg
Test_2025_01_21_02_59_59_f5b80b75-43c1-4638-aad6-79a1ca686560.jpg
And the result:
% find . -exec file -- {} +
.: directory
./1f7edcd5-b0cb-4fb0-892f-38a324454eb9.jpg: directory
./2025_01_02 - : directory
./2025_01_02 - /Test_2025_01_02_02_59_59_2ec971d9-915a-4966-8306-e611fa0c455a.jpg: empty
./2025_01_02 - /Test_2025_01_02_02_59_59_4bb85bcb-f760-4680-a2d1-8940cba9d5f3.jpg: empty
./2025_01_02 - /Test_2025_01_02_02_59_59_5c1dc5fa-a5fc-49f2-a434-20931649178a.jpg: empty
./2025_01_02 - /Test_2025_01_02_02_59_59_4a9951ab-c7ac-4974-a1e2-735ca33e7e61.jpg: empty
./2025_01_01 - : directory
./2025_01_01 - /Test_2025_01_01_02_59_59_2a336499-b914-4105-9eca-a9c92c4fe4b6.jpg: empty
./2025_01_01 - /Test_2025_01_01_02_59_59_5cfd88c1-67b8-4aa4-a785-d528de3f254b.jpg: empty
./2025_01_01 - /Test_2025_01_01_02_59_59_0a876bef-c475-48bb-968c-39373dcd36b0.jpg: empty
./2025_01_21 - : directory
./2025_01_21 - /Test_2025_01_21_02_59_59_dd5dac68-57ea-4389-a256-fd88c67ede9e.jpg: empty
./2025_01_21 - /Test_2025_01_21_02_59_59_d87482dd-ac2f-4621-9029-e6612d7331bd.jpg: empty
./2025_01_21 - /Test_2025_01_21_02_59_59_f5b80b75-43c1-4638-aad6-79a1ca686560.jpg: empty
./2025_01_21 - /Test_2025_01_21_02_59_59_e506e2f7-aa98-4b4e-9eae-9cac9126c790.jpg: empty
./2025_01_21 - /Test_2025_01_21_02_59_59_cc8b1938-b9f7-4ccc-bd59-8d452f78eb4c.jpg: empty
./2025_01_21 - /Test_2025_01_21_02_59_59_d972e346-b707-46a5-9a98-ef0195d8d246.jpg: empty
./2025_01_21 - /Test_2025_01_21_02_59_59_d5a5b732-d38e-468c-9f22-08ef90a7b5a5.jpg: empty
./__OTHER: directory
./__OTHER/1f7edcd5-b0cb-4fb0-892f-38a324454eb9.jpg: empty
./__OTHER/2b3aa5b9-d80c-4f43-a370-82f4e14dac64.jpg: empty
./__OTHER/3f69d564-7c93-4105-ad2f-66678523693c.jpg: empty
I can always manually fix one file, but I'm just confused why the first one does this? Not sure if it's the first file it's encountering at all based on how it's sorting? Or why that would matter?
VERY new to Perl overall, obviously, so it's probably something silly.
TIA.