Question:
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
What it should look like after renaming:
file1.txt
file2.txt
file7.txt
arquivo.html
Answer:
Suppose you have a directory with corrupted filenames. You just know that they are all .png files, but the extensions have been changed 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 then (the % stands for foreach-object) executes the block (between { and } ) for each of the items.
In this case, the command rename passing the name ($_)
and the new name ($_.name.substring(0, $_.name.length-4) + '.png' )