rename-item and override if filename exists

前端 未结 2 1038
日久生厌
日久生厌 2020-12-17 08:31

I am trying to use the Rename-Item cmdlet to do the following:

I have folder which contains \"*.txt\" files. Let\'s say 1.txt, 2.txt et

相关标签:
2条回答
  • 2020-12-17 08:42

    As noted by @lytledw, Move-Item -Force works great for renaming and overwriting files.

    To replace the index. part of the name, you can use the -replace regex operator:

    Get-ChildItem index.*.txt |ForEach-Object {
        $NewName = $_.Name -replace "^(index\.)(.*)",'$2'
        $Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
        Move-Item -Path $_.FullName -Destination $Destination -Force
    }
    
    0 讨论(0)
  • 2020-12-17 08:51

    I tried the rename-item -force and it did not work. However, I was able to do move-item -force and it worked fine

    0 讨论(0)
提交回复
热议问题