Jungle Coder
musings of a third culture kid who codes *too* much
Just added post themes! Witness The Colors, and Exult!

PowerShell: Organizing Desktop Detrius into Dated Folders

I was reading an article written by a linux sysadmin about the trials of good file organization. After trying various hierarchical schemes, he settled on an interesting one: Storing files by Date, in folders. I’ve also just learned about Engineering Notebooks, where ideas are logged for a company, and dated by page.

I decided to try something similar with a pile of documents on my current laptop. Hence, the following PowerShell script, run over several file extensions typs (see *.jpg).

    
    # 2015 Andrew Owen
    # Permission is given for any use, no warranty provided or implied.

    # 
    # I use the abbreviations that ship with PowerShell on Windows 8.1, 
    # to make it easy to show how concise PowerShell can be, 
    # compareble to Perl, Tcl or ruby
    #

    # ls    -> Get-ChildItem
    # sort  -> Sort-Object
    # gu    -> Get-Unique
    # mkdir -> New-Item -ItemType Directory
    # mv    -> Move-Item

    # Move to your desktop
    cd "C:\Users\$([Environment]::UserName)\Desktop"

    # Create folders for each of the files of w/ the extension below.
    ls *.jpg | % {$_.LastWriteTime.ToString('M-yyyy')} | sort | gu | % { 
        mkdir ".\datedNoteBook\$_" 
    }

    # Move the files to the folders that they belong to.
    ls *.jpg | % {
        $subfolder = $_.LastWriteTime.ToString('M-yyyy');
        mv $_.FullName ".\datedNoteBook\$subfolder\$($_.Name)";
    }

Comments

Previously: Make sure you're able to run timer based services on demand
Next: Adventures in using Nginx as a reverse proxy