Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

powershell - Renaming multiple files by rearranging the original file name

I want to rename multiple files that follow the same naming convention, the only variation is the date at the end of the name.

For example TRADARCASHROLLUP.GRPTIRHK.01052017.CSV I would like the new name of the file to begin with the date instead 01052017.TRADARCASHROLLUP.GRPTIRHK.CSV

Was hoping someone could help me write a batch script to achieve this. I have seen other answers but not been able to apply them to this.

I have tried using the following:

ls | %{ "C:UBSTestingIncomingTRADARCASHROLLUP.GRPTIRHK*" $_.name ($_.name 
-replace '^(w+).(w+).(w+)', '$3.$1.$2')}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with this code :

Get-ChildItem -Path 'C:	emp' | ForEach-Object {
    $newName = $_.Name -replace '(.+).(d{8})', '$2.$1'
    Rename-Item $_ -NewName $newName
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...