none
Powershell File Copy/Move/Rename RRS feed

  • Question

  • I have a large number of files to rename - essentially overwriting an existing file with the same name; I need to overwrite the existing file with the new file, removing the -NEW from the filename.

    FileName.txt FileName-NEW.txt

    I have found a number of ways to do this, but not with -Recurse option.

    Get-ChildItem, rename-item, -replace etc will work with -Recurse option as long as the file doesn't already exist.

    Not looking for a long script to create, just a quick option that I'm hoping someone will say 'obvious, why didn't you try....'

    Many thanks for your time

    PJ

    Wednesday, October 4, 2017 2:37 PM

Answers

  • Use Move-Item -Force instead of Rename-Item if you want to overwrite.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, October 4, 2017 3:12 PM
    • Marked as answer by PJ80 Thursday, October 5, 2017 7:44 AM
    Wednesday, October 4, 2017 3:11 PM
    Moderator

All replies

  • Rename only works WHEN the file exists so what you are stating makes no sense.

    What code did you try?

    Rename-Item filename.txt filename-new.txt


    \_(ツ)_/

    Wednesday, October 4, 2017 2:41 PM
  • Of course Rename-Item won't work if the file already exists. In that case there will be a non-terminating error.

    If you want to overwrite, you would want to use Move-Item rather than Rename-Item.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 4, 2017 2:42 PM
    Moderator
  • To recursively rename file:

    dir *.txt -rec | %{$_|rename-item -NewName ($_.BaseName+'_NEW'+$_.Extension) -WhatIf}


    \_(ツ)_/

    Wednesday, October 4, 2017 2:45 PM
  • Yes - if the new filename already exists then you have to add something. The original question is quite confusing to us mere mortals.


    \_(ツ)_/

    Wednesday, October 4, 2017 2:48 PM
  • Apologies - a long day already.

    If one file exists, eg - File-NEW.txt, and it is to be renamed File.txt and similar for sub-folders, then

    Get-ChildItem -Filter "*-New.txt" -Recurse -Force |Rename-Item -NewName {$_.name -replace '-New',''}

    works a treat.

    The issue is that File.txt already exists alongside File-New.txt. I had hoped the rename would overwrite the existing (old) file - but it doesn't, an error saying the file already exists appears...

    Rename-Item : Cannot create a file when that file already exists.
    At line:1 char:53
    + Get-ChildItem -Filter "*-New.txt" -Recurse -Force |Rename-Item -NewName
    {$_.nam ...
    +

    Wednesday, October 4, 2017 2:52 PM
  • Then you need to test to see if the file already exists and take custom actions.

    \_(ツ)_/

    Wednesday, October 4, 2017 2:59 PM
  • Use Move-Item -Force instead of Rename-Item if you want to overwrite.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, October 4, 2017 3:12 PM
    • Marked as answer by PJ80 Thursday, October 5, 2017 7:44 AM
    Wednesday, October 4, 2017 3:11 PM
    Moderator
  • With Move-Item you will have to use the fullname (pathname) of the target file.


    \_(ツ)_/

    Wednesday, October 4, 2017 3:15 PM
  • Sure. Just use the DirectoryName property of the source object.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 4, 2017 3:44 PM
    Moderator