Approximately 5k files to procees, ultimately I needing the counter ($i) available to all within the ‘for { …}’. This didn’t seem like a big deal, but leave it to me :-/ …
The issue: While there are valid duplicate files,
the ‘for’ is causing the script to create
duplicate output of entire path’s.
Example SubDir’s: 2019 to present (20??)
Example file’s being processed:
84_3_IPC10_NAPA_20220429050913.png
100_3_IPC10_NVR16-8580_20220507193710.mp4
127_0_IPC4_ROOF_20240619200942.png
27_0_IPC2_ROOF_20240619200942.jpg
27_0_IPC11_ROOF_20240619200942.png
The key difference’s with these filename’s will be:
^(\d{1,3}_
# The beginning of the string,
\d{1,2}_
# mostly followed by a single digit with a subsequent underscore,
\w{3})($i)
# the letters IPC followed by,
one or two digits, contained in $i,
i.e. IPC1
-thru- IPC14
;
all of them will change as the iteration continues.
# The script ...
Set-ExecutionPolicy -ExecutionPolicy remotesigned -Scope currentuser
for ($i = 1; $i -lt 14; $i++) {
# Only these SubDir's
Get-ChildItem -directory -Include 20?? -Recurse | foreach {
<# Get file's matching the regex.
Where $i is one of IPC1 -THRU- IPC
for each matching regex.
#>
Get-ChildItem -Recurse -File } | foreach { $newname = $_
$_regex = "^(\d{1,3}_\d{1,2}_\w{3})($i)_.+\.(jpg|mp4|png)"
if ($newname -match $_regex) {
$newname.fullname
}}}
Please note the difference between the above ps1
and the version run from the cli (below):
PS> Get-ChildItem -directory -Include 20?? -Recurse | foreach { Get-ChildItem -Recurse -File } | foreach {
$newname = $_
$_regex = "^(\d{1,3}_\d{1,2}_\w{3})(\d{1,2})_.+\.(jpg|mp4|png)"
if ($newname -match $_regex) {
$newname.fullname
}
}
Both version’s of the code create duplicate’s
of both filenames and full\file\path\to\filename;
depending on whether $newname.fullname or $newname.name is used.
A final note, when comparing the output of the two code version’s,
respective outputs, neither the total file count is the same, nor are all the item’s found in both of the resulting file’s a match; some are missing.
Thank you for taking the time to review; continued Respect.