none
File in use question from script

    Question



  •   When I run my script there is a piece after the email ... I used a comment to show line
      that always gets a file in USE or in use by another process. I want to after each file I process move it
    to an archive folder so I don't reprocess if it was successful. I would also like to rename the file before I move to archive to avoid duplicates(just in case)I have code commented out, but would like to get that installed along with
    figuring out why I get the file in use error. I'm creating an email piece and have another script that does the faxing.

     Thanks.


     Script.
    Function Write-Log($LogFile,$LogText){
    If($LogFile -match '_fax'){
    $LogEntry=@"
    $($LogText)
    "@
    }else{
    $LogEntry=@"
    $((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)
    "@
    }
    
    $LogEntry
    $LogEntry | Add-Content $LogFile     # I want this to be Shippernbr.txt
    }
    
    $writelogdir = 'c:\test'
    $rootfolder='c:\test'
    
    $files=Get-ChildItem "$rootfolder\*.csv"
    
    $def = "ggg@dcm.com"
    
    ForEach($file in $files){
        Write-Log "$writelogdir\files-processed.log" "Found files $file"
               
        Import-CSV $file -delimiter '|' | ForEach-Object{
            $attachment="$rootfolder\pdf\$($_.Shipper).pdf"
            $shipper = ($_.Shipper)
            $sub = 'Autosend for #' + ' ' + $shipper
                             
        If($_.email) {        
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "Attachment is $($attachment)"
             $email=$_.Email
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email address is $($email)"
            $emailCC=$_.EmailCC
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email CC address is $($emailCC)"
             $subject=$_.Shipper
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Autosend for # $($subject)"
            $from=$_.ReturnEmail
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address $($from)"        
        }
       Else
       {
            Write-Log "$writelogdir\files-processed.log" "No Email Address found in File--> $file"
       }        
       
            # Write a Fax Doc file if FaxNbr Not Blank
        If($_.FaxNbr) {    
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<adddoc:$attachment>"
             $Faxnbr = $_.Faxnbr
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOFAXNUM:$($_.FaxNbr)>"
             $Attn = $_.Attn
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TONAME:$($_.Attn)>"
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOCOMPANY: PACK:$($file)>"
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<FROMNAME: AUTO FAX>"
            }
    Else
     {
            Write-Log "$writelogdir\files-processed.log" "FaxNbr Not found in File--> $file"
     }        
     
     # Check if Return Email has valid data
     
        If($_.ReturnEmail){
         $from=$_.ReturnEmail
         }
       Else
         {
        $from=$def
         Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address Blank Using Default $($from)"        
         }    
        
          
      # Send Email
     
        #Send-MailMessage -from $from -To $email -Cc $emailCC -Subject $sub -attachments $attachment -SmtpServer 'local' -ErrorAction Continue
            if($?){
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
                Move-Item $file -Destination "$rootfolder\archive\"   #File in USE Message
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"
                }else{
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
            }
        gci "$writelogdir\*.csv" | % { rename-item –ath $_.Fullname –ewname ( $_.basename + (get-date -format ' dd-MM-yyyy') + $_.extension) -WhatIf
        
        }
      }
    }

    Saturday, June 22, 2013 6:32 PM

Answers

  • We posted at like the same time, so make sure you saw my last message.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Marked as answer by hart60 Tuesday, June 25, 2013 1:52 PM
    Tuesday, June 25, 2013 1:46 PM

All replies

  • The problem could most likely be with your attachment for email. You need to dispose of this object, before trying to move and/or rename the file, as the attachment still has a handle on the file. You can try the following function, I have not tested, so please test first, to make sure it actually creates and sends without issues, then implement it into your script:

    Function SendMail { param ( [string]$from, [string]$to, [string]$cc, [string]$sub, [string]$body, [string]$file, [string]$smtpServer ) # Create objects needed $msg = New-Object Net.Mail.MailMessage $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $att = New-Object Net.Mail.Attachment($file) # Populate objects $msg.From = $from $msg.To.Add($to)

    $msg.CC.Add($cc) $msg.Subject = $sub $msg.Body = $body $msg.Attachments.Add($att) # Send email $smtp.Send($msg) # Clean up objects $att.Dispose() $smtp.Dispose() $msg.Dispose() }

    Then just call it like so:

    SendMail "someone@nowhere.com" "someone@nowhere.com" "someone@nowhere.com" "This is a test email" "This is a test email, please review attachment" "C:\somfile.txt" "localhost"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by clayman2 Monday, June 24, 2013 3:15 AM update script
    Sunday, June 23, 2013 2:43 PM
  •  Can this be included or called from my current script. I will be processing multiple
    files and if email send complete then move (.csv) to the archive.

     Then on to next file in for each loop.

     Thanks for your response...

     

    Sunday, June 23, 2013 4:14 PM
  •  Not having any luck trying to implement into my current script.

    Sunday, June 23, 2013 10:51 PM
  •  Not having any luck trying to implement into my current script.


    Why not? What's happening when you try to use the function?
    Monday, June 24, 2013 12:01 AM

  •   I inserted the function into my script then added
    this piece. I used my variables created from script. I commented out
    the send-mailmessage. and substituted the line below.

     SendMail "$from" "$Email" "$emailCC" "$sub" "TEST" "$attachment" "local"

    "To" is a ReadOnly property.
    At C:\powershell\fax_packlist_pdf2.ps1:43 char:7
    +     $msg. <<<< To = $to
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyAssignmentException

    Exception calling "Send" with "1" argument(s): "Failure sending mail."
    At C:\powershell\fax_packlist_pdf2.ps1:50 char:12
    +     $smtp.Send <<<< ($msg)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

     
    Monday, June 24, 2013 1:40 AM
  • I updated the script, try the latest version.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 3:15 AM
  •  Did you attach ?

     Didn't see anything in post

      Thanks.

    Monday, June 24, 2013 9:58 AM
  •  Did you attach ?

     Didn't see anything in post

      Thanks.


    I modified the one above, rather than re-posting the whole thing

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 11:06 AM
  •   If CC is blank than it error's out..... It still has the issue of file in USE message.

     This is what send part looks like with new function added...

    # Send Email
     
       SendMail "$from" "$Email" "$emailCC" "$sub" "TEST" "$attachment" "local"
     
             if($?){
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
                Move-Item $file -Destination "$rootfolder\archive\"   #File in USE Message
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"
                }else{
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
            }

     THis was how I called email piece before.

     #Send-MailMessage -from $from -To $email -Cc $emailCC -Subject $sub -attachments $attachment -SmtpServer 'local' -ErrorAction Continue

     I added function code just before the For Each logic.

      Thanks again.

     

     

    Monday, June 24, 2013 12:10 PM
  •  DO I need to re-design how script works?

      It needs to send those emails than if success move the processed files to an archive so
    no chance to re-send.  There can be 20-25 files every hour than it will process. I'm also
    trying to keep a log file for each iteration of the For Each files piece. That way I have an
    audit of the process.

     Thanks to all responses!!!!!

    Monday, June 24, 2013 2:19 PM
  • So what you are trying to do is:

    Get a list of files from a directory, move those files to an archive folder and re-name them just in case there will be duplicates.

    Log everything that is going on while the script is running.

    Once script is done, send an email, only on success, or do you need to know what failed and what is being attached to the email?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 2:52 PM


  •  I need to send email while processing current file found then on success of email move to archive. Then
    process next file from folder... There will always be an attachment file sent with email.

      I was thinking maybe needed to rename just in case I happened to get a duplicate file at some point.

     Log everything that is going on while the script is running. -- yep.

     Thanks again!!!

     
    Monday, June 24, 2013 2:57 PM
  • Just curious as to why send an email for every file processed, and not just process all files, then send an email wth a detailed report of what was moved and renamed?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 3:09 PM
  •  Each file as a specific attachment and didn't know how to keep everything in sync other than
    as I loop thru folder pull everything needed to email then send. If I did all the processing(move\rename)
    still need away to send separate emails for file processed with given attachment. Inside each file(csv) there is the corresponding file needed for attachment.

    If($_.email) {        
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "Attachment is $($attachment)"
             $email=$_.Email


     Thanks!!!

    Monday, June 24, 2013 3:23 PM
  • OK, I see, and you said that after you added the SendMail function to your script, and calling that rather than Send-MailMessage, you still receive file in use error?

    The files that are being moved, can users get to that folder and open those files?

    Also, not sure if you know, but Move-Item not only moves files and/or folders, but it can rename them also.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 3:47 PM
  •  They won't have access to the Archive... it would be a manual process to re-process a
    file that's why I want to log all activities of each file in folder process.

      Also, not sure if you know, but Move-Item not only moves files and/or folders, but it can rename them also.

     I didn't know that ... thanks ...I have been learning as I go with PS and these scripts that get posted
    have been a huge help in that process.

     Did I insert the Function correctly?

     Thanks again.

    Monday, June 24, 2013 5:00 PM
  • So I tested with the code below. I first commented out the Clean up section of the SendMail function so that no dispose methods are called. When I did, I received the error message, that you are getting about it being used by antoher user or porcess. I then uncommented out the clean up section, so the dispose methods are called, once I did that, the script worked as expected.

    Function SendMail
    {
    	param
    	(
    		[string]$from,
    		[string]$to,
    		[string]$cc,
    		[string]$sub,
    		[string]$body,
    		[string]$file,
    		[string]$smtpServer
    	)
    	
    	# Create objects needed
    	$msg = New-Object Net.Mail.MailMessage
    	$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    	$att = New-Object Net.Mail.Attachment($file)
    	
    	# Populate objects
    	$msg.From = $from
    	$msg.To.Add($to)
    	If ($cc){ $msg.CC.Add($cc) }
    	$msg.Subject = $sub
    	$msg.Body = $body
    	$msg.Attachments.Add($att)
    	
    	# Send email
    	$smtp.Send($msg)
    	
    	# Clean up objects
    	$att.Dispose()
    	$smtp.Dispose()
    	$msg.Dispose()
    }
    $files = Get-ChildItem "F:\Testing\*.txt"
    ForEach ($file in $files)
    {
    	SendMail "user@domain.com" "user@domain.com" $null "Testing" "Testing email attachments" $file.FullName "mailserver.domain.com"
    	Move-Item -Path $file -Destination "$($file.Directory)\A\$($file.BaseName)-$(Get-Date -f MM-dd-yyyy-HHmmss)$($file.Extension)"
    	Sleep 2
    }

    Try this modified version of your script:

    Function SendMail
    {
    	param
    	(
    		[string]$from,
    		[string]$to,
    		[string]$cc,
    		[string]$sub,
    		[string]$body,
    		[string]$file,
    		[string]$smtpServer
    	)
    	
    	# Create objects needed
    	$msg = New-Object Net.Mail.MailMessage
    	$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    	$att = New-Object Net.Mail.Attachment($file)
    	
    	# Populate objects
    	$msg.From = $from
    	$msg.To.Add($to)
    	If ($cc){ $msg.CC.Add($cc) }
    	$msg.Subject = $sub
    	$msg.Body = $body
    	$msg.Attachments.Add($att)
    	
    	# Send email
    	$smtp.Send($msg)
    	
    	# Clean up objects
    	$att.Dispose()
    	$smtp.Dispose()
    	$msg.Dispose()
    }
    Function Write-Log($LogFile,$LogText)
    {
    If($LogFile -match '_fax'){
    $LogEntry=@"
    $($LogText)
    "@
    }else{
    $LogEntry=@"
    $((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)
    "@
    }
    $LogEntry
    $LogEntry | Add-Content $LogFile     # I want this to be Shippernbr.txt
    }
    $writelogdir = 'c:\test'
    $rootfolder='c:\test'
    $files=Get-ChildItem "$rootfolder\*.csv"
    $def = "ggg@dcm.com"
    ForEach($file in $files){
        Write-Log "$writelogdir\files-processed.log" "Found files $file"
               
        Import-CSV $file -delimiter '|' | ForEach-Object{
            $attachment="$rootfolder\pdf\$($_.Shipper).pdf"
            $shipper = ($_.Shipper)
            $sub = 'Autosend for #' + ' ' + $shipper
                             
        If($_.email) {        
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "Attachment is $($attachment)"
             $email=$_.Email
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email address is $($email)"
            $emailCC=$_.EmailCC
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email CC address is $($emailCC)"
             $subject=$_.Shipper
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Autosend for # $($subject)"
            $from=$_.ReturnEmail
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address $($from)"        
        }
       Else
       {
            Write-Log "$writelogdir\files-processed.log" "No Email Address found in File--> $file"
       }        
       
            # Write a Fax Doc file if FaxNbr Not Blank
        If($_.FaxNbr) {    
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<adddoc:$attachment>"
             $Faxnbr = $_.Faxnbr
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOFAXNUM:$($_.FaxNbr)>"
             $Attn = $_.Attn
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TONAME:$($_.Attn)>"
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOCOMPANY: PACK:$($file)>"
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<FROMNAME: AUTO FAX>"
            }
    Else
     {
            Write-Log "$writelogdir\files-processed.log" "FaxNbr Not found in File--> $file"
     }        
     
     # Check if Return Email has valid data
     
        If($_.ReturnEmail){
         $from=$_.ReturnEmail
         }
       Else
         {
        $from=$def
         Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address Blank Using Default $($from)"        
         }    
        
          
      # Send Email
     
        #Send-MailMessage -from $from -To $email -Cc $emailCC -Subject $sub -attachments $attachment -SmtpServer 'local' -ErrorAction Continue
    	$body = "Test email"
    	SendMail $from $email $emailCC $sub $body $attachment 'localhost'
            if($?){
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
                Move-Item $file -Destination "$rootfolder\archive\($_.basename + (get-date -format ' dd-MM-yyyy') + $_.extension)"   #File in USE Message
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"
                }else{
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
            }
        }
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 5:17 PM
  •  It did everything except the Move-Item for the rename and move to Archive.

     Move-Item : The specified path, file name, or both are too long. The fully qualifie
    d file name must be less than 260 characters, and the directory name must be less t
    han 248 characters.
    111 char:22
    +             Move-Item <<<<  $file -Destination "$rootfolder\archive\($_.basename
    + (get-date -format ' dd-MM-yyyy') + $_.extension)"   #File in USE Message
        + CategoryInfo          : WriteError: (C:\test\ans_CS13088321.csv:String) [Mov
       e-Item], PathTooLongException
        + FullyQualifiedErrorId : MoveItemIOError,Microsoft.PowerShell.Commands.MoveIt
       emCommand

     Cool ...

     Thanks.

    Monday, June 24, 2013 6:21 PM
  • I do not see anything out of the ordinary in the code that would make the filename and/or path more than 256 characters. After processing, you are moving the csv file to the archive or the pdf attachments?

    From what I see it is the csv file. Are any of these file names long?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 6:35 PM
  •  Just the processed(csv)... There is an underscore in the file name.  They will all
    be 14 postions in length including under_score and excluding extension.

     It shows in the error the filename.
    WriteError: (C:\test\ans_CS13088321.csv:String)

       The folder I have being using to TEST is called TEST. Then a sub dir of archive.

     Doesn't seem like 256 chars would come into play.

     I thought I could do a -Whatif but fails prior to executing.

     Thanks.

    Monday, June 24, 2013 6:45 PM
  • Modify your code by replacing:

    if($?){
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
                Move-Item $file -Destination "$rootfolder\archive\($_.basename + (get-date -format ' dd-MM-yyyy') + $_.extension)"   #File in USE Message
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"
                }else{
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
            }

    With this:

    if($?){
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
    			$newFileName = "$($_.basename)-$(get-date -format dd-MM-yyyy)$($_.extension)"
                Move-Item $file -Destination "$rootfolder\archive\$newFileName"   #File in USE Message
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\ as $newFileName"
                }else{
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
            }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by clayman2 Monday, June 24, 2013 6:55 PM Edit
    Monday, June 24, 2013 6:53 PM
  •  The new version went back to file in use message. In the log of move\rename...

     Moved file C:\test\ans_CS13088321.csv to c:\test\archive\ as -24-06-2013

     If that is helpful.

     Thanks.

    Monday, June 24, 2013 7:08 PM
  • The log file looks good, except for some reason the $_.BaseName isnt in the new name...hmmm

    OK, now changing my train of thought, because I believe the issue isn't with the email. The issue most likely has to do with the Import-CSV. You Imported the csv file, and then you are trying to move that file, the start of the next file, so I believe there is still a lock on the file. I will have to do some testing.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 7:17 PM
  • So I just verified, your issue is with the Import-Csv statement, then piping over to the ForEach-Object cmdlet. Seeing that you are looping through, there is a handle on the csv file, and you are trying to move the file, before the handle is released.

    I tested with this simple script:

    Import-Csv F:\Testing\testing.csv | ForEach-Object {
    	Write-Host $_
    	Move-Item F:\Testing\testing.csv F:\Testing\A\
    }

    I have a heading and two rows in the csv file. When I process it like so, I receive the dreaded error you are getting:

    Move-Item : The process cannot access the file because it is being used by another process.

    I will have to try and go through your script, to find another way to write it, so that way it will work how you intend it to.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 7:38 PM
  •  Thanks for the update... I used that method because there where other checks needed done
    and didn't know how to process all files in folder.

     I need to process single files from folder(loop) and send separate emails based upon
    data found in csv(shipper). I need conformation of success and need to store those files
    in archive to a given time frame. The audit log helps in case failure or validating process
    working.

     Thanks for all your help...

    Monday, June 24, 2013 7:53 PM
  • So I stripped my email function out, as that was not the cause. I moved your Move-Item out of the ForEach-Object loop and down to the bottom of the ForEach loop, as once you are done processing the csv file, you will move out of the foreach-object loop, back to the foreach loop, so that should work now

    The only issue I had was with your one log line after the Move-Item in your original script:

    Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"

    When I read that, I take it as the processing of the csv file is completed, but that isn't the case as it is in the loop you are processing the csv file, so there are more entries in the csv file to still be processed. Anyways, I left it there so it should still work. Here is the script:

    Function Write-Log($LogFile,$LogText)
    {
    If($LogFile -match '_fax'){
    $LogEntry=@"
    $($LogText)
    "@
    }else{
    $LogEntry=@"
    $((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)
    "@
    }
    $LogEntry
    $LogEntry | Add-Content $LogFile     # I want this to be Shippernbr.txt
    }
    $writelogdir = 'c:\test'
    $rootfolder='c:\test'
    $files=Get-ChildItem "$rootfolder\*.csv"
    $def = "ggg@dcm.com"
    ForEach($file in $files){
        Write-Log "$writelogdir\files-processed.log" "Found files $file"
               
        Import-CSV $file -delimiter '|' | ForEach-Object{
            $attachment="$rootfolder\pdf\$($_.Shipper).pdf"
            $shipper = ($_.Shipper)
            $sub = 'Autosend for #' + ' ' + $shipper
                             
        If($_.email) {        
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "Attachment is $($attachment)"
             $email=$_.Email
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email address is $($email)"
            $emailCC=$_.EmailCC
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email CC address is $($emailCC)"
             $subject=$_.Shipper
            Write-Log "$writelogdir\$($_.Shipper)_email.log" "Autosend for # $($subject)"
            $from=$_.ReturnEmail
             Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address $($from)"        
        }
       Else
       {
            Write-Log "$writelogdir\files-processed.log" "No Email Address found in File--> $file"
       }        
       
            # Write a Fax Doc file if FaxNbr Not Blank
        If($_.FaxNbr) {    
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<adddoc:$attachment>"
             $Faxnbr = $_.Faxnbr
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOFAXNUM:$($_.FaxNbr)>"
             $Attn = $_.Attn
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TONAME:$($_.Attn)>"
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<TOCOMPANY: PACK:$($file)>"
            Write-Log "$writelogdir\$($_.Shipper)_fax.log" "<FROMNAME: AUTO FAX>"
            }
    Else
     {
            Write-Log "$writelogdir\files-processed.log" "FaxNbr Not found in File--> $file"
     }        
     
     # Check if Return Email has valid data
     
        If($_.ReturnEmail){
         $from=$_.ReturnEmail
         }
       Else
         {
        $from=$def
         Write-Log "$writelogdir\$($_.Shipper)_email.log" "From Address Blank Using Default $($from)"        
         }    
        
          
      # Send Email
     
        Send-MailMessage -from $from -To $email -Cc $emailCC -Subject $sub -attachments $attachment -SmtpServer 'local' -ErrorAction Continue
            if($?){
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Sent Successfully"
                
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Moved file $($file) to $rootfolder\archive\"
                }else{
                Write-Log "$writelogdir\$($_.Shipper)_email.log" "Email Send failed: $($error[0])";break
            }
        }
    	
    	# Done processing csv file, move to archive
    	Move-Item $file -Destination "$rootfolder\archive\$($file.BaseName)-$(Get-Date -f dd-MM-yyyy)$($file.Extension)"   #File in USE Message
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, June 24, 2013 8:06 PM
  •  It still doesn't like the move to archive.

      Move-Item : Could not find a part of the path.
    At \\dopey\Powershell\test_email_fax_send.ps1:93 char:11
    +     Move-Item <<<<  $file -Destination "$rootfolder\archive\$($file.BaseName)-$(G
    et-Date -f dd-MM-yyyy)$($file.Extension)"   #File in USE Message

    Tuesday, June 25, 2013 2:24 AM
  •  Output from Whatif

    What if: Performing operation "Move File" on Target "Item: C:\test\ans_CS13088321.csv Destination: C:\test\archive\ans_CS13088321-24-06-2013.csv".

     

    Tuesday, June 25, 2013 2:28 AM
  •  Output from Whatif

    What if: Performing operation "Move File" on Target "Item: C:\test\ans_CS13088321.csv Destination: C:\test\archive\ans_CS13088321-24-06-2013.csv".

    Well it looks like the WhatIf is specifying the correct path. The issue may be because you are running the script from a server, as indicated in the error:

    Move-Item : Could not find a part of the path.
    At \\dopey\Powershell\test_email_fax_send.ps1:93 char:11

    The path is a UNC path, so first, try copying the script to your local machine, which has the files your testing with, and try again, do you still get the error?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, June 25, 2013 12:48 PM
  •  Same error... when executed locally

     Move-Item : Could not find a part of the path.
    At C:\powershell\test_email_fax_send.ps1:94 char:11

     Using Whatif

    What if: Performing operation "Move File" on Target "Item: C:\test\ans_CS13088321.csv Destination: C:\test\archive\ans_CS13088321-25-06-2013.csv".

    Tuesday, June 25, 2013 1:00 PM
  • C:\test\ans_CS13088321.csv does exist on your machine? The paths and everything looks correct?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, June 25, 2013 1:24 PM
  •  Paths exist along with file.

    Tuesday, June 25, 2013 1:41 PM
  • OK, I think I may have figured it out. The error is most likely due to the Archive folder not being there. Move-Item will not create a directory if it does not exist. So you can either make sure the archive directory exists or add the following to your code, right above the Move-Item statement:

    If (!(Test-Path "$rootFolder\archive"))
    {
      New-Item -ItemType Folder "$rootFolder\archive" | Out-Null
    }
    What that is doing, is checking to see if that directory exists, and if not it creats it, and because New-Item sends data to the console, I pipe it to Out-Null to supress the message of the directory being created.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, June 25, 2013 1:42 PM
  •  I manually copied file to c:\test\archive just to make sure no security or anything.

    Tuesday, June 25, 2013 1:42 PM
  • We posted at like the same time, so make sure you saw my last message.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Marked as answer by hart60 Tuesday, June 25, 2013 1:52 PM
    Tuesday, June 25, 2013 1:46 PM
  •   It ended up being a CASE on the folder called Archive versus archive.  I didn't
    think case would be an issue with Windows....

      It works like a champ....

      Thanks again for all the POSTS...

    Tuesday, June 25, 2013 1:52 PM
  •   It ended up being a CASE on the folder called Archive versus archive.  I didn't
    think case would be an issue with Windows....

      It works like a champ....

      Thanks again for all the POSTS...

    That is strange, case shouldn't matter. I even tested with:

    Move-Item F:\Testing\test.txt F:\Testing\Archive\testrename.txt
    That still worked, even though the folder Archive, is actually archive. As long as you got it to work, than all is good. You are welcome, it is my pleasure to help when and where I can.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, June 25, 2013 1:57 PM