Elizabeth Murray
  • home
  • research
    • phylogenomics in Aculeata
    • bee viruses
    • eucharitid ant parasitoids
  • publications
  • teaching
  • blog

Renaming multiple files using PowerShell

4/23/2017

2 Comments

 
For the PC owners: Have you ever used Windows PowerShell? It's something akin to Command Prompt, and can be utilized for task automation. It provides a really easy way to batch rename hundreds of files in a folder.
Specifically, I had hundreds of individual file names containing a "-" that needed to be replaced or removed, and I was looking for something straightforward for changing the names. I found Windows PowerShell, which is a management framework that was developed about ten years ago. I discovered it was pretty easy to use PowerShell for this task and others like it, and thought I'd share it here!

Here's how to get set up:
All your files should be in one folder, which you'll designate as your working directory. Open PowerShell on your PC. You can just type "powershell" into the search box in Windows 10. A window will open.
To navigate to your folder of files, type "cd" (change directory) at the prompt and then add a space. Drag and drop your working folder into the PowerShell window. The drag and drop puts the whole file path into place for you. Press enter and you'll see that you're now operating out of your folder. 
Picture
My files are all in a folder called "files_to_rename". This folder is now set as my working directory.

to do a batch rename on the files in your current directory:
Picture
original filenames
Picture
modified filenames
Picture
alternate renaming
There is just one line of code that you paste in to PowerShell.
dir | rename-item -NewName {$_.name -replace "-","_"}
Here, all you have to modify is what is found in the quotes. The 1st set of quotes contains the piece of the file to replace (a dash), and the 2nd set of quotes contains the string you want (an underscore). Alternatively, if you do not want any character, leave the second set of quotes empty, with no space (as in: -replace "-",""). If you have some files that don't match the string exactly, they will be ignored.
How does it work? You are 'piping' ("|") the contents of your directory ("dir") to the "rename-item" cmdlet (read: command-lette). If you were to enter only "dir" at the prompt, you would see all the files in your folder displayed; we use the last section of the script to run through all of these files.
I initially learned of this command at: https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/
2 Comments

    PhyloBlog

    Covering topics of phylogenetics and systematics & other science-related news.

    Archives

    October 2019
    June 2019
    March 2019
    November 2018
    October 2018
    September 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016

    Categories

    All
    History & Context
    Programs & Packages
    Taxonomy & Morphology

    RSS Feed

Elizabeth A. Murray, ​PHYLOGENETICS AND EVOLUTION of Hymenoptera

@PhyloSolving  |  e.murray @ wsu.edu
  • home
  • research
    • phylogenomics in Aculeata
    • bee viruses
    • eucharitid ant parasitoids
  • publications
  • teaching
  • blog