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

Michael O'Hara
Michael O'Hara used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Use LastWriteTime instead of CreationTime.

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial