0

Below script should check if files have been created today and send an email to one of our users.

The problem is that I can't send more than one value in the body. I need to remove "File" from body to get the email out. Also there is one email for each file. I would rewrite it to send one mail with all files listed with dates.

@echo off
setlocal enabledelayedexpansion
echo Files changed today %date%

FOR %%A IN (*.*) DO (
  set tf=%%~tA
  set fd=!tf:~0,10!
  if !fd!==%date% ( 
    powershell -ExecutionPolicy ByPass -Command Send-MailMessage ^
      -SmtpServer this.server.com ^
      -To [email protected] ^
      -From [email protected] ^
      -Subject Updated^
      -Body "%%A File"
  )
)

So i actually got this working with some heavy googeling (even went to page 3 at one point)

This is my "Check files in folder and send email with whats new and whats old"

@echo off
setlocal enabledelayedexpansion
echo Files changed today %date%
FOR %%A IN (*.*) DO (
  set tf=%%~tA
  set fd=!tf:~0,10!
  if !fd!==%date% ( 
      set "file=!file! <br> %%A !tf!"
    )
  if NOT !fd!==%date% ( 
      set "old=!old! <br> %%A !tf!"
    )
)
set "file=!file!
set "old=!old!

set "today=<b>New today:</b> %file%"
set "older=<b>Older files</b> %old%"
set "body=!today! <br><br> !older!"

powershell -ExecutionPolicy ByPass -Command Send-MailMessage -BodyAsHtml^
        -SmtpServer yourserver.com^
        -To [email protected] ^
        -From [email protected] ^
        -Subject Updated^
        -Body '!body!'
1
  • Drop batch and write the whole thing in PowerShell. Commented Aug 11, 2016 at 9:33

1 Answer 1

1

[Im]pure PowerShell solution without a batch file, save it to a file with .ps1 extension:

$modifiedToday = gci | ?{ $_.LastWriteTime.Date -eq (Get-Date).Date }
Send-MailMessage `
    -SmtpServer this.server.com `
    -To [email protected] `
    -From [email protected] `
    -Subject Updated `
    -Body ($modifiedToday -join "`n")

Impure because it uses abbreviated aliases like gci and ?{ instead of Get-ChildItem and Where-Object as I really don't like the verboseness of officially recommended PowerShell code style. As someone who doesn't mind writing cryptic batch-file code the OP might appreciate that as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.