2

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:

Organizing_your_day_in_acute_care_-_Flash_(Medium)_-_20120516_12.04.02PM.mp4
Developing_a_Business_Plan_-_IT_Projects_-_Flash_(Medium)_-_20120529_09.39.09PM.mp4
GI_Medications_-_Flash_(Medium)_-_20120607_11.35.50AM.mp4
Effectiveness_and_Data_and_Information_Set_-_Flash_(Large)_-_20120208_02.41.28PM.mp4
Adult_Learning_-_Theory_-_Flash_(Medium)_-_20100519_05.25.02PM.mp4

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
1

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")
endlocal
|improve this answer|||||
1

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
Next

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
1

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.