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.

  • 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")
|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
|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).

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.