开发者

Renaming many folders in PowerShell

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*”, “”}


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_' }


get-childItem ABC_* | rename-item -newname { $_.name -replace 'ABC_','' }

Source: get-help rename-item -full

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜