0

I want to write a script that will change the names of all the files in a directory to something uniform.

Example: existing files: awff.jpg, forp.jpg, ddd.jpg, www.jpg

files after script: file1, file2, file3, file4.jpg


So if i have a directory full of files, i want to run the script, i assume it will pull the contents of the directory into and array, then iterate through a loop applying the new name with the iterator appended to the end of it.

Does PowerShell have the ability to do this?

This gets me close but i want to just grab all the files, and rename them all in iterative order.

Get-ChildItem -Filter "*current*" -Recurse | Rename-Item -NewName {$_.name -replace ‘current’,’old’ }

or this:

Get-ChildItem -LiteralPath 'C:\temp' -Recurse | Where-Object {-Not $_.PsIscontainer -AND $_.name -match "."} | 
foreach {
$New=$_.BaseName.Replace("*","File")+$_.Extension
Rename-Item -path $_.Fullname -newname $New -passthru
}
asked Apr 26, 2019 at 13:59

1 Answer 1

0

While PowerShell itself understands typographic quotes,
I'd avoid them due to issues in edge cases with encodings.

Your description and code do not match, do you want to rename all jpg files (recursively) with the prefix file and a counter, or just parts of the names?

$Counter = 1
Get-ChildItem -Filter "*.jpg" -Recurse | 
 Rename-Item -NewName {'file{0:D3}.jpg' -f $script:Counter++} -WhatIf

In a folder:

> Get-ChildItem *.jpg -Name
awff.jpg
ddd.jpg
forp.jpg
www.jpg

the above script would yield (in my German locale windows):

WhatIf: Ausführen des Vorgangs "Datei umbenennen" für das Ziel 
"Element: A:\awff.jpg Ziel: A:\file001.jpg".
WhatIf: Ausführen des Vorgangs "Datei umbenennen" für das Ziel 
"Element: A:\ddd.jpg Ziel: A:\file002.jpg".
WhatIf: Ausführen des Vorgangs "Datei umbenennen" für das Ziel 
"Element: A:\forp.jpg Ziel: A:\file003.jpg".
WhatIf: Ausführen des Vorgangs "Datei umbenennen" für das Ziel 
"Element: A:\www.jpg Ziel: A:\file004.jpg".

If the output pooks OK, remove the -WhatIf

answered Apr 26, 2019 at 15:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.