Windows powershell file process duplication

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of venkatesh SQL venkatesh SQL 1 month, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #51393
    Profile photo of venkatesh SQL
    venkatesh SQL
    Participant

    My folder structure is "C:\Temp\SalesOrder\20160809\cc.txt" when its looking for all the files that need to be processed as soon as the recursion gets to folder "C:\Temp\SalesOrder" & "C:\Temp\SalesOrder\20160809" its processing cc.txt file. How do I rewrite the gci to not to traverse subfolders and only process files "C:\Temp\SalesOrder\20160809"

    $flatFilePath = "C:\Temp\Email.csv"
    $flatFile = Import-Csv -Path $flatFilePath -Header Dir,Email
    $emailID = @{}
    foreach ($record in $flatFile) { $emailID[$record.Dir]=$record.Email }

    # organization folder
    $orgFolder = Get-ChildItem -Path "C:\Temp\" -Recurse -Exclude *.*

    # Send email
    foreach ($dir in $orgFolder) {
    $processedfiles = $null
    $emailTo = $null

    Get-ChildItem -Path $dir.FullName -Recurse -Include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName } |
    Foreach-Object {
    [string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
    $processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";
    }

    $emailTo = $emailID.Get_Item($dir.Name) -split ";"

    if ($processedfiles) { Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort }

    }

    Problem area:

    Get-ChildItem -Path $dir.FullName -Recurse -Include *.txt,*.xls,*.csv,*.xlsx | ? { $_.FullName } |
    Foreach-Object {
    [string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
    $processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";
    }

    Thank you.

    #51410
    Profile photo of Anthony
    Anthony
    Participant

    So basically you just want it to process files and not folders? Or do you mean you only want it to go down a certain depth?

    Get-childitem -File will only return files
    Get-ChildItem -Recurse -Depth 2 will recurse but will only go a maximum of 2 folders deep

    Note: You need PS 3.0+ for -file I believe.

    • This reply was modified 1 month, 1 week ago by Profile photo of Anthony Anthony. Reason: Added note about PS version
    #51566
    Profile photo of venkatesh SQL
    venkatesh SQL
    Participant

    Hi Anthony, I want to process files only when the file that meets the criteria exists. For some reason even though I have just 1 file to process in all the folders put together it does process more than once and cannot figure out why? I have no hidden files either.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.