在文件夹中查找特定大小的文件,将文件附加到电子邮件

问题描述 投票:0回答:1

我正在尝试创建脚本,该脚本将检查源文件夹是否为空以及大小是否匹配。然后它将在这些文件夹中查找具有特定字符串的文件,然后将这些文件发送给特定的收件人,然后将这些文件及其父文件夹移动到不同的目的地。

到目前为止我有这个:

$logDate = get-date -format yyyy-MM-dd
$log = "\\source\log\$logdate.log"

$mailkomu = "[email protected]"
$mailod = "[email protected]"
$subject = "A100 files"
$zprava = "Text"
$SMTPServer = "smtp.server"
 
$destination = "\\destination\"
$source = "\\source\"
$path = Get-ChildItem -Path $source -Recurse -File
$folders = Get-ChildItem -Path $source -Directory -Recurse
 
$PC1 = "A100"
 
# checks if the source folder is empty
If ((Get-ChildItem -Path $source -Force | Measure-Object).Count -eq 0) {
        Write-Output "$source - ok - no files" | Out-File $log -Append 
     }
Else {
    foreach ($folder in $folders) {

        # checks, if the folder size is greater than 15 MB
        $size = (Get-ChildItem -Path $folder.FullName -File -Recurse | Measure-Object -Property Length -Sum).Sum
        $sizeInMB = $size / 1MB
            if ($sizeinMB -gt 15) {
            Write-Output $folder "Folder size is greater than 15 MB" | Out-File $log -Append 
            }

            # the bottom part does not work if there is folder size check
            Else {foreach ($file in $folder){
                $SplitArray = $file.Name.Split("_")
                    foreach ($Splitfile in $SplitArray) {
                        if ($Splitfile -match $PC1) {
                            Write-Output $file | Out-File $log -Append

                            # This part is not working, I don't know how to attach files from $file to email
                            Send-MailMessage -From $mailod -To $email -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $file

                            # This part will not move parent folders and files in $file
                            Move-Item -path $file.FullName -destination $destination
                            }
                   }
            }
                     
        }
    }
}

源目标和文件如下所示:

\\source\A\XXX_A100_YYY.pdf
\\source\B\ZZZ_A100_FFF.pdf
\\source\C\XXX_A300_YYY.pdf
\\source\D\XXX_A200_YYY.csv
\\source\E\XXX_A300_YYY.pdf
\\source\F\XXX_A100_YYY.pdf

A、B、C... - 这些文件夹始终具有不同的名称。文件也是如此,只是字符串始终位于名称中的某个位置。

检查源是否为空可以正常工作。 文件夹大小检查工作正常。

  1. 问题- 目前,我不知道如何加载大小不大于15MB的文件夹中的文件,因此其余代码不起作用。

如果没有文件夹大小检查,则分割数组正确分割文件名。它会正确找到匹配。

  1. 和 3. 问题 - 如何将与拆分数组中的字符串匹配的文件附加到电子邮件,然后将它们及其父文件夹移动到不同的目的地。

搬家后应该是这样的。

\目的地\A\XXX_A100_YYY.pdf

\目的地\B\ZZZ_A100_FFF.pdf

\目的地\F\XXX_A100_YYY.pdf

你能帮我吗?

编辑

我编辑了脚本,现在它正在正确地移动文件及其文件夹。电子邮件的附加文件仍然存在问题。它仍然为每封电子邮件附加一个文件,而不是所有文件都附加到一封电子邮件。

If ((Get-ChildItem -Path $source -Force | Measure-Object).Count -eq 0) {
        Write-Output "$source - ok - no files" | Out-File $log -Append 
     }
Else {
    foreach ($folder in $folders) {

        # checks, if the folder size is greater than 15 MB
        $size = (Get-ChildItem -Path $folder.FullName -File -Recurse | Measure-Object -Property Length -Sum).Sum
        $sizeInMB = $size / 1MB
            if ($sizeinMB -gt 15) {
            Write-Output $folder "Folder size is greater than 15 MB" | Out-File $log -Append 
            continue
                }
        Else {
            Write-Output "folder is not empty"               
            $files = Get-ChildItem -Path $folder.FullName
            foreach ($file in $files) {
            $SplitArray = $file.Name.Split("_")
                foreach ($Splitfile in $SplitArray) {
                    if ($Splitfile -match $PC1) {
                        $attach = @(Get-ChildItem $file.fullname)
                        Write-Output $file
                        Send-MailMessage -From $mailod -To $mailkomu -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $attach
                        }
                    }
                }
            }
        }
}
 
Start-Sleep -seconds 5
 
If($source.Count -eq 0){
    }
Else {
    foreach($folder in $folders) {
            Write-Output "folder moved"
            # this part is for logging purpose
            $data = Get-ChildItem -Path $folder.fullname | Select-Object DirectoryName, Name 
            move-item -path $folder.fullname -destination $destination
            Write-Output "Complete"
            }
}
powershell size email-attachments
1个回答
0
投票

这是最终的脚本。它检查文件夹是否为空。如果不是,它会检查它们是否不大于 15 MB。如果不是,它将根据特定字符串检查文件。如果它找到了,它会将它们发送给收件人。然后它会等待 5 秒并将这些文件移动到目的地。

$logDate = get-date -format yyyy-MM-dd
$log = "\\source\log\$logdate.log"

$mailkomu = "[email protected]"
$mailod = "[email protected]"
$subject = "A100 files"
$zprava = "Text"
$SMTPServer = "smtp.server"
 
$destination = "\\destination\"
$source = "\\source\"
$path = Get-ChildItem -Path $source -Recurse -File
$folders = Get-ChildItem -Path $source -Directory -Recurse
 
$PC1 = "A100"
 
# checks if the source folder is empty
If ((Get-ChildItem -Path $source -Force | Measure-Object).Count -eq 0) {
        Write-Output "$source - ok - no files" | Out-File $log -Append 
     }
Else {
    foreach ($folder in $folders) {

        # checks, if the folder size is greater than 15 MB
        $size = (Get-ChildItem -Path $folder.FullName -File -Recurse | Measure-Object -Property Length -Sum).Sum
        $sizeInMB = $size / 1MB
            if ($sizeinMB -gt 15) {
            Write-Output $folder "Folder size is greater than 15 MB" | Out-File $log -Append 
            continue
                }
        Else {
            Write-Output "folder is not empty"               
            $files = Get-ChildItem -Path $folder.FullName
            foreach ($file in $files) {
            $SplitArray = $file.Name.Split("_")
                foreach ($Splitfile in $SplitArray) {
                    if ($Splitfile -match $PC1) {
                        $attach = Get-ChildItem $files.fullname
                        Write-Output $file
                        }
                    }
                }
            }
        }
}
Send-MailMessage -From $mailod -To $mailkomu -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $attach
 
Start-Sleep -seconds 5
 
If($source.Count -eq 0){
    }
Else {
    foreach($folder in $folders) {
            Write-Output "folder moved"
            # this part is for logging purpose
            $data = Get-ChildItem -Path $folder.fullname | Select-Object DirectoryName, Name 
            move-item -path $folder.fullname -destination $destination
            Write-Output "Complete"
            }
}
© www.soinside.com 2019 - 2024. All rights reserved.