Forum Discussion

Fred_Elmendorf's avatar
Fred_Elmendorf
Brass Contributor
Dec 19, 2023
Solved

I need the latest file in each level 6 subfolder in this file structure

\\fileshare\Department\Device\Mfg\Location 1\Type1\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type2\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type3\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type4\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type5\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type6\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type7\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type8\somefile.ext
\\fileshare\Department\Device\Mfg\Location 1\Type9\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type1\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type2\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type3\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type4\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type5\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type6\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type7\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type8\somefile.ext
\\fileshare\Department\Device\Mfg\Location 2\Type9\somefile.ext
...

 

For this task I need the fully qualified name, file size, creation time, and write time, of the most recent file in each of the level 6 (Type*) folders.

 

Any assistance will be greatly appreciated!

  • Hi Fred_Elmendorf 

     

    Try this

     

    $LocationFolders = Get-ChildItem -Path \\fileshare\Department\Device\Mfg -Directory

    Foreach ($LocationFolder in $LocationFolders)
    {
    Write-Host "Working on Folder: $($LocationFolder.FullName)" -ForegroundColor Green
    $TypeFolders = Get-ChildItem -Path $LocationFolder.FullName -Directory
    Foreach ($TypeFolder in $TypeFolders)
    {
    Write-Host "Working on Folder: $($TypeFolder.FullName)" -ForegroundColor Green
    $Files = Get-ChildItem -Path $TypeFolder.FullName -File
    Write-Host "Files: $($Files.Count)"
    $File = $Files | Sort-Object LastWriteTime -Descending | select -First 1
    $File | fl FullName, Length, CreationTime, LastWriteTime
    }
    }

     

    Regards

    Andres

6 Replies

  • Andres-Bohren's avatar
    Andres-Bohren
    Steel Contributor

    Hi Fred_Elmendorf 

     

    Try this

     

    $LocationFolders = Get-ChildItem -Path \\fileshare\Department\Device\Mfg -Directory

    Foreach ($LocationFolder in $LocationFolders)
    {
    Write-Host "Working on Folder: $($LocationFolder.FullName)" -ForegroundColor Green
    $TypeFolders = Get-ChildItem -Path $LocationFolder.FullName -Directory
    Foreach ($TypeFolder in $TypeFolders)
    {
    Write-Host "Working on Folder: $($TypeFolder.FullName)" -ForegroundColor Green
    $Files = Get-ChildItem -Path $TypeFolder.FullName -File
    Write-Host "Files: $($Files.Count)"
    $File = $Files | Sort-Object LastWriteTime -Descending | select -First 1
    $File | fl FullName, Length, CreationTime, LastWriteTime
    }
    }

     

    Regards

    Andres

    • Fred_Elmendorf's avatar
      Fred_Elmendorf
      Brass Contributor
      Hi Andres,

      Your code worked beautifully, first try. Thank you!
      I need to get the output into a .csv file rather than the console, but I was trying to keep the problem as simple as possible to get the desired output. I'll work on that and may be able to pull from some other tasks I've already done. But if you have a solution for that, I'm sure your code will be more efficient than mine.

      Thanks!
      Fred
      • Andres-Bohren's avatar
        Andres-Bohren
        Steel Contributor

        Hi Fred_Elmendorf 

         

        I've extended the Script.

         

        $LocationFolders = Get-ChildItem -Path \\fileshare\Department\Device\Mfg -Directory
         
        #Create empty Array
        $Result = @()
         
        #Loop through Folders
        Foreach ($LocationFolder in $LocationFolders)
        {
        Write-Host "Working on Folder: $($LocationFolder.FullName)" -ForegroundColor Green
        $TypeFolders = Get-ChildItem -Path $LocationFolder.FullName -Directory
        Foreach ($TypeFolder in $TypeFolders)
        {
        Write-Host "Working on Folder: $($TypeFolder.FullName)" -ForegroundColor Green
        $Files = Get-ChildItem -Path $TypeFolder.FullName -File
        Write-Host "Files: $($Files.Count)"
        $File = $Files | Sort-Object LastWriteTime -Descending | select -First 1
        #$File | fl FullName, Length, CreationTime, LastWriteTime
         
        $FileObject = [PSCustomObject]@{
            FullName = $File.FullName
            Lenght = $File.Lenght
            CreationTime = $File.CreationTime
        LastWriteTime = $File.LastWriteTime
        }
        #Add FileObject to Array
        $Result += $FileObject
        }
        }
         
        #Export CSV
        $Result | Export-CSV -Path C:\Temp\FileStat.csv -NoTypeInformation -Encoding UTF8
         
        Kind Regards
        Andres