Batch rename files using Windows Power Shell

Asked

Viewed 8,265 times

4

How to rename all files in a folder whose name is a number (integer) followed by a certain extension (my choice) using Windows Power Shell?

Example: The folder as is:

1.txt
2.txt
7.txt
arquivo.html

How it should look after renaming:

file1.txt
file2.txt
file7.txt
arquivo.html
  • Good evening Ricardo, we are discussing this question here http://meta.pt.stackoverflow.com/q/4516/3635

  • Looks like I was wrong +1

3 answers

5

Suppose you have a directory with corrupted file names. You just know that they are all files. png, but the extensions have been switched to random names. You would then like to rename everything to . png, at once.

Using Windows Powershell:

 dir | % {ren $_ ($_.name.substring(0, $_.name.length-4) + ‘.png’ ) } 

The first command, dir, gets a list of files from the current directory and passes objects (not text!) to the next command in the pipeline. The next one (the % means foreach-Object) executes the block (between { e } ) for each item.

In this case, the Rename command ($_) and the new name ($_.name.substring(0, $_.name.length-4) + ‘.png’ )

Source: http://www.purainfo.com.br/programacao-scripts/power-shell/renomando-arquivos-em-massa-pelo-power-shell/

3

I used the following command to rename all files of a given folder and a certain extension:

Dir *.txt | ForEach-Object  -begin { $count=1 }  -process { rename-item $_ -NewName "$count.txt"; $count++ }

It is required to be in the folder you want to rename the files.

NOTE: Does not distinguish names composed by numbers.

  • 1

    Cool, I’ve never used Powershell I’m testing Since it seems to work. + 1 I haven’t been able to reproduce exactly the way you did, but I’ll study :)

  • 1

    @Guilhermenascimento looks I also do not know the powershell, I had the need to rename files in batch (with some specific requirements that was not possible only with the explorer) so I searched and found the powershell and found it interesting to bring the content here.

3


Can solve the problem by combining cmdlets Get-Childitem to list the directory files and Rename-Item to rename them

$raiz = "c:\ps"
$arquivos = Get-ChildItem $raiz  -Filter "*.txt" | Where-Object {$_.Name -match "^\d"} 
foreach($item in $arquivos){
    Rename-Item -NewName ("novo"+$item.Name) -Path ($raiz+$item.Name)
}

The first part of the line filters all files with extension .txt, the second part takes the cmdlet return(pipe) and applies a regex to the file name it says to capture only the files that the names start with some number and third part is the assignment of the selected files to be renamed.

Rename-Item changes the file name with the prefix "new" followed by the old name (which was a number)

$arquivos = Get-ChildItem $raiz  -Filter "*.txt" | Where-Object {$_.Name -match "^\d"} 
|3 parte    |1 parte                             |2 parte

Browser other questions tagged

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