AUTOMATION IS STILL MANUALLY CREATED. LENGTH IS UNIQUE.


rv * -ErrorAction SilentlyContinue
$scriptdir = "C:\automation\filecleanup"
function arraytohash {
    param (
        $array,
        $key
    )
    $hash = @{}
    
    $arrayproperty = ($array | Get-Member | Where-Object {$_.membertype -eq "NoteProperty" -or $_.membertype -eq "Property"}).Name
    foreach ($i in $array) {
        $object = New-Object pscustomobject
        foreach ($p in $arrayproperty) {
            $object | Add-Member -NotePropertyName $p -NotePropertyValue $($i).$($p)
            if ($($i).$($p)) {
                if ($($i).$($p).gettype().Name -eq "string") {
                    $($i).$($p) = $($i).$($p).trim()
                }
            }
        }
        $hash.Add($i,$key,$object)
    }
}
function index {
    param(
    $array
    )
    $array | Add-Member -NotePropertyName "index" -NotePropertyValue $null -Force
    $count = 0
    foreach ($i in $array) {
        $i.index = $count
        $count++
    }
    return $array
}
#target directory to clean
$target = 'C:\#young\_divineyc\jpg'
$list = ls -Recurse $target
$list = $list | ? {$_.attributes -ne "Directory" -and $_.Attributes -ne "ReadOnly, Directory"}
$list = index $list
#variables to use and fill in - new friendly name
$list | Add-Member -NotePropertyName newname -NotePropertyValue $null -Force
#variables to use and fill in - new friendly name full path
$list | Add-Member -NotePropertyName newnamelong -NotePropertyValue $null -Force
#variables to use and fill in - extension without "." folder creation (no hidden).
$list | Add-Member -NotePropertyName extensionshort -NotePropertyValue $null -Force
#status update field.
$list | Add-Member -NotePropertyName action -NotePropertyValue $null -Force

$extension = $list | Group-Object extension 
mkdir $scriptdir
foreach ($i in $extension) {
    $newname = $($i.name).replace(".","")
    mkdir $scriptdir\$newname -ErrorAction SilentlyContinue
}
#varibles filled in
#the regex lucky for me groups as full words based on the regex. then join the spacing as _. Lovely.
$regex = [regex] '[a-zA-Z0-9.]+'

foreach ($i in $list) {
    $i.newname = "$((($regex.matches($($i.Name.ToLower()))).value -join "_"))"
    $i.newnamelong = "$scriptdir\$($i.extensionshort)\$($i.newname)"
    $i.extensionshort = $i.Extension.replace(".","")
}
<#we use the length value which was determined to be unique enough. 
where at a glance I was unable to find not the same.
with that value, I keep the first, and delete the rest.

NOTE: that $glist modifies $list when iterating through the group.
#>
$glist = $list | Group-Object length
foreach ($i in $glist) {
    $count = 0
    foreach ($i2 in $i.group) {
        if ($count -eq 0) {
            $i2.action = "keep"
        } else {
            $i2.action = "delete"
        }
        $count++
    }
}
<# another thing to do is cat the file and 'grep' the 50..100 lines, but was unsuccessful
(cat $filname)[50..100] 
as the unique value, the length was good enough
#>

foreach ($i in $list) {
    if ($i.action -eq "delete") {
        Remove-Item $i.fullname
        if ($? -eq $false) {
            pause
        } else {
            $i.action = "delete complete"
        }
    }
}

$glist = $list | Group-Object newnamelong

<#after reviewing the file names, 
because each file is unique, 
the duplicates so appending the $count value#>
foreach ($i in $list) {
    Move-Item $i.fullname $i.newnamelong
    $count = 0
    while ($? -eq $false) {
        $count++
        Move-Item $i.fullname "$($i.newnamelong)$count"
    }
}





<#



foreach ($i in $list3) {
    $i.grouping = $glist | ? {$_.name -eq $i.length}    

}



#syntax
(($test.matches($i.Name)).value)[0]

$norm = [regex]'([a-zA-Z0-9.]*)'
$norm.matches(($i.Name)).value[0]

$test = [regex] '[a-z0-9\.]+'



<#old shit
$list = ls | select *
foreach ($i in $list) {
    $count++
    write-host "$($count) out of $($list.count)"
    cat $i.FullName
    $input = read-host "action"
    if ($input -eq "del") {
        remove-item $i.fullname
    } else {
        mkdir "F:\#story\$($input)"
        Move-Item $i.FullName "F:\#story\$($input)\$($i.Name)"
    }
}
#>