troubleshooting Question

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

Avatar of Michael O'Hara
Michael O'Hara asked on
Powershell
1 Comment1 Solution106 ViewsLast Modified:
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\"
ASKER CERTIFIED SOLUTION
Qlemo
"Batchelor", Developer and EE Topic Advisor
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros