How do I need to do to let this script only scan the last folder created

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 4 months, 1 week ago.

  • Author
  • #91334

    Sunny Days

    This script work, but it scans all the folders. I only want it to scan for new folder. The folder name are yyyymmdd 20180112

    param (
    [Parameter( Mandatory=$false)]

    [Parameter( Mandatory=$false)]

    [Parameter( Mandatory=$false)]

    [Parameter( Mandatory=$true)]

    [Parameter( Mandatory=$false)]
    #Declaring additional variables
    $EmailData = @()
    $ReportEmailSubject = "Great news. You received new wonder files from ABCD."
    $SMTPPass = $SMTPPass | ConvertTo-SecureString -AsPlainText -Force
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Pass

    # Modify these Email Settings

    $smtpsettings = @{
    To = "","","",""
    From = ""
    Subject = "$ReportEmailSubject – $date"
    SmtpServer = ""
    Credential = $cred

    Function New-ReportRequest {
    [Parameter( Position=0,Mandatory=$true)]

    [Parameter( Mandatory=$true)]
    if ($Data.count -gt '0') {
    #Generating HTML Report

    BODY{font-family: Arial; font-size: 8pt;}
    H1{font-size: 18px; color: #419944}
    H2{font-size: 14px;}
    H3{font-size: 12px;}
    TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt; margin-left: auto; margin-right: auto}
    TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}
    TD{border: 1px solid black; padding: 5px; }

    Report Created: $now
    Total Files Moved: $($Data.count)
    $htmlbody = $Data | Sort-Object UserName | ConvertTo-Html
    $htmltail = "


    $htmlreport = $htmlhead + $htmlbody + $htmltail

    Send-MailMessage @smtpsettings -Body $htmlreport -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -UseSsl

    $allFiles = Get-ChildItem -Path $SourcePath -Recurse -Include $SearchParameters

    foreach ($file in $allFiles) {
    if ($file.Extension -eq ".zip") {
    Expand-Archive -Path $file.FullName -DestinationPath "$SourcePath\Temp" -Force
    $subFiles = Get-ChildItem -Path "$SourcePath\Temp" -Include $SearchParameters -Recurse
    foreach ($sub in $subFiles) {
    Copy-Item -Path $sub.FullName -Destination $TargetPath -Force
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "SourceFile" -Value $($sub.FullName)
    $obj | Add-Member -MemberType NoteProperty -Name "TargetPath" -Value $TargetPath
    $EmailData += $obj
    } else {
    Copy-Item -Path $file.FullName -Destination $TargetPath -Force
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "SourceFile" -Value $($file.FullName)
    $obj | Add-Member -MemberType NoteProperty -Name "TargetPath" -Value $TargetPath
    $EmailData += $obj

    New-ReportRequest -Subject $ReportEmailSubject -Data $EmailData

  • #91436

    Don Jones

    Get-ChildItem -Path c:\path\to\containing\folder -Directory |
    Sort CreationTime -Descending |
    Select -First 1 |

    Should return the files in the most recently created folder.

You must be logged in to reply to this topic.