1

I have over 1000+ files that have to be renamed.

The first set folder and/or files are grouped by location, so the first four characters are the same for each file; there are four-five different locations. I need to delete the first few characters of the folder's name.

Example:

Old File: ABC_Doe, Jane
New File: Doe, Jane

any suggestions as to the quickest way to carry this out?

I've tried all of the following:

  1st Attempt 
  $a = Get-ChildItem C:\example
  $b = Where-Object {$_.name -like “*ABC_*”}
  $cmdlet_name = “Rename-Item”
  $d = (cmdlet_name $a $b)
  invoke-expression $d

  2nd Attempt 
  $e = Get-ChildItem C:\example
  $f = $e.TrimStart (“ABC_”)

3rd Attempt

Rename-Item -{$_.name -like “*ASD*”, “”}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kambridge
  • 11
  • 1
  • 2
  • 1
    You are just blindly shooting in the dark. Why don't you read up some basics of Powershell and then start with it? – manojlds Aug 07 '11 at 09:14
  • @manojilds at least he's trying? many post here without any prior effort at all... – x0n Aug 07 '11 at 13:31
  • @x0n - and those get downvoted and closed etc...just saying since he is wasting time doing random things and some basic intro would save a lot of time. And wouldn't have bothered to comment on the questions where the OP had not tried anything. – manojlds Aug 07 '11 at 22:48

2 Answers2

6

Try this, get all child items (files only), remove abc_ by replacing them (with nothing) and rename each file. To rename files in sub-directories add the -Recurse switch to the Get-ChildItem command:

Get-ChildItem c:\example -Filter ABC_* | Where-Object {!$_.PSIsContainer} | Rename-Item -NewName { ($_.BaseName -replace '^ABC_') + $_.Extension }

UPDATE

Actually, this should work as well and is much shorter (no need to append the file extension cause renaming is performed on the file name).

Get-ChildItem c:\example -Filter ABC_* | Where-Object {!$_.PSIsContainer} | Rename-Item -NewName { $_.Name -replace '^ABC_' }
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 2
    Add `-verbose` to the `rename-item` to get a listing of what is changed. Or a `-WhatIf` to list what would be changed (but not change anything). – Richard Aug 07 '11 at 08:44
0
get-childItem ABC_* | rename-item -newname { $_.name -replace 'ABC_','' }

Source: get-help rename-item -full

Richard
  • 106,783
  • 21
  • 203
  • 265
Isa
  • 1