Powershell - Rename filename by removing the last few characters

前端 未结 3 757
说谎
说谎 2021-01-19 01:02

I want to remove the last 11 characters of multiple files names. For example I have these file names:

ABCDE_2015_10_20
HIJKL_2015_10_20
MNOPQ_2015_10_20
RSTU         


        
3条回答
  •  渐次进展
    2021-01-19 01:40

    Just to add to the response from arco444:

    Get-ChildItem 'E:\Thomson Reuters\Stage' -filter *.txt | rename-item -NewName {$_.name.substring(0,$_.BaseName.length-6) + $_.Extension -replace "_"," "}
    

    This would rename all .txt files in the directory, remove the last 6 characters of the file name, replace any remaining underscore in the filename with a space but still retain the file extension.

    So assuming these were text files you would see something like this:

    ABCDE_2015_10_20.txt
    HIJKL_2015_10_20.txt
    MNOPQ_2015_10_20.txt
    RSTUV_2015_10_20.txt
    

    Become this:

    ABCDE 2015.txt
    HIJKL 2015.txt
    MNOPQ 2015.txt
    RSTUV 2015.txt
    

提交回复
热议问题