Search code examples
powershellsharesynology

Cannot rename files in sub directories with rename-item in Shared Folder location on Synology


I have several .mkv files on a Synology NAS in sub directories of a Shared Folder that have the following .partial~ extension appended at the end.

I am trying to remove .partial~ from the end of these files, and ignore all other files located within such as .jpg

Folder\File Structure Eg:

\\NAS\SharedFolder\Subdirectory1\Subdirectory2\file.mkv.partial~

\\NAS\SharedFolder\Subdirectory1\file.mkv.partial~

\\NAS\SharedFolder\Subdirectory1\file.jpg

I have created the following powershell script strippartialext.ps1 that works correctly when run on my Windows 10 PC from within Subdirectory1 or Subdirectory2:

\\NAS\SharedFolder\Subdirectory1\strippartialext.ps1 [WORKS HERE]

\\NAS\SharedFolder\Subdirectory1\Subdirectory2\strippartialext.ps1 [WORKS HERE]

It strips out everything below either (and into subdirectories below) but does not work when run from the top SharedFolder location:

\\NAS\SharedFolder\strippartialext.ps1 [DOES NOT WORK HERE]

Get-ChildItem -File -Recurse | % { Rename-Item -Path $_.PSPath -NewName $_.Name.replace(".partial~","")}

I get the following errors when running the .ps1 file from the SharedFolder location:

Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core\FileSystem::\NAS\sharedfolder\Sub Directory1\file.jpg' does not exist. At \NAS\sharedfolder\strippartialext.ps1:1 char:36 + ... curse | % { Rename-Item -Path $.PSPath -NewName $.Name.replace(".pa ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

I would like to know how to get this file to run from the SharedFolder location as I would only have to run this script once to cover all of my files instead of hundred of times separately within each Sub directory Folder.


Solution

  • The Rename-Item cmdlet accepts piped input, so I'd try:

    Get-ChildItem -File -Recurse "*.partial~" | 
        Rename-Item -NewName {$_.Name.replace(".partial~","")} -WhatIf
    

    If the output looks OK remove hte -WhatIf