Last active
August 3, 2022 20:25
-
-
Save andreabradpitto/86baa9e53038ed30b8e9da7a6108cc45 to your computer and use it in GitHub Desktop.
A simple PowerShell script I made for fun/learning, that automates image collection from folders which contain potentially any file type. The collected images are then stored together in a separated folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -------------------- | |
# Author: Andrea Pitto | |
# Created: 05/05/2022 | |
# -------------------- | |
# --- PURPOSE --- | |
# This script picks all and only the .png/.jpg/.jpeg images from all the first level sub-folders | |
# of a determined source folder ($src_folder_name), and it then copies them inside | |
# another dedicated destination folder ($dest_folder_name). | |
# The script does not overwrite image files as, before each copying attempt, | |
# it preemptively checks for the existence of a file with the same name in the destination folder. | |
# --- INSTRUCTIONS --- | |
# - Option 1 - | |
# Launch PowerShell and move to the folder where this script has been stored, then run the script with: | |
# .\img_collector.ps1 | |
# - Option 2 - | |
# Alternatively, launch File Explorer, reach the folder where this script has been stored, then right-click on this file, | |
# and finally left-click on the "Run with PowerShell" overlaying menu entry. | |
# --- PARAMETERS --- | |
$src_folder_name = "Original folders" # Name of the source folder | |
$dest_folder_name = "Filtered images" # Name of the destination folder | |
# --- CODE --- | |
$src = $PSScriptRoot + "\$src_folder_name" | |
$dest = $PSScriptRoot + "\$dest_folder_name" | |
# The 2 instructions above generate absolute paths to the source and destination folders. | |
# This way, the script is independent from the location it gets stored in. | |
# The only requirement is that this script, the source folder and the destination folder | |
# are located under the same parent folder (represented by "." below): | |
# . | |
# ├── img_collector.ps1 | |
# ├── $src_folder_name | |
# │ ├── sub-folder 1 | |
# │ ├── sub-folder 2 | |
# │ ├── sub-folder 3 | |
# │ └── etc. | |
# └── $dest_folder_name | |
$img_counter = 0 # Counter for the number of successfully copied images | |
$file_total = 0 # Counter for the total number of parsed files | |
$subfolder_total = 0 # Counter for the number of parsed source sub-folders | |
ForEach ($dir in (Get-Item -Path "$src\*" | ?{$_.PSIsContainer})) # N.B.: $dir and $file represent paths! | |
{ | |
ForEach ($file in (Get-Item -Path "$dir\*")) | |
{ | |
#Write-Output $file # Useful for debugging | |
$file_name = Split-Path $file -leaf | |
If ($file -Like "*.png" -Or $file -Like "*.jpg" -Or $file -Like "*.jpeg" -And -Not(Test-Path -Path .\$dest_folder_name\$file_name)) | |
{ | |
Copy-Item $file -Destination $dest | |
$img_counter++ | |
} | |
$file_total++ | |
} | |
$subfolder_total++ | |
} | |
Write-Output "`nNew images copied : `t$img_counter`nTotal files parsed: `t$file_total`nSub-folders parsed: `t$subfolder_total`n" | |
Remove-Variable -Name * -ErrorAction SilentlyContinue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment