Sunday, February 26, 2012

PowerShell: Moving Files Based On File Extension

I wrote this Saturday night. I had to recover all files on my storage server back in December/January, but I still haven't stored through them yet. All of the files are unsorted in the \testdisk-6.13\ folder, and need to be moved to individual folders for each file type for easier sorting (the \-=RECOVERED_FILES=-\JPEG\ folder, \PDF\, \AVI\, etc). Pretty self explanatory script.

#  Define file paths:
$Path = "C:\Users\Ken\Desktop\\testdisk-6.13\"
$Dest = "C:\Users\Ken\Desktop\-=RECOVERED_FILES=-\JPEG\"

#  Counter
$fNum = 0

#  Start-Transcript records everything up until Stop-Transcript is used (saved to log)
Start-Transcript C:\jpeg_transcript.txt

# If folder defined in $Path exists, get a list of all children (files/folders) and move each one to the $Dest folder
if(Test-Path $Path)
        Get-ChildItem -Path $Path -Recurse -Include *.JPEG | Move-Item -Destination $Dest -Force
        "    "
        #  Too lazy to do any error handling here...
        #  mainly because there are no duplicate file names...
        #  and if there are dupes, they'll be overwritten by -force.
        #  Also, any problematic files will be in the original folder, 
        #  so I can deal with them manually.
        "*** *** Couldn't do a damned thing! *** ***"
        "    "
"Files moved: " + $fNum
"Files renamed: " + $fNum

No comments:

Post a Comment