Skip to content

Instantly share code, notes, and snippets.

@ThomazPom
Last active November 28, 2019 17:23
Show Gist options
  • Save ThomazPom/9a1000b3ef5aa0b1c7320bf3ba3935ec to your computer and use it in GitHub Desktop.
Save ThomazPom/9a1000b3ef5aa0b1c7320bf3ba3935ec to your computer and use it in GitHub Desktop.
Function Move-Create-Rename {[CmdletBinding()]Param([Parameter(ValueFromPipeline)]
$item,$Destination
)
$item_filter = Join-Path -Path "./" -ChildPath ($item.Name -replace '[[*?\]]','`$&')
$itemName_filter = $item.Name -replace '[[*?\]]','`$&'
$itemBaseName_filter = $item.BaseName -replace '[[*?\]]','`$&'
New-Item -ItemType Directory -Force -Path $Destination
$num=1
$nextName = Join-Path -Path $Destination -ChildPath $itemName_filter
while(Test-Path -Path $nextName)
{
$nextName = Join-Path -Path $Destination -ChildPath ($itemBaseName_filter + "_$num" + $item.Extension)
$num+=1
}
echo $item_filter
echo $nextName
Move-Item $item_filter -Destination $nextName
}
$hashtable = @{
Audios='*.aif,*.cda,*.mid *.midi,*.mp3,*.mpa,*.ogg,*.wav,*.wma,*.wpl';
Discs='*.bin,*.dmg,*.iso,*.toast,*.vcd';
Executables='*.apk,*.bat,*.bin,*.cgi,*.pl,*.com,*.exe,*.gadget,*.jar,*.wsf,*.sh,*.cmd';
Datas='*.csv,*.dat,*.db,*.dbf,*.log,*.mdb,*.sav,*.sql,*.tar,*.xml';
Fonts='*.fnt,*.fon,*.otf,*.ttf';
Images='*.ai,*.bmp,*.gif,*.ico,*.jpeg,*.jpg,*.png,*.ps,*.psd,*.svg,*.tif,*.tiff';
Presentations='*.key,*.odp,*.pps,*.ppt,*.pptx';
Programming='*.asp,*.aspx,*.cer,*.cfm,*.cgi,*.pl,*.css,*.htm,*.html,*.js,*.jsp,,*.php,*.py,*.rss,*.xhtml,*.c,*.class,*.cpp,*.cs,*.h,*.java,*.sh,*.swift,*.vb';
Spreadsheets='*.ods,*.xlr,*.xls,*.xlsx,*.xlsb,*.xlsm';
System='*.bak,*.cab,*.cfg,*.cpl,*.cur,*.dll,*.dmp,*.drv,*.icns,*.ico,*.ini,*.msi,*.sys,*.tmp,*.inf';
Videos='*.3g2,*.3gp,*.avi,*.flv,*.h264,*.m4v,*.mkv,*.mov,*.mp4,*.mpg,*.mpeg,*.rm,*.swf,*.vob,*.wmv';
Documents='*.doc,*.docx,*.odt,*.pdf,*.rtf,*.tex,*.txt,*.wks,*.wps,*.wpd';
Archives='*.7z,*.arj,*.deb,*.pkg,*.rar,*.rpm,*.tar,*.gz,*.z,*.zip,';
Other='not.not';
Links='*.url';
}
$exclude = "*.part,+*"
foreach ($key in $hashtable.Keys) {
$value = $hashtable[$key]
$path = "./$key/"
#New-Item -ItemType Directory -Force -Path $path
Get-ChildItem ./* -Include $value.Split(",") -Force -Exclude "$exclude".Split(",") | ForEach-Object {Move-Create-Rename $_ -Destination $path }
}
If ((Get-Item ./).Name -eq "Desktop")
{
$path = "C:\users\public\Desktop"
Get-ChildItem -Path $path -Include *.lnk -Force -Recurse -Exclude "$exclude".Split(",") | Where-Object {$_.DirectoryName -eq $path} | ForEach-Object {Move-Create-Rename $_ -Destination ".\Links" }
$path=Get-Location;
$path="$path";
Get-ChildItem -Path $path -Include *.lnk -Force -Recurse -Exclude "$exclude".Split(",") | Where-Object {$_.DirectoryName -eq $path} | ForEach-Object {Move-Create-Rename $_ -Destination ".\Links" }
}
$joined = $hashtable.Keys -join ','
$path = "./Other/"
Get-ChildItem ./ -Exclude "$joined,$exclude".Split(",") -Force | Where-Object {$_.Length -gt 0} |ForEach-Object {Move-Create-Rename $_ -Destination $path }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment