Have Pshell That Creates Directories By Date, But...

It's doing by the file "creation date" - which is not what I am looking for.   Would like to have file last updated timestamp used (i.e. when it was downloaded, not when file was created).

In this case I'd like all files (which are dropped into directory on March 7, 2017) to be copied to a destination directory with only one directory year/month/date of 2017/03/07.   With files that were "created" it's giving me folders with 2013, 2014, 2015, etc.

What is the Powershell function that would pull file download date vs. created date?
function doTheSort([string]$SourceDir, [string]$DestinationDir) {
    $files = get-childitem $SourceDir -Include @("*.txt","*.log", "*.gtm", "*.x340", "p*") 
    $files = get-childitem $SourceDir-Exclude @("Archive","BAI Files","ICMS Output","restore","TEST-ARCH")

    foreach ($file in $files)
    {
        $Directory = $DestinationDir + "" + $file.CreationTime.Date.ToString('yyyy') + "\" + $file.CreationTime.Date.ToString('MM-MMM') + "\" + $file.CreationTime.Date.ToString('dd')

        if (!(Test-Path $Directory))
        {
            New-Item $directory -type directory
        }
        Copy-Item $file.fullname $Directory 
    }
}

doTheSort "I:\Prior Day files for Upload" "C:\DIR-TEST\"

Open in new window

Michael O'HaraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Use LastWriteTime instead of CreationTime.

You set files twice, overwriting the first results by the second, btw.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.