Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
How can I mass rename files from the command line or using a 3rd party tool?

We have several files with filenames similar to below:


What I'd like to do is create a batch script to rename these files so that all the underscores are removed and everything after the "_-_Flash" string is removed, essentially leaving the files as so:

Organizing your day in acute care.mp4
Developing a Business Plan - IT Projects.mp4
GI Medications.mp4
Effectiveness and Data and Information Set.mp4
Adult Learning - Theory.mp4

Any help would be greatly appreciated.

share|improve this question

migrated from Sep 3 '12 at 6:59

This question came from our site for system and network administrators.

marked as duplicate by splattne, Indrek, Nifle, RedGrittyBrick, 8088 Sep 3 '12 at 16:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

on a server so it has to be done via batch / cmd line... 3rd part tool will not work – rj454me Sep 3 '12 at 7:10
Follow the link - there is one PowerShell solution which may apply to your problem. – splattne Sep 3 '12 at 7:48

This .bat should work, but keep in mind that in case you have files that don't follow given filename pattern, results will be unpredictable:

setlocal EnableDelayedExpansion
FOR /F "usebackq delims==" %%i IN (`dir /b *.mp4`) DO (
    set oldfilename=%%i
    set oldfilename=!oldfilename:^(=!
    set oldfilename=!oldfilename:^)=!
    set endbit=!oldfilename:*_-_Flash=_-_Flash!
    call set newfilename=%%oldfilename:!endbit!=%%
    set newfilename=!newfilename:_= !
    rename "%%i" "!newfilename!.mp4")
share|improve this answer

Might be easier to do this in VBScript

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder(".")
  ext = LCase(fso.GetExtensionName(f))
  If ext = "mp4" Then
    f.Name = Replace(Left(s, InStr(s, "_-_Flash")-1), "_", " ") & "." & ext
  End If

or in PowerShell

Get-ChildItem -Filter *.mp4 | ForEach-Object {
  $newname = $_.Name -replace "_-_Flash.*(.mp4)", '$1'
  $newname = $newname -replace "_", " "
  Rename-Item $_.FullName $newname
share|improve this answer
Doesn't f.Name have name sans extension, so you don't need last & "." & ext? – Oleg V. Volkov Sep 3 '12 at 13:47
@OlegV.Volkov: No. – Ansgar Wiechers Sep 3 '12 at 14:35

Since you're on Windows, you can use full power of JScript + ActiveX to automate pretty much any task in JavaScript and I, personally, think it just looks cleaner.

Save following somewhere as rename.js:

var shell = new ActiveXObject("Shell.Application")
var folder = shell.NameSpace("D:\\1\\test") // <--- your folder name here
// or interactive: var folder = shell.BrowseForFolder(0, "Select folder to clean-up", 0)
var items = folder.Items()
for (var idx = 0; idx < items.Count; idx++) {
   var file = items.Item(idx)
   var filename = file.Name
   var new_filename = filename.replace(/_\-_Flash_.*/, '').replace(/_/g, ' ')
   if (new_filename != filename) {
      file.Name = new_filename
      // You can uncomment next line if you run in console.
      // WScript.Echo(filename + ' -> ' + new_filename)

And run with either wscript.exe (GUI) or cscript.exe (console).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged .