Need help with Script to Backup Old Photos in Diff Local, Same File Structure RRS feed

  • Question

  • Hey all, I'm super new to Powershell so forgive me in advance.  I'm looking to reduce the size of my company server but don't want to delete any files and still need them accessible in an alternative backup. 

    I'd like to create a script that will scan a specified folder (has about 600k files of various types in a mess of subfolders) for a specific file type (in this case .jpg) of a certain age or older (date modified) and move those files to another location.  The kicker is I would like the folder structure to be recreated in the destination folder and to be able to run this manually on a monthly basis.


    Source folder ->         C:\Server\Active Projects\JOB NAME\JOB TYPE\FOLDER #1\PICS\Example photo.jpg

    Destination folder ->   D:\Server Backup\Active Projects\JOB NAME\JOB TYPE\FOLDER #1\PICS\Example photo.jpg

    If any of the sub folders already exist, it will simply create the folders that do not and move the files accordingly.  Running this the first time would move about 300k of photo files in the same folder structure into the Server Backup location.  Then moving forward I would like to be able to run this on a monthly basis by changing the Date Modified variable and it would simply move any files older than that date (much less than the initial 300k).

    I've got this so far from quite a bit of googling, though it doesn't take into account a file type.

    $oldPath = 'C:\Scripts'
    $newDrive = 'H:'
    $oldDate = Get-Date -Date 7/1/2019

    $oldFiles = Get-ChildItem $oldPath -Recurse | Where-Object { $_.lastwritetime -le $oldDate } 
    $oldDirs = $oldFiles | Split-Path
    $oldDirs = $oldDirs | select -Unique

    foreach ($oldDir in $oldDirs) {
        $strdir = $newDrive + ($oldDir | Split-Path -NoQualifier | Out-String).trim()
        if (!(Test-Path $strdir)) {
            Write-Host "$strdir does not exist. Creating directory..."
            mkdir $strdir | Out-Null
            } # end if
        } # end foreach

    foreach ($file in $oldFiles) {
        $strfile = $newDrive + ($file | Split-Path -NoQualifier | Out-String).trim()
        Write-Host "Moving $file to $strfile..."
        Move-Item $file -Destination $strfile -Force -WhatIf
        } # end foreach

    $oldfiles | select pspath | Split-Path -NoQualifier | Out-File c:\scripts\oldfiles.log

    Any help would be amazing! Thanks in advance :)

    Thursday, January 9, 2020 8:50 PM

All replies