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

Welcome Forums General PowerShell Q&A 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

 
Keymaster
10 months ago.

  • Author
    Posts
  • #91334

    Participant
    Points: 0
    Rank: Member

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

    [CmdletBinding()]
    param (
    [Parameter( Mandatory=$false)]
    [string]$SMTPUser="Outbound@yahoo.com",

    [Parameter( Mandatory=$false)]
    [string]$SourcePath="\\internal\ABC\Automation\FTPWorking\IncomingFiles\ABC",

    [Parameter( Mandatory=$false)]
    [string]$TargetPath="\\internal\ABC\DataAssets\Submissions\ABCD_RAPS\InboundClaimFlatFiles",

    [Parameter( Mandatory=$true)]
    [string[]]$SearchParameters,

    [Parameter( Mandatory=$false)]
    [string]$SMTPPass="Xova75672017"
    )
    #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 = "cohen.carryl@yahoo.com","cohen.carryl@gmail.com","cohen.carryl@hotmail.com","cohen.carryl@msn.com"
    From = "Outbound@great.com"
    Subject = "$ReportEmailSubject – $date"
    SmtpServer = "smtp.office365.com"
    Credential = $cred
    }

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

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

    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; }

    $Subject
    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

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

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

    Should return the files in the most recently created folder.

The topic ‘How do I need to do to let this script only scan the last folder created’ is closed to new replies.