Friday, February 24, 2017

command line - How to specify a space as part of a wildcard string for bulk renaming of files



I have approximately 1200 songs in a folder whose filenames are structured as follows:




artist_name - song_name.mp3



For example:



Billy Joel - Piano Man.mp3
The Beatles - Nowhere Man.mp3


I'd like to use DOS commands in Windows PowerShell to do a bulk rename of these files to remove the space on either side of the hyphen. I don't want to remove all spaces, however. The basic problem is to be able to include spaces in the filename specs in order to change " - " to "-" in all files in a directory. Here are some examples of what I'd like to change:




Existing filenames



Billy Joel - Piano Man.mp3
The Beatles - Nowhere Man.mp3


Desired filenames



Billy Joel-Piano Man.mp3

The Beatles-Nowhere Man.mp3


Can someone think of a DOS command that could be executed at the command prompt to make such a change on all files in the directory?



I've found that to list all such files with a DIR command, I can use:



DIR asterisk+doublequote+space+hyphen+space+doublequote+asterisk+dot+asterisk



I had to write it out that way because the actual syntax appears to be treated as formatting instructions here in this forum.



This lists all files which have the 3-character string of space-hyphen-space.



Surely, there must be a way.


Answer



You can run this command:



ls | %{ ren $_ ($_.name -replace ' - ','-') }



using Powershell. This replaces {space}-{space} with -.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...