@David-Baker-0 ,
The script I posted earlier was a little complex, because it was dealing with EXIF data of image files.
Since you are working with file metadata, the file object has that information readily available. One thing to consider with the file creation date in Windows, is this date can change. Seems odd, but true. The file creation date in Windows is based on when the file object was created. So if you copy a file, to Windows this will change the file creation date, since technically, the copy is a new object, just created. This is why you can have a last modified date earlier than the file creation date.
So the date you really want to use is the last modified date, or as PowerShell refers to it, the LastWriteTime. This isn't perfect, because it is updated when the file changes, but usually aligns better with when the file was created than the CreationTime property.
If you know the files are the originals, you could use the CreationTime property instead of the LastWriteTime. I would suggest trying it an see if you get the expected results. In the following script, simply replace LastWriteTime
with CreationTime
and see what happens. You could also change Move-Item
to Copy-Item
while testing, so you can repeat the test.
I've added some comments to the script to explain what each step is doing.
# Get a list of files to move
$files = Get-ChildItem -Path "D:\test" -Recurse
# Loop through the list of files
foreach ($file in $files) {
# Get the year the file was last modified
$yearCreated = ($file).LastWriteTime.Year
# Set the destination path
$rootPath = "D:\Archive\"
# Create a path based on the year
$yearDir = $rootpath + $yearCreated
# Check if the year directory already exists
If (-not(Test-Path $yearDir)){
# Create the year directory if it doesn't exist
New-Item -Path $yearDir -ItemType Directory
}
# Move the file to the year directory
Move-Item -Path $file.FullName -Destination $yearDir
}
PS Markdown doesn't do a good job rendering PowerShell, the last part of the script looks like one big comment. Once you copy and paste into PowerShell ISE or VSCode, it should be ok.
Mike Rodrick
Edutainer, ITProTV
**if the post above has answered the question, please mark the topic as solved.