none
move-item: could to force the move if the target file already exist?

    Question

  • The -force doesn't help, if the target file exist, I can't do the move-item, anyway to force it?
    Tuesday, October 26, 2010 9:04 AM

Answers

All replies

  • Interesting. I just did a force.

    PS C:\dell> notepad test.txt
    PS C:\dell> cd ..
    PS C:\> cd .\Intel
    PS C:\Intel> notepad test.txt
    PS C:\Intel> Move-Item .\test.txt C:\dell
    Move-Item : Cannot create a file when that file already exists.
    At line:1 char:10
    + Move-Item <<<< .\test.txt C:\dell
      + CategoryInfo     : WriteError: (C:\Intel\test.txt:FileInfo) [Move-Item], IOException
      + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
    
    PS C:\Intel> Move-Item .\test.txt C:\dell -Force
    PS C:\Intel> dir
    
    
      Directory: C:\Intel
    
    
    Mode        LastWriteTime   Length Name
    ----        -------------   ------ ----
    d----    10/21/2010  2:58 PM      ExtremeGraphics
    d----    10/22/2010 12:07 AM      Logs
    
    
    PS C:\Intel>
    

    Ravikanth
    http://www.ravichaganti.com/blog
    Twitter: @ravikanth
    PowerShell 2.0 remoting - eBook
    Tuesday, October 26, 2010 9:09 AM
  • Sorry I mean  rename-item

     rename-item $targetTempFile $finalCSVFile -force

     

    The error is:

    Rename-Item : Cannot create a file when that file already exists.

    Wednesday, October 27, 2010 1:41 AM
  • Aah..that makes sense. see the help for -Force

    -Force [<SwitchParameter>]
        Allows the cmdlet to rename items that cannot otherwise be changed, such as hidden or read-only files or read-only aliases or variables. The cmdl
        et cannot change constant aliases or variables. Implementation varies from provider to provider. For more information, see about_Providers. Even
        using the Force parameter, the cmdlet cannot override security restrictions.

    Use Move-Item instead


    Ravikanth
    http://www.ravichaganti.com/blog
    Twitter: @ravikanth
    PowerShell 2.0 remoting - eBook
    Wednesday, October 27, 2010 2:09 AM
  • then why we have Move-Item and rename-item. Move-item alone is not enought? Looks like what rename-item can do can also be done in move-time. 
    Wednesday, October 27, 2010 5:49 AM
  • I would say yes.

        The Rename-Item cmdlet changes the name of a specified item. This cmdlet does not affect the content of the item be
        ing renamed.

        You cannot use Rename-Item to move an item, such as by specifying a path along with the new name. To move and renam
        e an item, use the Move-Item cmdlet.


    Ravikanth
    http://www.ravichaganti.com/blog
    Twitter: @ravikanth
    PowerShell 2.0 remoting - eBook
    Wednesday, October 27, 2010 10:12 AM