16

Currently I'm using cmdlets like:

dir | rename-item -NewName {$_.name -replace "-","_"}

But now I need to add a prefix to the filename for all files in the directory.
I can't predict how long each filename is (it varies), so I can't just use a bunch of . wildcards.

I need it to append a fixed phrase to the beginning of each filename, regardless what that filename is.

Giffyguy
  • 1,022
  • 10
  • 22
  • 38
  • 1
    See solution here http://stackoverflow.com/a/20874916/5518385 – Yisroel Tech May 07 '17 at 22:53
  • 1
    What is it now? A prefix or a suffix? For a prefix you already got a name so (literally) just add to your prefix or (for a suffix) just add the suffix to it. – Seth May 08 '17 at 12:09

4 Answers4

29

An alternative approach to a regular expression would be to use simple string operations to create the new file name.

$items=Get-ChildItem;
$items | Rename-Item -NewName { "Prefix_" + $_.Name };
Blaze
  • 773
  • 4
  • 6
Seth
  • 9,000
  • 1
  • 19
  • 34
  • 8
    I'm trying this, but it seems to go mad and keep trying to prefix filenames over and over until it crashes from the filename being too long - e.g. running the command above results in filenames like `Prefix_Prefix_Prefix_Prefix_Prefix_Prefix_Prefix_Prefix_Prefix_Prefix_...` – Tom Carpenter Sep 02 '17 at 14:37
  • Split the command. `$items = Get-ChildItem;` And replace the `Get-ChildItem` in the above case with `$items`. – Seth Sep 06 '17 at 10:21
  • 2
    In the end based on [this](https://stackoverflow.com/a/37279979/1557472), I just added ```-Exclude Prefix_*``` to the `Get-ChildItem` command. – Tom Carpenter Sep 06 '17 at 10:23
  • Make it into a one liner without the variables: ``Get-ChildItem C:\Directory\Path -File | Rename-Item -NewName { "PrefixName_" + $_.Name }`` – DBADon Dec 01 '22 at 19:25
13

You are quite near.

  • -replace uses RegEX and in a Regular Expression you anchor at the beginning with a ^
  • for a suffix you can similarly replace the anchor at line end with $
  • to avoid any possible re-iteration of the renamed file enclose the first element of the pipeline in parentheses.

(Get-ChildItem -File) | Rename-Item -NewName {$_.Name -replace "^","Prefix_"}
LotPings
  • 7,011
  • 1
  • 15
  • 29
  • 1
    I like this answer because it doesn't cause an infinite loop and I don't need to write the prefix twice - like what happens in other solutions with the `-Exclude Prefix_*` to avoid the loop. – TCB13 Jul 16 '18 at 07:34
1

To remove prefix of all in folder:

Get-ChildItem 'Folderpath\KB*' | Rename-Item -NewName { $_.Name -replace 'KB*','' }

To Add a prefix to all in folder:

foreach($item in 'Folderpath')
{
Get-ChildItem 'C:\Users\ilike\OneDrive\Desktop\All in one script\Single Patch here' | Rename-Item -NewName { "KB" + $_.Name }
}

Took a while, hope this can help others in need

Reddy Lutonadio
  • 17,120
  • 4
  • 14
  • 35
0

get-childitem *.FileExtension_ | foreach { rename-item $_ $.Name.Replace($.Name,"Prefix_" + $_.Name) }

Replace FileExtension_ with the extension corresponding to the filetype you want to add prefix to. Or just remove .FileExtension_ along with the "." to add prefix to all files in working directory. Replace Prefix_ with the desired prefix.