locked
Afer Copy-tem, I want it to be the new path. RRS feed

  • Question

  • How to I make the Bold work. After a copy the file, I want that to be my NewPath

    #Path to the file $Path = "\\SERVER\Downloads\PositivePay.csv" $Date = get-date $NowDate = get-date -format yyyyMMdd #$NewPath = rename-item $Path -newname ("PositivePay-" + $NowDate + ".csv") $newPath = Get-ChildItem $Path | Copy-Item -Destination ("\\SERVER\Downloads\PositivePay-" + $NowDate + ".csv") Start-Sleep -s 5

    Get-ChildItem $Path | ForEach { $check = Import-Csv $_ If ($check) { $EMailBody = $Data | out-string $smtp = "XX.XX.XX.XX" $to = "Miguel Santillan <abc@email.com>" $from = "EPICOR9 <EPICOR9@email.com>" $subject = "CNB Positive Pay for " $subject += Get-Date -f MMddyyyy #Will insert a new line at each entry $body = "Attached is this weeks checks." #### Now send the email using \> Send-MailMessage send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high -Attachment $NewPath ########### End of Script################ } Else { return 0 } } If (Test-Path $Path){ Remove-Item $Path } If (Test-Path $NewPath){ Remove-Item $NewPath }


    Monday, August 21, 2017 5:14 PM

Answers

  • Each of the solutions gave me an error....

    What I did was place the bold within the email function.  JUst looked that the variable wasnt seen locally within the function

    #Path to the file
    $Path = "\\SERVER\Downloads\PositivePay.csv"
    
    $Date = get-date
    $NowDate = get-date -format yyyyMMdd
    
    
    #$NewPath = rename-item $Path -newname ("PositivePay-" + $NowDate + ".csv")
    Get-ChildItem $Path | Copy-Item -Destination ("\\SERVER\Downloads\PositivePay-" + $NowDate + ".csv")
    Start-Sleep -s 5
    
    #Check if file is empty
    Get-ChildItem $Path | ForEach {
    
        $check = Import-Csv $_
    
        If ($check) { 
    
    	
    $EMailBody = $Data | out-string
    
    $smtp = "XX.0.XX.XX"
    $to = "Miguel Santillan <msantillan@ABC.com>"
    $from = "EPICOR9 <EPICOR9@abc.com>"
    $subject = "CNB Positive Pay for " 
    $subject += Get-Date -f MMddyyyy
    #Will insert a new line at each entry
    $NewPath = "\\Server\Downloads\PositivePay-" + $NowDate + ".csv"
    $body =  "Attached is this weeks checks."
    
    #### Now send the email using \> Send-MailMessage 
    send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high -Attachment $NewPath
    ########### End of Script################
    
    				}
    
        Else { return 0 }
    
    }
    
    If (Test-Path $Path){
    	Remove-Item $Path
    }
    If (Test-Path $NewPath){
    	Remove-Item $NewPath
    }

    Friday, August 25, 2017 5:16 PM

All replies

  • $Path = '\\SERVER\Downloads\PositivePay.csv'
    $NewPath = '\\SERVER\Downloads\PositivePay-' + (Get-Date -Format 'yyyyMMdd') + '.csv'
    Copy-Item -Path $Path -Destination $NewPath
    ... is it that what you wanted?

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Monday, August 21, 2017 5:59 PM
  • Hi Miguel.

    The $Path variable includes both the full path to the file and the file itself: you could split it into two separate variables (say $Path for the path and $FileName for the file name) and combine them in a new variable in this way

    $FullPath = $Path + $FileName

    This variable will be used in the bold instruction as the source of the copy operation.
    Alternatively, you should extract the substring containing the path within the $Path variable (you should stop at the last "\" character).

    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Monday, August 21, 2017 6:10 PM
  • Very easy:

    $newPath = Copy-Item $path -Destination "\\SERVER\Downloads\PositivePay-$NowDate.csv" -PassThru
    $newPath.DirectoryName
    

    "PassThru" gets you the target file and "DirectoryName" gets the target folder.

    No need to use "Get-ChildItem" here.


    \_(ツ)_/


    • Edited by jrv Monday, August 21, 2017 6:25 PM
    Monday, August 21, 2017 6:24 PM
  • Gave me an error.  When it ran, it didnt copy the item

    Copy-Item : The network path was not found.
    At C:\temp\PositivePay3.ps1:7 char:10
    + Copy-Item <<<<  -Path $Path -Destination $NewPath
        + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
        + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

    Send-MailMessage : Could not find file '\\CCCAEPI01\Downloads\PositivePay-20170821.csv'.
    At C:\temp\PositivePay3.ps1:36 char:17
    + send-MailMessage <<<<  -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high
     -Attachment $NewPath
        + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
        + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage

    Monday, August 21, 2017 6:25 PM
  • Hi,

    Was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, August 25, 2017 12:44 PM
  • Each of the solutions gave me an error....

    What I did was place the bold within the email function.  JUst looked that the variable wasnt seen locally within the function

    #Path to the file
    $Path = "\\SERVER\Downloads\PositivePay.csv"
    
    $Date = get-date
    $NowDate = get-date -format yyyyMMdd
    
    
    #$NewPath = rename-item $Path -newname ("PositivePay-" + $NowDate + ".csv")
    Get-ChildItem $Path | Copy-Item -Destination ("\\SERVER\Downloads\PositivePay-" + $NowDate + ".csv")
    Start-Sleep -s 5
    
    #Check if file is empty
    Get-ChildItem $Path | ForEach {
    
        $check = Import-Csv $_
    
        If ($check) { 
    
    	
    $EMailBody = $Data | out-string
    
    $smtp = "XX.0.XX.XX"
    $to = "Miguel Santillan <msantillan@ABC.com>"
    $from = "EPICOR9 <EPICOR9@abc.com>"
    $subject = "CNB Positive Pay for " 
    $subject += Get-Date -f MMddyyyy
    #Will insert a new line at each entry
    $NewPath = "\\Server\Downloads\PositivePay-" + $NowDate + ".csv"
    $body =  "Attached is this weeks checks."
    
    #### Now send the email using \> Send-MailMessage 
    send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high -Attachment $NewPath
    ########### End of Script################
    
    				}
    
        Else { return 0 }
    
    }
    
    If (Test-Path $Path){
    	Remove-Item $Path
    }
    If (Test-Path $NewPath){
    	Remove-Item $NewPath
    }

    Friday, August 25, 2017 5:16 PM