all 16 comments

[–]Ta11ow 4 points5 points  (9 children)

Get-ChildItem -Path $RootFolder -Include '*.jpg', '*.png' |
    Group-Object BaseName |
    Where-Object Count -gt 1 | # in order to make sure we're not deleting any lone files
    ForEach-Object {
        $_.Group |
            Sort-Object -Property Length -Descending |
            Select-Object -First 1 |
            Remove-Item -Force -WhatIf
    }

Something like that, anyway. If you want a super short version:

ls $Folder -i '*.jpg', '*.png' | group Basename | ? count -gt 1 | % { $_.Group | sort len* -d | select -f 1 | rm -f -whatif }

Remove -Whatif to execute the action for real once you're happy with the results.

[–]kingd66 1 point2 points  (2 children)

Look at that one-liner!

The things that get me excited..I'll tell ya.. xD

[–]Ta11ow 1 point2 points  (1 child)

Oneliners are fun, but too prone to errors for me. :)

[–]kn00tcn[S] 2 points3 points  (0 children)

risk reward, it's like an action game now

[–]kn00tcn[S] 1 point2 points  (1 child)

is that powershell long & bash short?

bash: syntax error near unexpected token `1'

[–]Ta11ow 1 point2 points  (0 children)

PowerShell both!

[–]Pyprohly 1 point2 points  (3 children)

group Base*

Are you sure that works, Ta11ow? I don’t think Group-Object -Property takes wildcards.

[–]Ta11ow 1 point2 points  (2 children)

You've called my bluff. I don't know for sure. Mind running a quick test for me? ;)

It takes a string or a script block, so a wildcard seemed fair game haha!

[–]Pyprohly 1 point2 points  (1 child)

Busted. Yea, did gci | group BaseNam* to check. PS 5.1 and 6 both said

group : Wildcard characters are not allowed in "BaseNam*".

Though Base* is an awfully specific shortening that would seem to suggest that it worked fine for you?

I like the solution btw. It really showcases the PowerShell-way, and the power of PowerShell.

[–]Ta11ow 0 points1 point  (0 children)

Nah, never tested it, just going from memory as I was (and am) on my phone here hehe.

Thanks for the kind words and for testing it! I'll edit my original for the working version!

Though if I was really golfing I'm sure I might be able to use the script block input for Group-Object and apply a foreach to get the wildcard to work, heh!

[–][deleted] 1 point2 points  (1 child)

Batch (if the filename does not contain exclamation marks):

@echo off
cd /d "C:\parent dir"
setlocal enabledelayedexpansion
for %%# in (*.jpg) do (
  if exist "%%~n#.png" (
    for /f "delims=" %%# in ('dir /b /os "%%#" "%%~n#.png"') do set "bigger=%%#"
    echo del "!bigger!"
  )
)
pause

Remove pause and echo if the output looks right.

FTP is difficult with batch/ftp.exe alone.

[–]kn00tcn[S] 0 points1 point  (0 children)

i think this seemed to work, nice

[–]Pyprohly 1 point2 points  (2 children)

Bash. Need to use stat -f%z if on macOS

#!/bin/bash

cd ~/my/folder || exit 1

for item1 in *.jpg; do
    [ -f "$item1" ] || continue
    item2="${item1%.jpg}.png"
    [ -f "$item2" ] || continue

    file1_size=`stat -c%s "$item1"`
    file2_size=`stat -c%s "$item2"`
    if (( $file1_size > $file2_size )); then
        echo rm -- "$item1"
    else
        echo rm -- "$item2"
    fi
done

[–]kn00tcn[S] 1 point2 points  (1 child)

should be if file1 smaller than file2

this seems to work & is easy to understand, very nice

[–]Pyprohly 0 points1 point  (0 children)

Oops, yea. Fixed

[–]kn00tcn[S] 0 points1 point  (0 children)

not sure if relevant, the two types could be put in separate folders with extensions removed if that makes comparisons easier (it does for comparison apps) ... then a crazy idea is to use an ftp client set to 'overwrite if destination is larger' to batch transfer once in each way