【问题标题】:How kan Send a file with mail If there is the new file(today's date) in the directory? [closed]如何用邮件发送文件 如果目录中有新文件(今天的日期)? [关闭]
【发布时间】:2019-05-15 18:49:11
【问题描述】:

如果有一个带有今天日期的新文件(文件名:Orbit2Spor_201812140200.csv),我将有一个 powershell 脚本来查看目录,然后通过邮件将其发送到我的邮件地址! 不知道怎么查看今天到的一个目录下的文件,所以没写foreach在这里问问题?

     `# Mail parametrar.
      $PSEmailServer = "smtpmail.test.com"
      $MailFrom = "am.b@Yahoo.com"
      $MailTo = "am.b@Yahoo.com"
      $Subject = "Larm!"
      $MBody = "Test"
      $Attachment = $File

  #################################################################

       $FolderSpor = "C:\temp\CSV"
       $date = (get-date).date
       if (test-path $FolderSpor)
  {
         $files = Get-ChildItem -path $FolderSpor
        foreach ($File in $Files){
       if(((Get-Item $File).CreationTime) -gt $date)

     {
       Send-MailMessage -To $MailTo -From $MailFrom -Subject $Subject - 
       Body $MBody -Attachments $Attachment
 }
 }
 }` 

【问题讨论】:

  • 这篇文章不是当前形式的问题。这是半个订单或半个声明,我不太确定。请编辑您的原始帖子,以包含您尝试运行的完整代码,并详细说明它如何无法按您预期的方式运行。
  • 您似乎不仅缺少一个问题,而且您似乎还缺少部分代码。 foreach 之后是什么?
  • $TodaysFiles = gci ("\\server1\C$\folder1\CSV\Orbit2Spor_{0}*.csv" -f [datetime]::Today.ToString('yyyyMMdd')) 然后检查 .count 属性。顺便说一句,我会使用 splatting 作为邮件参数
  • 不知道怎么查看今天到的一个目录下的文件,所以没有写foreach在这里问问题?

标签: powershell email


【解决方案1】:

我为您编写了简单的脚本,该脚本将获取文件夹中的所有文件,并会按照您的要求检查今天是否创建了任何文件。我假设您知道如何处理您找到的文件。

## Your folder Path
$folderpath = "C:\temp"
## Getting today date (only date with no time)
$date = (get-date).Date

## Checking if the path is reachable 
if (Test-Path $folderpath)
   {
    ## getting all the files in the folder
    $files = Get-ChildItem $folderpath
    ## Running a for each loop for all the files
    foreach ($file in $files)
          {
           ## checking if the file creation time is today
           if(((Get-Item $file).CreationTime) -gt $date)
             {
               ## do what you want here
             }
          }
    }   

希望对您有所帮助,如果您还有其他需要,请告诉我。

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 2011-08-26
    • 2021-09-12
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多