I am trying to make a script that takes a list of file paths from a .txt file and check if the file is in the desired location, and if it isn't then send me an email. When I hardcode the file paths into the script to read from a variable it works. However when I read the file paths from a .txt file which contain date formatting it reads the filepath as if the formatting is a part of the path rather than script formatting to allow for a new date to be checked e.g. \\Serverlocation\foldername\Filename$(Get-Date -Format 'yyyyMMdd').csv
Below is the code I am using
$dateFormat = 'ddMMyyyy'
$1dayAgo = (Get-Date).AddDays(-1)
$FileList = '\\serverName\FileChecks\FileCheckList.txt'
#Example 1 of file with formatting \\Serverlocation\foldername\Filename$(Get-Date -Format 'yyyyMMdd').csv
#Example 2 of file with formatting \\linapp04\Applications\Data\LP_Holdings\LP_Holdings_$(Get-Date -Date $1dayAgo -Format $dateFormat).csv
$Checks = Get-content $FileList
$location = 'The following File has not been generated: '
$PSEmailServer
foreach ($Check in $Checks){
if(Test-Path -Path $Check -PathType Leaf ){
Write-Output "Truee"
}
else{
"The following File has not been generated " + $Check
Send-MailMessage -From '[email protected]' -To '[email protected]' -SmtpServer 'eu-smtp-outbound-1.mimecast.com'-port '25' -Subject "File Missing" -Body $location$Check
}
}
Please Let me know if there is something i'm missing or if this is not possible in powershell
Thanks
When reading fileCheckList.txt, each line is a string. So the path:
is a string, and:
will not be enumerated to a date. You would need to convert that string back into code, for example:
Perhaps instead, build the file path in the script. It's difficult to advise as I don't know for which file types you're searching. If they're all CSV files and follow the format of the example, then you could populate filechecklist.csv with filenames like:
And then build the file path to search on in your script:
If the files in filechecklist.csv have different extensions, you could put a placeholder in the file path and replace it when the script executes. So for a file path of: