Powershell

Today I had something that was just BUGGING me.   Like many people I have a collection of Music.   And like many, that folder structure justs SITS for the most part unmaintained.  The only maintenance being whatever the music software did.

I started going through the pile and found I had a series of folders that no longer contained MP3 files (I deleted it from the collection at one point with the supplied software).  However it left behind .INI, Thumbs.DB or .JPG files

Well I wasn’t worried about the JPG files in the music folder collection since most of the time they would redownload from the source site.  So I decided to get thiings clearned up ONCE and FOR ALL without spending $$$.

So I used Powershell 🙂

GET-CHILDITEM FolderName –recurse –include *.EXT | REMOVE-ITEM

Will easily remove all offending items (like say .TMP files?) but certain file types are automatically hidden by the O/S.  Rather than editing my default folder settings globally, I can tack on a –force which “FORCES” the Commandlet to show me everything.

So phase one.  REmove all those .JPG files in the Music Collection.

GET-CHILDITEM C:Music –recurse –include *.jpg,Desktop.ini,thumbs.db –force

Once I could see it was pulling up what I wanted successfully I patched on REMOVE-ITEM.  Now since I trust MYSELF but NOT “Murphy’s Law” I tacked on a –whatif just to be safe.  Also since many of the items I was going to removewere hidden, I have to tack on an additional –force to make sure REMOVE-ITEM is allowed to see them. 

If you don’t, you will get an error about lack of access, security denied, cows falling from the sky, blah blah blah….

GET-CHILDITEM C:Music –recurse –include *.jpg,Desktop.ini,thumbs.db –force | REMOVE-ITEM –force -whatif

So now once I can see it will remove all the correct data I remove the –whatif and the let the command do it’s work in Powershell.  Now a new problem surfaces.  I have folders that are now empty wasting space.

Again in Powershell this is not a problem.  For within the Objects for Filesystem are Methods to see how many files OR Directories are within a folder.  So by adding in a .GETFILES().COUNT or GETDIRECTORIES().COUNT to a particular object I can see, literally if it’s empty!

So I make second script than can remove the dead folders

GET-CHILDITEM C:Music –recurse –force | where { $-.PSISContainer } | foreach { IF { $-.Getfiles().Count –eq 0 –and $-.GetDirectories.Count –eq 0) { REMOVE-ITEM $- }}

Now the tricky part.   Since we know each time we clean this up, we may have NEW empty folders.  so we’ll have to repeat this process until we have no empty ones.  So we’ll need to add in a counter to track how many empty folders.  So at this point, let’s just make a script so it’s all viewable and clean.

————— Begin Cleanup Script ——————————

Do {

# Get of all the directories in the Target tree, ONLY the Directories

$LIST=GET-CHILDITEM C:Music –recurse –force | where { $-.PSISContainer }

# We Flag the $DONE variable.  If even ONE folder is removed (Empty)
# It will flag as $FALSE

$DONE=$TRUE

foreach ($Directory in $LIST) {

# Check the count of folders and Directories in THIS directory
# If it’s a total of 0 for both, we remove the directory and flag at least ONE $FALSE
#
# If no Blank folders ever get found, $DONE will be $TRUE and the loop will exit
#

IF { $Directory.Getfiles().Count –eq 0 –and $Directory.GetDirectories.Count –eq 0)
     { REMOVE-ITEM $Directory
     $DONE=$FALSE
}
}

} until ($DONE –eq $TRUE)

—————– End Cleanup Script ——————————

So now what do we have?  A simple way to go through a list of folders and purge blanks.  You could tweak this script any way that suits you as well,  tacking in Date/Time filters.  Even prompting where to start.

Remember, the Power of Shell is in YOU

Sean
the Energized Tech