Justin Swall
Justin Swall

Reputation: 1

Batch - Find and Move Folders Based on Folder Created Date

I'm trying to create a batch (or PowerShell) script that does the following:

  1. Gets the current date (i.e. 06/21/2018)
  2. Looks at all sub-folders in a specific folder (not recursively, just the immediate sub-folders) and finds all folders with a created date in the previous year, up to the current date in the previous year (i.e. 01/01/2017 - 06/21/2017).
  3. Moves all of those folders to a '2017 Jobs' folder.

So I've been searching around for an answer to this question but everything seems to be focused around file dates, not folder dates, so here we are. I know how to use Robocopy to move the folders once found, but all of it's switches are based around moving files older than X date, not folders. Any ideas on how I can achieve a folder-based created date looping lookup?

Upvotes: 0

Views: 595

Answers (1)

Cory Knutson
Cory Knutson

Reputation: 164

Without building the entire script for you, here are your pieces:

$date = Get-Date
$dateAYearAgo = $date.AddYears(-1)

$items = Get-ChildItem "C:\base\folder" | Where-Object {$_.CreationTime -gt $start -and $_.CreationTime -lt $end}
$items | Move-Item "C:\base\folder\2017 Jobs"

As for filtering out just folders, you can see if the version of powershell you are on allows Get-ChildItem C:\ -Directory to pull only directories, or you can use Get-ChildItem C:\ | Where-Object { $_.PSIsContainer }

Upvotes: 3

Related Questions