Asked by:
Need help with Script to Backup Old Photos in Diff Local, Same File Structure

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.
Example:
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.logAny help would be amazing! Thanks in advance :)
Thursday, January 9, 2020 8:50 PM
All replies
-
Thursday, January 9, 2020 10:01 PM
-
Here is an example of how to rename and move fil;es.
$archiveFolder = 't:\myfiles\Archive' Get-ChildItem *.pdf -File | ForEach-Object{ $newname = '{0}_{1:yyyyMMdd}{2}' -f $_.BaseName,[datetime]::Today, $_.Extension $newname = Join-Path $archiveFolder $newname $_ | Rename-Item -NewName $newname -WhatIf }
\_(ツ)_/
Thursday, January 9, 2020 10:02 PM -
Also note that we will not design a full solution for you. The forum is for questions about a script you are writing and is not a free consulting forum.
Please carefully review the following links to set your expectation for posting in technical forums.
- This Forum is for Scripting Questions Rather than script requests
- How to ask questions in a technical forum
- How to post code in Technet Forums
\_(ツ)_/
Thursday, January 9, 2020 10:04 PM