Helen
Helen

Reputation: 617

Windows CMD: add suffix with spaces to all subfolders

This question kind of look like this one here Windows-cmd-add-suffix-to-all-files-in-folder, but it has some additional elements I'm not able to overcome.

I have a main folder, containing many subfolders, with a lot of .xlsb files, and I wish to add a postfix " 2018" to eaxh file, e.g. after renaming a file with name "abc.xlsb" will be called "abc 2018.xlsb". I'm having problem with finding a code, making a script in the command line that will do this for all subfolders. For now, this is what I have for one folder:

rename *.xlsb "* 2018.xlsb"

,but the resulting name for, e.g., "a.xlsb" comes out "a.xlsb 2018.xlsb".

Also, how do I do this for all folders?

EDIT: I found a command to rename for all subfolders:

for /r %x in (*.xlsb) do ren "%x" *.xlsb

,but

for /r %x in (*.xlsb) do ren "%x" "* 2018.xlsb"

gives, e.g. for a file "abc.xlsb" --> "abc.xlsb 2018.xlsb"

Upvotes: 0

Views: 1142

Answers (1)

lit
lit

Reputation: 16266

This is pretty straightforward in PowerShell. Use the BaseName and Extension members separately. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.

Get-ChildItem -File -Recurse -Filter '*.xlsd' |
    ForEach-Object {
        $newname = $_.BaseName + ' 2018' + $_.Extension
        Rename-Item -Path $_.FullName -NewName $newname -WhatIf
    }

If you must run this from a cmd.exe shell, then put the PowerShell code above into a filename such as renappend.ps1. Then run it as shown.

powershell -NoLogo -NoProfile -File renappend.ps1

Upvotes: 1

Related Questions