Script to check if zip file exist and send email

Tagged: 

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 1 month ago.

  • Author
    Posts
  • #20250
    Profile photo of Robin16
    Robin16
    Participant

    Hello,

    I'm trying to write a script to check for a file in a path and send an email. please see the script and let me know where i'm going wrong.

    $file1 = C:\test.txt

    #Email Settings
    $MailFrom = “xxxxx@xxxx.com”
    $Mailto = “xxxxxxx@xxx.com”
    $MailSubject = “Status of backup file”
    #$body = “The following files are missing:`r`n”
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    #Check for Existence of Files
    if (Test-Path $file1)

    {
    echo "File $file already exists" ;
    Send-MailMessage -to $Mailto -from $MailFrom -subject $MailSubject -body "$file1 exist on the path" -smtpServer xxxx.xxx.com
    }
    Else
    {

    #Send Email
    Send-MailMessage -to $Mailto -from $MailFrom -subject $MailSubject -body "$file1 does not exist on the path" -smtpServer xxxx.xxxxx.com

    }

    i'm getting the following error

    est-Path : Cannot bind argument to parameter 'Path' because it is null.
    At line:12 char:14
    + if (Test-Path <<<< $file1) + CategoryInfo : InvalidData: (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathComman Also, my file name changes with date like test1103 ( today ) test1104 ( tomorrow ). can anyone please help me on this ? Thanks in advance

  • #20251
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Hello,

    Try this to fix the Test-Path issue:

    $file1 = "C:\test.txt"

    /Alexander

  • #20253
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You need to place quotation marks (single or double, either will work) when assigning a string value to the $file1 variable:

    $file1 = C:\test.txt
    
    # Should be:
    
    $file1 = 'C:\test.txt'
    
  • #20254
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Your issue is the $file1 variable is not recognized as a string. The path should be enclosed in qoutes:

    PS C:\Users\rsimmers> $test = C:\Test\123.txt
    C:\Test\123.txt : The term 'C:\Test\123.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that 
    the path is correct and try again.
    At line:1 char:9
    + $test = C:\Test\123.txt
    +         ~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Test\123.txt:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
     
    
    PS C:\Users\rsimmers> $test = "C:\Test\123.txt"
    

    You can shorten this to single line, but I made it two so it's somewhat easier to read. This would produce an email body like "File Exists: True":

    $file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd") #Custom Date time format:  http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
    Send-MailMessage -to me@test.com -from donotreply@test.com -subject "File Checker" -body ("File Exists: {0}" -f (Test-Path -Path $file)) -smtpServer mailhost.susq.com
    
  • #20257
    Profile photo of Robin16
    Robin16
    Participant

    Thanks a lot everyone...

    it worked. but my filename changes with with date or time at the end of it... is there any way to find it ?

    file name will be same but numbers ( time or date ) changes
    e.g., backupfile1103.zip , backupfile1104.zip

    Rob, thanks for the code. Is there any way to find the file using strings or something like this ?

    like $filename = "backupfile%.zip"

    Thanks

  • #20259
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Yes, that is what I posted.

    $file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd")

    results in:

    PS C:\Users\rsimmers> "whatever{0}.zip" -f (Get-Date).ToString("MMdd")
    whatever1103.zip

  • #20260
    Profile photo of Robin16
    Robin16
    Participant

    Thanks again Rob..

    we don't know which date it will be so it's not safe to use mmdd.....i'm thinking to find the filename using extension ( because we will be having only one file with .zip extension )

    also, in the above reply you mentioned "$file = "whatever{0}.zip" -f (Get-Date).ToString("MMdd")" but $file already contains path right ? how to proceed with this ?

  • #20262
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You would use Get-ChildItem with a wildcard, something like this:

    $file = @(Get-ChildItem "C:\users\rsimmers\desktop\help*.zip" | Select FullName, LastWriteTime)
    
    if ($file.Count -gt 0) {
        $body = "Found {0} zip files: `r`n`r`n{1}" -f $file.Count, ("{0} - {1}" -f $file.FullName, $file.LastWriteTime)
    }
    else {
        $body = "No zip files found, rut roh raggy"
    }
    
    $body
    
    

    Depending on the version of Powershell, you would have to test formatting. If there is a possibility of multiple files, I would use HTML email and use ConvertTo-HTML to generate a table from the $file object.

  • #20266
    Profile photo of Robin16
    Robin16
    Participant

    Thanks a lot Rob for your help...

    i'm not getting the full output but it's finding the .zip file.

    btw can you tell me what is – ?

    code is some what stuck here

  • #20270
    Profile photo of Robin16
    Robin16
    Participant

    i'm seeing this output :

    Found 1 zip files:

  • #20271
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Our forums software is still HTML escaping some things in code blocks that are occasionally annoying. I think that particular sequence should just be a hyphen character.

You must be logged in to reply to this topic.