Rename multiple files with different extensions to .backup

This topic contains 5 replies, has 4 voices, and was last updated by  random commandline 2 weeks, 1 day ago.

  • Author
    Posts
  • #101828

    Joseph
    Participant

    Hello –
    I am trying to create a script that will be contained in file distributions that copies the files contained and renames the existing files at the endpoint to *.backup – The files do not have uniform extensions, or names.

  • #101831

    Rob Simmers
    Participant

    Have you tried anything? Maybe a search for Powershell Rename File. We're happy to help if you're having issues, but this isn't a script writing service.

    • #101839

      Joseph
      Participant

      So far I have tried a couple dozen versions. Apologies for not having posted them.
      Im new to powershell, so im struggling to understand how each of the objects is being processed and what I can use as a wildcard and what I cannot.
      This is what I have been trying to get working
      get-childitem -path $path | rename-item -newname (*.*, *.backup)
      I have tried a large number of different methods to change the wild cards including *_.name, "name.$_", $_.

      Im not needing a script written, Just need to understand how to create the correct wild card after -newname to leave the file name, and change only the extension.

  • #101840

    Jon
    Participant

    https://stackoverflow.com/questions/12120326/how-do-i-change-the-extension-of-many-files-in-a-directory

    get-childitem -path $path | rename-item -newname { [io.path]::ChangeExtension($_.name, ".backup") }

  • #101869

    Joseph
    Participant

    End result
    Files = Get-ChildItem -path $path -Recurse | Select-Object -ExpandProperty Name
    Foreach ($File in $Files)
    {
    IF ($File -like '*.*')
    {
    Rename-Item -Path $path\$File -NewName "$file.backup" -Force
    }
    else
    {
    $false
    }
    }

  • #101876

    random commandline
    Participant
    Get-ChildItem -Path \\path\ -Recurse -File | 
    Rename-Item -NewName {$_.Name + '.backup'} -WhatIf
    

You must be logged in to reply to this topic.