Cleaning Document Folders

Often, in your document folders a lot of files exist, and most of the time they are not really organized. With the help of a little PowerShell script, you can easily clean up and organize your files based on file type.

Warning: The following script will move file content into organized subfolders. Do this only on folders that contain documents. Never try and "clean up" the windows folder or a program folder or else programs will be damaged. Try at your own risk!

The script cleans up your documents folder. It will only clean up file types listed in the $types array. It creates a subfolder for each type listed in $types and then moves the files into that subfolder.

(Note: the folder $home\Documents exists on Vista and above only. On XP, adjust the path accordingly)

$folder = "$home\Documents"
$types = ".bmp", ".jpg", ".doc", ".ps1", ".ocx", ".pdf",
".docx", ".txt", ".vbs", ".xls", ".zip", ".htm", ".bat"

# get a list of files grouped by extension
$files= dir $folder | Where-Object { -not $_.PSisContainer } |
Group-Object Extension

# limit files to only the types listed in $types:
$files = $files | Where-Object { $types -contains $_.Name }

# create a subfolder for each type if necessary
$files | ForEach-Object { New-Item -itemType Directory -path `
"$folder\$($_.Name)" -ea SilentlyContinue }

# move files into the appropriate subfolder
$files | ForEach-Object { $_.Group | Move-Item -destination `
"$folder$($_.Extension)\$($_.Name)" }

To clean up your downloads folder, simply change $folder to $folder = "$home\Downloads".


Posted Feb 27 2009, 08:00 AM by ps1

Comments

Brian Scott wrote re: Cleaning Document Folders
on 02-28-2009 3:18 AM

Thanks

I was looking for an example to do just this!

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.