How can I create a batch file that removes specific characters from the filenames only? Is it possible to do in a script? I'm looking to remove these symbols:
({[}])+-
Let me introduce you to Ruby, a scripting language far more powerful and easier to learn than the M$ alternative Powershell. The windows interpreter you can find at http://rubyinstaller.org/ I could give you the code in one line but for clarity here a 4 line program that does what you need.
Dir['c:/temp/*.package'].each do |file|
new_name = File.basename(file.gsub(/[({\[}\])+-]/, ''))
File.rename(file, new_name)
end
Let me explain, the Dir command enumerates the path with wildcards inside, in unix notation with slashes instead of backslashes and for each file it generates a new_name by taking the basename (filename only) of the path and using a Regular Expression to replace (gsub) the characters inside the /[]/ with the second parameter, '' (empty string). Regular Expressions are the way to go for such things, plenty of information to google if you want to know more about. Finally I use the utility class File (yes Ruby is totally Object Oriented) to rename the file to the new name.
You could do this with any language but I bet not so concise and readable as in Ruby. Install the Ruby interpreter, save these lines in eg cleanup.rb, change the path to yours and fire it up with ruby cleanup
, or just cleanup
if your extension is correctly associated.
This renames
c:\temp\na({[}])+-me.package
to
c:\temp\name.package
And as a bonus: here the one line version that does the same in the current folder.
Dir['*'].each {|f| File.rename(f, File.basename(f.gsub(/[({\[}\])+-]/, '')))}