locked
powershell xcopy - trying to add MMDDYYYY on to the end of the CSV file name RRS feed

  • Question

  • Running a powershell script from a scheduled job in SSMS.

    1st 2 powershell xcopy commands work, but next one hangs indefinitely.

    What is wrong with

          powershell xcopy \\xxPD\xxBI\Pending\AA_Customer.csv        \\xxPD\xxxBI\Pending\AA_Customer$((Get-Date).ToString('MMddyyyy')).csv  /y /n /j

    ?



    • Edited by Tom Stone 98 Friday, November 8, 2019 8:35 PM clarification
    Friday, November 8, 2019 8:01 PM

Answers

  • This has noting to do with PowerShell and what yo have posted is still missing information. Again - DO NOT POST IMAGES. Copy the screen text in its entirety.

    Note that your image has a message that occurs BEFORE the line that is doing the copy.

    It appears that the copy succeeded as there is no error after the copy command.


    \_(ツ)_/


    • Edited by jrv Monday, November 11, 2019 9:46 PM
    • Marked as answer by jrv Monday, November 11, 2019 9:46 PM
    Friday, November 8, 2019 10:28 PM

All replies

  • Build your filename first and then add to a variable as such:

    $Date=Get-Date -Format "MM-dd-yyyy"
    
    $FileName="MyFileName-$Date.csv"

    Friday, November 8, 2019 8:34 PM
  • Friday, November 8, 2019 9:14 PM
  •  powershell {Copy-Item '\\xxPD\xxBI\Pending\AA_Customer.csv' "\\xxPD\xxxBI\Pending\AA_Customer$((Get-Date).ToString('MMddyyyy')).csv"}

    This is one of many places where using PS operators and expressions are critical tto understanding how to code someth9ing easily.

    $FileName = 'MyFileName-{0:yyMMdd}.csv' -f [datetime]::Today

    Based on the OP's example the correct answer to the question is to do this:

     powershell {Copy-Item '\\xxPD\xxBI\Pending\AA_Customer.csv' "\\xxPD\xxxBI\Pending\AA_Customer$((Get-Date).ToString('MMddyyyy')).csv"}
    No need for XCOPY in PowerShell to copy a single file.


    \_(ツ)_/

    • Marked as answer by jrv Friday, November 8, 2019 9:23 PM
    • Unmarked as answer by jrv Friday, November 8, 2019 9:23 PM
    Friday, November 8, 2019 9:23 PM
  • Friday, November 8, 2019 9:37 PM
  • Without you code this is not helpful.

    Please read the following: An image of code is not helpful

    Also review the following:


    \_(ツ)_/

    Friday, November 8, 2019 9:40 PM
  • Code:

    powershell {Copy-Item 'AA_Customer.csv' "AA_Customer$((Get-Date).ToString('MMddyyyy')).csv"}
    pause

    When the batch file is run the 1st thing displayed is the last image I sent.

    Friday, November 8, 2019 10:20 PM
  • This has noting to do with PowerShell and what yo have posted is still missing information. Again - DO NOT POST IMAGES. Copy the screen text in its entirety.

    Note that your image has a message that occurs BEFORE the line that is doing the copy.

    It appears that the copy succeeded as there is no error after the copy command.


    \_(ツ)_/


    • Edited by jrv Monday, November 11, 2019 9:46 PM
    • Marked as answer by jrv Monday, November 11, 2019 9:46 PM
    Friday, November 8, 2019 10:28 PM
  • Hi,

    Thanks for your reply.

    I think you can ignore it. Cmd.exe does not support UNC names as the current directory. 

    Maybe you can create a Mapped Network Drive to skip it.

    Also I think your script problem has been solved. If so, please try to mark as answer to help others with the same question quickly.

    Best regards,

    Lee


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

    Monday, November 11, 2019 7:36 AM
  • The script problem has not been solved.

    I am a newbie to Powershell, so I apologize for not communicating with clarity.

    I created a solution not involving Powershell.

    • Marked as answer by Tom Stone 98 Monday, November 11, 2019 9:43 PM
    • Unmarked as answer by jrv Monday, November 11, 2019 9:45 PM
    Monday, November 11, 2019 9:43 PM