0

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.

B.Ahern
  • 27
  • 5
  • What have you tried so far, where are you stuck? Please share your efforts; [edit] your question for that... – aschipfl Dec 18 '17 at 16:33
  • Might be able to find your answer here https://superuser.com/questions/236820/how-do-i-remove-the-same-part-of-a-file-name-for-many-files-in-windows-7 – Zannith Dec 18 '17 at 16:39
  • Possible duplicate of [Rename multiple files in cmd](https://stackoverflow.com/questions/17271586/rename-multiple-files-in-cmd) – Guillaume S Dec 18 '17 at 16:46
  • Thanks for all the advice. I am really only beginning to try and learn PS/cmd so I appreciate the feedback and direction. – B.Ahern Dec 19 '17 at 15:27

2 Answers2

2

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

    get-childitem -path 'c:\folderwherefilesarelocated' -recurse |
    rename-item -newname {$_.Name -replace "-module", ""}
MattMoo
  • 192
  • 2
  • 3
  • 11
0

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.

C. Helling
  • 1,394
  • 6
  • 20
  • 34