Append MMYYYY to File to Copy

  • OK, not as clean as you may like, but just saw this. Here's what I'd do:

    $source="c:\fileloading" #location of starting directory
    $destination="c:\filecopy"; #location where files will be copied to
    $files="*dys_ihhist*" #files matching this pattern

    # write a powershell command to get a list of files in $source matching the $files pattern
    $a = get-childitem $source -filter $files
    $a | foreach {write-host $($_.basename)-$(get-date -f yyyyMMdd)$($_.extension)}

    # write a powershell command to copy files from source to destination appending the date to the filename
    $a | foreach {copy-item $_.fullname $destination\$($_.basename)-$(get-date -f yyyyMMdd)$($_.extension)}

    I don't see the need for $files to do more than be a string and I find -filter works well here. Then I added a debug line for you wiht the foreach{write-host} to see the list of files with the extension. Basename is the name without the extension, which you are modifying to add the date stamp. I added dd, because I expect possible collissions with yyyymm, but I could be wrong. Up to you.

     

  • For giggles, this is from Copilot:

    2023-07-31 15_08_04-● copyfiles.ps1 - sqlsatwebsite - Visual Studio Code

  • Thanks worked nice.

  • Good to hear.

Viewing 4 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply