locked
Delete End Of File Name RRS feed

  • Question

  • I recently created a Powershell script to add data to the end of invoice names that would add the phrase " - DOC #"

    So an example would be 2017-05-25 INV 123456 and my script would make it 2017-05-25 INV 123456 - DOC 1

    I am doing this for multiple invoices in a folder and they get numbered DOC 1-however many invoices there are. So if there are 20 invoices in a folder they would be DOC 1 - DOC 20.

    Here is the script I am using:

    $nr = 1
    Dir | %{Rename-Item $_ -NewName ($_.BaseName + ' - DOC {0}' -f $nr++ + $_.Extension)}
    ls

    I now need to create a script to mass delete the the characters I have added from the end of the file name. But I want to keep the extension. Can someone help me with a script for this?

    Thanks

    Thursday, May 25, 2017 2:50 PM

All replies

  • You know how to rename the basename of a file. Now you just need to remove what you've added before ... like this:

    'OriginalFileName - DOC 1' -replace '\s+\-\s+DOC\s+\d*'

    Some general tips:

    Please use the code formatting tool here in the forum and try to avoid using aliasses. 


    Grüße - Best regards

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


    • Edited by BOfH-666 Thursday, May 25, 2017 7:34 PM
    • Proposed as answer by Luc Fullenwarth Thursday, May 25, 2017 7:56 PM
    Thursday, May 25, 2017 7:33 PM