How to remove part of the name of all files in a folder via CMD/Powershell

Asked

Viewed 133 times

2

  • I need to standardize the name of all the files in a folder, considering that the files already have a pattern at the beginning of their name up to the 8th character, being only necessary to demote everything in sequence. Example: currently "A200T011 - So-and-so of such.docx" -> as it should be "A200T011.docx".

  • Conditions: use script to run on Windows or Windows Server, may or may not be applied to a file. bat

I have already written a script that deletes the characters I do not want, but it is very expensive to list all the characters that should be deleted, it would be more interesting to do the opposite, informing a set of characters (few) and deleting everything that is not equal to this. Follow my script:

get-childitem | foreach {rename-item $_ $_.Name.Replace("E","")}

The above script replaces the 'E' letter with empty.

  • In case Voce wants to remove all caracertes from the files except the first 8 + extension.

1 answer

1


You can do with the function Substring to fetch part of the file name and merge with its extension.

Behold:

Get-ChildItem * | % { Rename-Item $_ ($_.Name.Substring(0,8) + $_.Extension) } 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.