B.Ahern
B.Ahern

Reputation: 27

Renaming multiple files in Windows

I have a folder containing multiple .txt files which have been created by another program. However the program outputs each file with "-Module" in the filename. eg

filename-Module.txt
filename1-Module.txt
filename2-Module.txt

I would like to know if there's a script or command (Powershell/cmd) that I can run which will iterate over each file and remove the "-Module" from each filename so I simply end up with

filename.txt
filename1.txt
filename2.txt

I have tried using cmd from the directory, with the following:

rename *-Module.txt *txt

This resulted in no change.

Upvotes: 0

Views: 871

Answers (2)

C. Helling
C. Helling

Reputation: 1402

Relatively straightforward:

Get-ChildItem ComputerName\TestLocation\testfolder -filter "*-module*" | Rename-Item -NewName {$_.name -replace '-module', ''}

Get the items in the folder, look for filenames with "-module" in them, and then replace the "-module" text with an empty string. You can also append a -whatif to see the output before performing this action.

Upvotes: 0

MattMoo
MattMoo

Reputation: 192

You can use get-childitem and pipe it into rename-item.

    get-childitem -path 'c:\folderwherefilesarelocated' -recurse |
    rename-item -newname {$_.Name -replace "-module", ""}

Upvotes: 2

Related Questions