0

I try to rename files with PwerShell Rename-Item cmdlet. Code below

Get-ChildItem -recurse * `
  | ?{!$_.PsIsContainer} `
  | Rename-Item -NewName {$_.FullName -Replace '.abcd@email.com','.abcd@email_A.com.abcd@email_B.com.abcd@email_E.com'}

But, PowerShel tells me about long path or file name; which is irrelevant to my process. But it is strongly necessary keep new long name.

How to except this error?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Alex
  • 685
  • 3
  • 9
  • 20
  • `-NewName` should be **string** (but `{}`indicate a _ScriptBlock_) Use something like `-NewName $($_.FullName -Replace '.abcd@email.com','.abcd@email_A.com.abcd@email_B.com.abcd@email_E.com')` – JosefZ Jun 12 '17 at 20:48
  • Hi! Thank you for answer. If I do like you mentioned, I get error with message: "Rename-Item : Cannot bind argument to parameter 'NewName' because it is null." – Alex Jun 12 '17 at 20:58
  • @JosefZ see https://blogs.msdn.microsoft.com/powershell/2006/06/23/flexible-pipelining-with-scriptblock-parameters/ section "ScriptBlock Parameters" - passing a scriptblock as a parameter which doesn't specify a scriptblock type is valid and it will be dynamically evaluated. – TessellatingHeckler Jun 12 '17 at 21:12
  • Possible duplicate of [Get all files in folder including those with long (>256 characters) path + name](https://stackoverflow.com/questions/34459752/get-all-files-in-folder-including-those-with-long-256-characters-path-name) - if not a direct duplicate of this, one of many "long path error" questions, which this has some links to. Short answer: there's no easy way round it. Long answer: you call out to lower level APIs, custom modules, or install Windows 10 latest and edit the registry – TessellatingHeckler Jun 12 '17 at 21:19
  • @TessellatingHeckler maybe however I'd prefer a conservative approach with `Foreach-Object`. Moreover, `-NewName` might not allow a path and name. I'd use either `Move-Object` or (changed) `$_.Name`. – JosefZ Jun 12 '17 at 21:33
  • @JosefZ is it possible ask you about code example? I'm new in PowerShell commands. – Alex Jun 12 '17 at 21:44
  • 1
    Renaming an item implies that it stays at the same folder, so don't use `$_.FullName` but `$_.Name` to **only** change the name. Otherwise use Move-Item as already suggested. –  Jun 12 '17 at 22:22
  • Although I didnot get the error and operationally it worked properly. Still suggesting [XYExplorer](https://www.xyplorer.com/) – Ranadip Dutta Jun 13 '17 at 08:32
  • Yes, change `$_.fullname` to `$_.name`. And you can use `get-childitem -file`. And test `rename-item` with `-whatif`. – js2010 Jul 03 '19 at 15:45

1 Answers1

3

is it possible ask you about code example? The following commented code snippet (conservative approach) could help:

Get-ChildItem -recurse * | Where-Object {!$_.PsIsContainer} | 
  ForEach-Object {
    ###  in regular expression:  ↓           ↓   escape dots
    $NewName = $_.Name -Replace '\.abcd@email\.com',
      '.abcd@email_A.com.abcd@email_B.com.abcd@email_E.com'

    ### here is right place to check target filename length:
    $targetLength = 1 + $_.DirectoryName.Length + $NewName.Length

    Rename-Item -Path $_.FullName -NewName $NewName
  }

Read Rename-Item reference and Regular Expression Language - Quick Reference.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
JosefZ
  • 28,460
  • 5
  • 44
  • 83