0

I have a bunch of files in a directory (windows) that I wish to rename so that a certain character sequence is removed from each. For example, I wish to remove the "-FFF" from filenames which are similar to below.

asd-dfj-FFF.mp3
dfd-sdj-FFF.mp3

What is the best way to do this? Any language is fine. I know C & Java so I'd prefer if it was in a language that wasn't them so I get to learn/see another language in action.

Michael
  • 5,994
  • 7
  • 44
  • 56

3 Answers3

1

I'm pretty sure you can do this with just the rename command in the Windows cmd shell, no additional languages required.

Try this out on a few test files in a temporary directory:

C:\tmp\> ren *-FFF.mp3 *.mp3

(I don't have a windows box handy to verify, but I remember this working back in the DOS days, and the Microsoft docs suggest it still works)

ObscureRobot
  • 7,306
  • 2
  • 27
  • 36
  • Hmm, that doesn't work unfortunately, if I just do `ren *.mp3 *.txt` it works but adding in the `*-FFF.mp3 *.mp3` causes nothing to happen. However, when I go `ren *-FFF.mp3 *.txt` the file extension of the files change however the name doesn't – Michael Nov 13 '11 at 07:35
  • what if you do: `ren *.mp3 *` then `ren *-FFF *`, will that work? – ObscureRobot Nov 13 '11 at 07:37
  • Probably best to go with regexes in perl or python then, per @Danish's suggestion. – ObscureRobot Nov 13 '11 at 08:02
1

Considering there are no solutions, I'll post my own solution.

I made the following batch file which works for my case as desired. This strips filenames which have -aSBo appended to it.

::Change the *-aSBo.* to whatever files you want to strip
for %%f in (*-aSBo.*) do call :rename "%%f"

goto :eof

:rename
set var="%~n1
set var1=%~x1"
::Change the 0 (start) and -5 (end) values to match the substring start/end values that you want to rename to
ren %1 %var:~0,-5%%var1%
Michael
  • 5,994
  • 7
  • 44
  • 56
0

I would say give either Windows Power Shell, Perl or Python a look. My personal favorite would be Python.

All three above are scripting languages and well suited to what you want to do.

Danish
  • 3,708
  • 5
  • 29
  • 48
  • You can find several options to do a pattern based rename at this stack overflow page - http://stackoverflow.com/questions/225735/batch-renaming-of-files-in-a-directory . In my opinion, the code looks much cleaner and readable than the batch file version. – Danish Nov 13 '11 at 14:42