Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Delete temporary file sent through StreamContent in ASP.NET Web API HttpResponseMessage

Recently I had to delete a temporary (temp) file/resource sent through HttpResponseMessage. The code I tried first was like this:

var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var response = new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new StreamContent(fileStream);
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };
File.Delete(filePath);
return response;

Fooling myself I didn’t notice that one cannot do File.Delete(filePath); before returning the file because it’ll be deleted from the server disk before arriving at the client – see that response.Content receives a StreamContent, that is, the file is yet to be streamed to the client – only after it’s been streamed is that we can delete the file.

This is the exception that was being logged:

20131028 18:29:10 435 [7299] ERROR com.itvizion.data.services.Controllers.DataTablesController - Unhandled exception during file action Could not find a part of the path 'C:\ITVizion\MyProject\FileExportService\Files\datatable.csv'.

Sure it could not find the file, it had already been deleted. D'oh

So how can we dispose a managed resource (in this case the file) after the client has successfully downloaded it? That’s a damn good question I made myself and Google yesterday.

Googling I found this StackOverflow question:

How to delete the file that was sent as StreamContent of HttpResponseMessage

The asker pointed me in the right direction. One has to inherit from HttpResponseMessage and override the Dispose method like this:

public class FileHttpResponseMessage : HttpResponseMessage
{
    private string filePath;

    public FileHttpResponseMessage(string filePath)
    {
        this.filePath = filePath;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        Content.Dispose();

        File.Delete(filePath);
    }
}

Note the order of the calls inside the Dispose method. This is an essential part that differs from the hint given by the SO question I mentioned above.

Now what’s left is put FileHttpResponseMessage to use like this:

var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

var response = new FileHttpResponseMessage(filePath);
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = fileName
};

return response;

FileHttpResponseMessage will take care of calling Dispose on the correct time deleting the temporary file from the server disk right after the client has finished downloading the file.

Cleaning up the server disk is a good practice after all. This can avoid future headaches.

Hope it helps.

Manage folders & files in your ASP.NET MVC app with elFinder.Net

Recently I had to evaluate what were my options when it comes to managing folders and files in an ASP.NET MVC project – a files manager somewhat like what a Content Management System does but I needed something way simpler and intuitive and principally of easy integration.

The Story
As part of a project requirement, a super user wants to able to offer other users some files like PDFs, Images, Videos, etc. This super user should be capable of creating a folder structure as he sees fit and then upload files to a central point (the server). All this looks like a difficult task to accomplish through a client server architecture since we’re dealing with clients and their files that need to go to the server. At first I thought about using a SharePoint module/web part but then it seemed overkill for what I wanted.

As always I just put some simple words in a Google search and to my surprise I found an outstanding open source project called elFinder by Studio 42. Here’s their ASCII art for your amusement:

      _ ______ _           _           
     | |  ____(_)         | |          
  ___| | |__   _ _ __   __| | ___ _ __ 
 / _ \ |  __| | | '_ \ / _` |/ _ \ '__|
|  __/ | |    | | | | | (_| |  __/ |   
 \___|_|_|    |_|_| |_|\__,_|\___|_|   

elFinder is an open-source file manager for web, written in JavaScript using jQuery UI. Creation is inspired by simplicity and convenience of Finder program used in Mac OS X operating system.

After going through the readme file for a moment, I thought: Oh my god, this is just what I need. The only drawback in my case was that the connector (server part) of elFinder is written in PHP and I’m developing an ASP.NET MVC app. Then let me go to Google once again and type elFinder ASP NET. To my surprise there are some ports to the .NET arena but they seem to lack integration simplicity, that is, the dependencies these ports need just interfere with my current code as was the case of the elFinder ASP.NET Connector that uses Autofac as a dependency. I use Castle Windsor for the IoC part and then this was a no go. A more careful read at the original elFinder wiki let me know about different ports of this amazing project: 3rd party connectors, plugins, modules. This page pointed me to ElFinder.NET connector for elFinder 2.x hosted at CodePlex.

I downloaded ElFinder.NET’s source code that comes with a sample ASP.NET MVC app and played with it. When I saw the amount of features available [ Client options + Connector options ] I almost cried in excitement Crying face. ElFinder.NET is a very well written and organized port put together by Evgeny Noskov. Congrats to him and to Studio 42 from Russia for sharing the code with the community. It works great and so I started integrating it with my ASP.NET MVC 4 app.

Along the way I hit some missing features that I saw are part of the original elFinder by Studio 42. They are startPath and uploadMaxSize. I contacted Evgeny through the CodePlex contact form and asked him about the option to set a start path in the root directory. Thinking he wouldn’t even answer me, I decided to implement it myself looking at the original elFinder codebase. Today I got an e-mail from Evgeny telling me that he just added the start path option to elFinder.Net. What a joy! Then I wanted to set a max upload size and I just saw that this option was also not present in elFinder.Net. Asked about it in this discussion and Evgeny promptly added it to the library. I even had no time to try to implement this one… he went even further and added a must have feature that I didn’t notice was missing: file download.

So after all this amazing story of “is giving that you receive” I decided to spread the word and write a blog post about elFinder.net…

It’s enough of background info. Let me show you one simple use case and how you can take advantage of such outstanding open source project.

The Use Case
Let’s say you want to let one super user with read/write permissions create a folder structure inside a given root directory in the server and upload files there. Other users accessing the app will then be presented with a page that has links that point to the 1st level folders of that root directory. These users will be able to only read those folders/files uploaded by the super user. Note: this app has some kind of membership implemented with roles and permissions granted to users. I’ll omit this part in this sample code for simplicity sake.

The super user has access to a screen (Files menu option) like the following one that contains elFinder files manager:

Figure 1 - elFinder.Net file manager UI styled with jQuery UI supporting any folder depth, file/folder upload/download, delete, rename, copy/cut/paste, preview, properties, drag and drop, etc.Figure 1 - elFinder.Net file manager UI styled with jQuery UI supporting any folder depth, file/folder upload/download, delete, rename, copy/cut/paste, preview, properties, drag and drop, etc.

Other users when logging in for example will have access to a page (Home menu option) listing the 1st level folders like this:

Figure 2 - 1st level folders links allow users to click on them and have the selected folder opened in elFinder’s file manager automagicallyFigure 2 - 1st level folders links allow users to click on them and have the selected folder opened in elFinder’s file manager automagically

When clicking the folder name link, the user will be sent to elFinder’s file manager and the folder will be selected automatically showing its content to the user. The user without super powers then is allowed only to read the folder content and if they want they can even download the files to their machines. The possibilities are endless…

The Code
The full code is available at this GitHub repo: https://github.com/leniel/elFinder.Net

You’ll find comments throughout the code. Make sure you read them carefully.

Let’s start defining two action methods in the HomeController.

public partial class HomeController : Controller
{
    [GET("")]
    public virtual ActionResult Index()
    {
        DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Files/MyFolder"));
        // Enumerating all 1st level directories of a given root folder (MyFolder in this case) and retrieving the folders names.
        var folders = di.GetDirectories().ToList().Select(d => d.Name);

        return View(folders);
    }

    [GET("FileManager/{subFolder?}")]
    public virtual ActionResult Files(string subFolder)
    {
        // FileViewModel contains the root MyFolder and the selected subfolder if any
        FileViewModel model = new FileViewModel() { Folder = "MyFolder", SubFolder = subFolder };

        return View(model);
    }
}

The Index action method corresponding View has this code:

@model IEnumerable<string>

@{
    ViewBag.Title = "Index";
}

Available 1st level Folders - clicking will navigate you to the File Manager setting the selected folder as elFinder's start path.

<ul>
    @foreach (string folder in Model)
    {
        <li><a href="@Url.Action(MVC.Home.ActionNames.Files, MVC.Home.Name, new { subFolder = folder })">@folder</a> </li>
    }
</ul>

The Files action method corresponding View has this code:

@model FileViewModel

@{
    ViewBag.Title = "Files";
}

@Html.Partial(MVC.Shared.Views.FilesForm, Model)

The FilesForm partial view has elFinder’s client side interesting pieces of code:

@model FileViewModel

@{
    ViewBag.Title = "Files";
}

@* Bundles with elFinder's CSS and JavaScript files configured in App_Start\BundleConfig.cs*@
@Styles.Render("~/Content/elfinder")
@Scripts.Render("~/Scripts/elfinder")

<script type="text/javascript">
    $(function ()
    {
        var myCommands = elFinder.prototype._options.commands;

        var disabled = ['extract', 'archive', 'resize', 'help', 'select']; // Not yet implemented commands in ElFinder.Net

        $.each(disabled, function (i, cmd)
        {
            (idx = $.inArray(cmd, myCommands)) !== -1 && myCommands.splice(idx, 1);
        });

        var selectedFile = null;

        var options = {
            url: '/connector', // connector route defined in the project folder App_Start\RouteConfig.cs
            customData : { folder : '@Model.Folder', subFolder: '@Model.SubFolder' }, // customData passed in every request to the connector as query strings. These values are used in FileController's Index method.
            rememberLastDir: false, // Prevent elFinder saving in the Browser LocalStorage the last visited directory
            commands: myCommands,
            //lang: 'pt_BR', // elFinder supports UI and messages localization. Check the folder Content\elfinder\js\i18n for all available languages. Be sure to include the corresponding .js file(s) in the JavaScript bundle.
            uiOptions: { // UI buttons available to the user
                toolbar: [
                    ['back', 'forward'],
                    ['reload'],
                    ['home', 'up'],
                    ['mkdir', 'mkfile', 'upload'],
                    ['open', 'download'],
                    ['info'],
                    ['quicklook'],
                    ['copy', 'cut', 'paste'],
                    ['rm'],
                    ['duplicate', 'rename', 'edit'],
                    ['view', 'sort']
                ]
            },

            handlers: {
                select: function (event, elfinderInstance) {

                    if (event.data.selected.length == 1) {
                        var item = $('#' + event.data.selected[0]);
                        if (!item.hasClass('directory')) {
                            selectedFile = event.data.selected[0];
                            $('#elfinder-selectFile').show();
                            return;
                        }
                    }
                    $('#elfinder-selectFile').hide();
                    selectedFile = null;
                }
            }
        };
        $('#elfinder').elfinder(options).elfinder('instance');

        $('.elfinder-toolbar:first').append('<div class="ui-widget-content ui-corner-all elfinder-buttonset" id="elfinder-selectFile" style="display:none; float:right;">'+
        '<div class="ui-state-default elfinder-button" title="Select" style="width: 100px;"></div>');
        $('#elfinder-selectFile').click(function () {
            if (selectedFile != null)
                $.post('file/selectFile', { target: selectedFile }, function (response) {
                    alert(response);
                });
               
        });
    });
</script>

<div id="elfinder"></div>

The last part is the FileController that gets called in every connector request:

public partial class FileController : Controller
{
    public virtual ActionResult Index(string folder, string subFolder)
    {
        FileSystemDriver driver = new FileSystemDriver();

        var root = new Root(
                new DirectoryInfo(Server.MapPath("~/Files/" + folder)),
                "http://" + Request.Url.Authority + "/Files/" + folder)
        {
// Sample using ASP.NET built in Membership functionality... // Only the super user can READ (download files) & WRITE (create folders/files/upload files). // Other users can only READ (download files) // IsReadOnly = !User.IsInRole(AccountController.SuperUser)
            IsReadOnly = false,
Alias = "Files", // Beautiful name given to the root/home folder MaxUploadSizeInKb = 500 // Limit imposed to user uploaded file <= 500 KB }; // Was a subfolder selected in Home Index page? if (!string.IsNullOrEmpty(subFolder)) { root.StartPath = new DirectoryInfo(Server.MapPath("~/Files/" + folder + "/" + subFolder)); } driver.AddRoot(root); var connector = new Connector(driver); return connector.Process(this.HttpContext.Request); } public virtual ActionResult SelectFile(string target) { FileSystemDriver driver = new FileSystemDriver(); driver.AddRoot( new Root( new DirectoryInfo(Server.MapPath("~/Files")), "http://" + Request.Url.Authority + "/Files") { IsReadOnly = false }); var connector = new Connector(driver); return Json(connector.GetFileByHash(target).FullName); } }

Hope it helps.

Updated PowerShell script to insert copyright notice/banner/header in all source code files

In a recent post I described how one could insert a copyright banner in all source code files located on a given path. You can read all the details about it here: Inserting copyright notice/banner/header in all source code files with PowerShell script

During the past month or so I updated that PowerShell script to give it even more power. In the Notes section of that post I mentioned this point:


  • Beware that if you run the script twice it'll add the copyright notice twice in each file

With this updated version (see the highlighted parts) I’ve overcome such limitation.

I’m now also using the built-in Powershell Filter keyword to filter the files, that is, processing only .cs files (C# code files) excluding some other files that have the .cs extension but that have a given naming pattern. I exclude those files because it doesn’t make sense to add copyright to them and because in some cases it would break the build.

Here’s the updated PowerShell script with comments to help you understand what’s going on in each line:

param($target = "C:\MyProject\trunk", $companyname = "Leniel’s Software House")

#[System.Globalization.CultureInfo] $ci = [System.Globalization.CultureInfo]::GetCultureInfo("pt-BR")

[System.Globalization.CultureInfo] $ci = [System.Globalization.CultureInfo]::GetCurrentCulture

# Full date pattern with a given CultureInfo
# Look here for available String date patterns: http://www.csharp-examples.net/string-format-datetime/
$date = (Get-Date).ToString("F", $ci);

# Header template
$header = "//-----------------------------------------------------------------------

// <copyright file=""{0}"" company=""{1}"">

// Copyright (c) {1}. All rights reserved.

// <author>Leniel Macaferi</author>

// <date>{2}</date>

// </copyright>

//-----------------------------------------------------------------------`r`n"

function Write-Header ($file)
{
    # Get the file content as as Array object that contains the file lines
    $content = Get-Content $file
    
    # Getting the content as a String
    $contentAsString =  $content | Out-String
    
    <# If content starts with // then the file has a copyright notice already
       Let's Skip the first 14 lines of the copyright notice template... #>
    if($contentAsString.StartsWith("//"))
    {
       $content = $content | Select-Object -skip 14
    }

    # Splitting the file path and getting the leaf/last part, that is, the file name
    $filename = Split-Path -Leaf $file

    # $fileheader is assigned the value of $header with dynamic values passed as parameters after -f
    $fileheader = $header -f $filename, $companyname, $date

    # Writing the header to the file
    Set-Content $file $fileheader -encoding UTF8

    # Append the content to the file
    Add-Content $file $content
}

#Filter files getting only .cs ones and exclude specific file extensions
Get-ChildItem $target -Filter *.cs -Exclude *.Designer.cs,T4MVC.cs,*.generated.cs,*.ModelUnbinder.cs -Recurse | % `
{
    <# For each file on the $target directory that matches the filter,
       let's call the Write-Header function defined above passing the file as parameter #>
    Write-Header $_.PSPath.Split(":", 3)[2]
}
Hope you make even better use of such a pearl that comes in handy from time to time.

Inserting copyright notice/banner/header in all source code files with PowerShell script

I have a better version of this PowerShell script if you’re interested.
I suggest you read this post first (there’s a bonus point) then go take a look at the new one here:
Updated PowerShell script to insert copyright notice/banner/header in all source code files

Today my client asked for the source code of a project we worked together. He wants to submit it to patent review and he wanted to include a copyright notice in each C# code file (.cs).

I googled about a way of accomplishing this and found this post:

Use a Visual Studio Macro to Insert Copyright Headers into Source Files

It looked promising but as I’m working with Visual Studio 2012 I just hit a road block because it dropped the support for Macros. So I had to find some other way… in the meantime I asked a question at StackOverflow to see if someone had any good advice regarding this. Right now there’s one nice answer but it requires manual work. I’m really interested in an automated process (do the work in a batch fashion) because there are lots of .cs files in the Visual Studio solution comprised of 7 projects.

Some more Googling with the right words (read PowerShell) and I was able to find something that could do the work. Kishor Aher wrote about it Powershell – Copyright header generator script. Oh God, the internet is really amazing thingy! Remember to just search for the right words…

Now it was just a matter of adapting the PowerShell script to my needs. Here is it:

param($target = "C:\MyProject", $companyname = "My Company", $date = (Get-Date))

$header = "//-----------------------------------------------------------------------

// <copyright file=""{0}"" company=""{1}"">

// Copyright (c) {1}. All rights reserved.

// <author>Leniel Macaferi</author>

// <date>{2}</date>

// </copyright>

//-----------------------------------------------------------------------`r`n"

function Write-Header ($file)
{
    $content = Get-Content $file

    $filename = Split-Path -Leaf $file

    $fileheader = $header -f $filename,$companyname,$date

    Set-Content $file $fileheader

    Add-Content $file $content
}

Get-ChildItem $target -Recurse | ? { $_.Extension -like ".cs" } | % `
{
    Write-Header $_.PSPath.Split(":", 3)[2]
}

As you see above, the script will place the copyright notice in every .cs file that lies within the folder C:\MyProject. So let’s say you have a Visual Studio solution with various projects – the script will traverse all files placing the notice in every .cs file it finds along the way…

I named the script Copyright.ps1.

To make this work I just opened PowerShell command line, navigated to the folder where I placed the script file and typed .\Copyright.ps1 to run the script:

Executing Copyright PowerShell script using PowerShell command line toolFigure 1 - Executing Copyright PowerShell script using PowerShell command line tool

You’re done!

Enjoy.

Notes

  • Close Visual Studio solution before running the PowerShell script. Otherwise you may get some error message telling you that some file is in use.
  • Be sure to have your source code files commited in a source control repository before running the scripts so that if anything goes wrong you can just revert to the previous state.
  • Beware that if you run the script twice it'll add the copyright notice twice in each file.

A big thank you to Kishor for providing us the PowerShell script. I’m not that experienced with PowerShell but adapting existing scripts is something easy to do IMHO.

Bonus
To get support for PowerShell scripting in Visual Studio with syntax highlighting and IntelliSense, there’s an awesome and free product out there. It’s called PowerGUI. In order to use it, download PowerGUI here and then install its accompanying Visual Studio extension available here. As I’m using Visual Studio 2012 I had to install the alpha release available here.

See for yourself the awesomeness:

PowerGUI editing PowerShell script with full IntelliSense support and Code Highlighting
Figure 2 - PowerGUI editing PowerShell script with full IntelliSense support and Code Highlighting

Listing compressed files within a .zip or .rar by file size

I'd like to know any app that shows a list of the files that lie within a compressed file .zip or .rar. If the list could be ordered by file size it'd be great! It could be any software that runs on Windows or Mac OS since I use both OS using Parallels.

Why this is important to me?
The answer is that sometimes I need to send a compressed file to someone and this file generally contains lots of coding files that are part of a Visual Studio Solution/Project for example. I just want to send a subset of those files. There are some big files like DLLs and database files scattered everywhere in a lot of folders that don’t need to go with the compressed file.

So how can I avoid spending time going through all those folders/files, looking their size and deleting one by one to make the final compressed file to be uploaded smaller?

I thought about asking this question at SuperUser. To tell the truth I already had the question typed there but before I could post it and after a few tries by myself I got what I wanted using WinRAR with an easy play of menu commands. So how can you do this? Follow me…

1 - If you don’t know or don’t have WinRAR or use another file archiver, you can get it at:
http://www.rarlab.com/download.htm

I use WinRAR in its evaluation copy incarnation that once in while pops up a dialog asking you to purchase it. Smiley surpreso It works great even if you don’t want to purchase it… take it easy.

2 - At first compress the folders and files you want without getting bothered about the size of the resulting compressed file. After installing WinRAR it’s available in the context menu of any file or folder in Windows Explorer.

3 - Open the compressed file in WinRAR and play with the menu just a little bit. Go to the Options | File list and select Flat folders view and Details menu options.

WinRAR listing the files of a compressed file (trunk.rar) by file size
Figure 1 - WinRAR listing the files of a compressed file (trunk.rar) by file size

Taking the above screenshot into consideration (it lists the content of a trunk project folder compressed), we can see that it has 1554 folders and 1624 files. This compressed file alone has 87 MB in disk when the actual uncompressed content has 193 MB in size but it’s still pretty big to send it over the wire… I have a lot of folders and files to go through if I want to delete the big and unnecessary files to try to decrease the final size! As you see, it also contains many duplicate files in different paths… it’s pretty daunting just to think about a manual scan. A visual scan in a single screen is much better. How do you get a big picture of this compressed file?

4 - Order all the files by size by clicking the size column header.

5 - Select all the files you want to remove/delete from the compressed file in just one go. One can do this in two different ways: clicking a file and holding the ctrl key and clicking another file to select not contiguous files OR selecting one file and pressing the shift key and clicking another file to select contiguous files. After this it’s just a matter of right clicking the file selection and executing the Delete files command.

Hope this useful tip helps you someday!

Batch renaming and copying MP3 values from tag to tag

This is just one more post in the MP3 series where I write about how you can organize your MP3 library/collection performing batch renaming to correct MP3 metadata and save time.

Just after I posted about Using Regular Expressions to correct mistagged MP3 I had other task to accomplish with Mp3tag but I didn’t know how to do that. What I want now is somewhat like what I showed in that previous post. The difference is that now I want to use the content of a given tag to fill another tag, thus copying/cutting that content from tag to tag.

As always I have a feeling that there must be a way of doing what I want and as always I go after it.

Here’s the problem this time:

Title tag has both the Title and Artist names and Artist tag has a wrong valuePicture 1 - Title tag has both the Title and Artist names and Artist tag has a wrong value

As you see in Picture 1, I have some MP3 files that have the Artist name included in the Title tag. The Artist name is between brackets while the Artist tag has the text Various. Obviously this is wrong because the Artist name should be in its proper Artist tag.

How can we correct this without manual intervention that would be a time consuming task?
The simple answer is using Mp3tag and its powerful set of features like the one called Guess values.

Guess values is so amazing that its window dialog just takes 2 parameters to get the job done. They are: Source format and Guessing pattern.

The Source format field in this case has the value %title% and the Guessing pattern field has the value %title% (%artist%). You can use any mp3 tag field between percent signs (%). What do these values mean?

Source format retrieves the value present in the Title tag of each MP3 and by guessing the values through the Guessing pattern it’s possible to split the Title tag according to the pattern. This opens up a world of possibilities where you can use any kind of combinations (masks/patterns) to split the value of a given tag and have those split values fill any MP3 tag of your choice.

So, in this case we have something as this happening behind the curtains:

Title: Begins With Me (Point of Grace)

Source format = %title%
Guessing pattern = %title% (%artist%)

Making the substitution, we have:

Guessing pattern = %Begins With Me% (%Point of Grace%)

With this, Mp3tag now knows what values it must use to fill the MP3 tags we want correctly.

Here’s the final result after you press the OK button present in Picture 1:

Correctly tagged MP3 files
Picture 2 - Correctly tagged MP3 files

Hope you got the idea and make good use of this.

I already have the material to write the next post in this series. Keep an eye in this blog. :)

Using Regular Expressions to correct mistagged MP3

Two months have passed since I last posted something here. These were 2 busy months in my life. Hooray, I bought a brand new car and got my driver license, not necessarily in this order.

This post is about something I had planned to write sometime ago… the two screenshots shown here I got maybe 3 months ago. :D So let’s get to it.

As a big and eclectic fan of music that I’m, every now and then I see mistagged MP3 files like the ones with title tags that contain both the artist name and the song name. The following picture shows what I mean:

Mistagged MP3 files (Title field has both the Artist and Song names) Picture 1 - Mistagged MP3 files (Title field has both the Artist and Song names)

Here’s where Mp3tag comes to the rescue. As you see in Picture 1, I’m using the dialog “Replace with regular expression”. Read my previous post about this great piece of software called Mp3tag to see how to get to this dialog. It has 3 fields that you must fill to make some magic happen allowing you to correct those wrongly tagged/mistagged MP3 titles all at once. Ha! You won’t lose your precious time correcting MP3 by MP3. I know that this is boring and that’s why I desperately searched for a solution. I know that if you’re reading this, you’re probably in the same situation and you just found a solution. :)

I’ve chosen the Field TITLE since it’s the problematic field in this particular case. Now the most important part, the so called Regex or Regular expression: (.*) - (.*). This thing means that we’re gonna separate the MP3 Title field in two parts. One part will have everything (.*) before the hyphen - and the other part will contain everything after the hyphen (.*).

Example:

Dru Hill - Away (Prod. by B.Cox) (Full + NoShout) (2010)

The regex (.*) - (.*) will separate the MP3 title above in two parts…

$1 = Dru Hill
$2 = Away (Prod. by B.Cox) (Full + NoShout) (2010)

The Replace matches with field has the value $2 because in this case I want to replace/substitute the MP3 Title with only the Song/Track name (the 2nd part/match of the regex above). If instead I wanted to keep the Artist name in the Title tag (D'oh!, not something I’d want to do), I’d write $1 in this field.

Now, take a look at Picture 2. When you click OK, this is the end result/magic you get. Nice and correctly titled/tagged MP3 files. The way I wanted them to be.

Correctly tagged MP3 filesPicture 2 - Correctly tagged MP3 files

To make things last forever, do not forget to click the Save button present in Mp3tag’s toolbar or in the Save tag option present in the File menu. I like to press Ctrl+S as a shortcut.

If you want to learn the basics about regular expressions to use with Mp3tag, check this out: http://help.mp3tag.de/options_format.html#regexp

As you see, using regexes (one of the most powerful features of computers) you can make any kind of change to your MP3 tags like for example removing that (2010) present in each MP3 Title field above. That 2010 should be in its proper MP3 tag, namely the Year tag. Don’t ya think?

Hope this simple process helps someone out there keep an organized MP3 library as I do like to keep mine.

Note
Mp3tag is a Windows only application as is Windows Live Writer that I use to write these blog posts. I use/run it through Parallels Desktop on my Mac mini. Read this post to get more info about how to run Windows side by side with your Mac OS.

Software to add Lyrics to MP3 files ID3 metadata

Take a look at the MP3 series. Probably you’ll find something interesting.

If you're like me, you also like to take a look at the lyrics of music that is currently playing in your computer or mobile device as the iPhone. I do it to learn a bit more of English since its not my main language and of course because I also want to sing along correctly. :)

The iPhone for example allows you to read the lyrics of the current song if the lyrics are present in the MP3 ID3 metadata container. This is pretty cool. If you already have lyrics embedded in your MP3 files you can see them while in a bus trip, waiting for a service, etc.

Every MP3 has a specific field (also know as frame) in its metadata to store lyrics information. It’s just a matter of filling this field with the correct lyrics. This is a hard work to do manually because you have to search for the lyrics and then copy/paste it in the right field. This sounds like a great thing to be done by software instead. Again, that’s what computers are for… save us time.

In iTunes (the media player/library software I use) for example, one would right-click a music file and then select the Get Info context menu option. Then you’d select the Lyrics tab and paste the lyrics in the white huge field making sure to click OK as seen in Figure 1 below:

Adding lyrics to an iTunes music file through the Lyrics tab
Figure 1 - Adding lyrics to an iTunes music file through the Lyrics tab

Some time ago I asked a question at SuperUser site: Software to add Lyrics to MP3 files ID3 metadata. It seems that there are a lot of people (3,356 to be precise as the time of this post) out there looking to accomplish what this post tries to clarify.

When I asked the question I was using Windows and I got good answers.

I also discovered other software by myself as MiniLyrics for Windows at that time. If you’re interested in MiniLyrics, here goes a small tutorial to save the lyrics to MP3 metadata!

MiniLyrics
Right click MiniLyrics icon in the system tray, choose Preferences and then select the Lyrics icon. Under the Save downloaded lyrics in: - select Save lyrics in mp3 file.

Other great feature MiniLyrics has is that while the music is playing the lyrics can be shown on your screen according to what is being sung, that is, the lyrics flows in your screen according to the music timing. Fantastic job from crintsoft people... :)

Besides saving the lyrics to MP3 metadata, there are lots of features and possibilities when it comes to lyrics in MiniLyrics software.

From the official site:

    MiniLyrics Display lyrics for your favorite music!

        * Lyrics plugin software for iTunes, Windows Media Player, MediaMonkey, Winamp, etc. You do not need to change the way you enjoy music.
        * Display scrolling lyrics, you can follow along with the artist and catch every word.
        * Automatically search and download lyrics.
        * Huge lyrics database, and it is expanding everyday.
        * Free Trial version that never expires.

Lyricator
Lyricator as suggested by merv is a fantastic/great/cool piece of software to go with MediaMonkey but it is having some problems currently as you can find in this thread.

I had to resort to other service while Lyricator is being repaired. I found other free software that does the job, but only on a Mac computer (that’s OK because I’ve switched to the Mac world). It's name is Get Lyrical.

Get Lyrical

Get Lyrical doing its job in the background
Figure 2 - Get Lyrical doing its job in the background

    Get Lyrical auto-magically add lyrics to songs in iTunes!

You can choose either a selection of tracks, or the current track. Or turn on "Active Tagging" to get lyrics for songs as you play them.

    You can also browse and edit the lyrics of your iTunes tracks right from Get Lyrical.

I highlighted in yellow above a powerful feature of Get Lyrical. You can even add lyrics to a selection of tracks at once. This is a batch processing feature really welcome when you want to add lyrics to an artist’s complete discography for example.

I’ve been using Get Lyrical for some time now and it is really competent in the job. I highly recommend it.

Add Songs to iTunes Playlist with Automator

Last time I showed you how to Automate tasks in Mac OS with Automator. I used Automator to create a simple workflow that helps moving MP3 files to iTunes folder. Check that here.

Now I have another task to Automator.

This is the description of what I want to accomplish or the problem description if you prefer:

I often download some free legal MP3 files from the internet for evaluation, more specifically from Indie Rock Cafe (great great site by the way :D - check it out if you like Indie Rock music). I use IndieRockCafe mainly to discover bands that I’ve never heard about. It’s being a great experience so far. I’ve come to know some really good bands that, else I would never hear a song of theirs.

To download those MP3s I use DownThemAll that is a really nice piece of software that works beautifully with Firefox. I wrote about DownThemAll in Automate the download of a list of URLs/links. Although I didn’t write how to download only MP3 files with DownThemAll, this post gives you an idea about the purpose of DownThemAll. I’ll write about how to download only specific kind of files (MP3 in this case) using DownThemAll, but that’s another post. Probably I’ll detail this download process I’m mentioning here.

As I was telling you, I often do the same task, that is, I go to IndieRockCafe, click on DownThemAll icon in my Firefox toolbar and tada, the download of IndieRockCafe’s recently added MP3s just start. DownThemAll will download only page links that point to MP3 files saving these MP3 in a single folder called IndieRockCafe.

After the download I used to select all the files within that folder and drag and drop them inside an iTunes playlist called IndieRockCafe. iTunes is wise enough to tell me that some files are already part of the playlist and gives me the option to skip them adding only new files into that playlist. Doing so I always have a fresh playlist with the latest files of IndieRockCafe. It works, but it has a lot of manual steps.

Yesterday I thought: the above steps are a perfect fit to be automated with Automator.

Let’s create a workflow:

1 - Go to the Applications folder and select Automator.

2 - You’ll be presented with the following screen to choose a template for your workflow. Select Folder Action as the template.

Types of templates available to create an Automator workflow (Folder Action)Figure 1 - Types of templates available to create an Automator workflow (Folder Action)

3 - In Folder Action receives folders and files added to, select the folder you want. In my case it is the IndieRockCafe folder.

4 - Now select Music in Library list and then select Import Files into iTunes under the Actions list. Drag this action to the workflow area in the right.

5 - Select Existing playlist and the playlist you want the files to go to. As I wrote above I already have a Playlist called IndieRockCafe inside iTunes. So I selected it.

6 - Go to the File menu and select Save. Give the workflow an appropriate name, e.g. IndieRockCafe.

The following screenshot shows the Folder Action workflow configured:

IndieRockCafe.workflow configured according to the six steps described above
Figure 2 - IndieRockCafe.workflow configured according to the six steps described above

7 - Now that the workflow is created, there’s a last step required to orchestrate things: go to the IndieRockCafe folder and right-click it. Select Services > Folder Actions Setup… Make sure you attach the IndieRockCafe workflow to this folder as shown in Figure 3:

Attaching IndieRockCafe.workflow in Folder Actions Setup
Figure 3 - Attaching IndieRockCafe.workflow in Folder Actions Setup

Make sure you click the Enable Folder Actions checkbox too:

Enabling Folder Actions and turning IndieRockCafe.workflow ON for the IndieRockCafe folder
Figure 4 - Enabling Folder Actions and turning IndieRockCafe.workflow ON for the IndieRockCafe folder

… and we’re done! As you see this is totally life saver.

Every new MP3 that gets added in my IndieRockCafe folder through DownThemAll or that I manually place in this folder will be automatically added in IndieRockCafe playlist.

Task successfully automated!

I’m a music lover and I hope you can take advantage of it too.

Important
DownThemAll creates file segments ( *.dtapart files ) while downloading. DownThemAll splits the file into several parts and then downloads each segment of the file individually, which gives you better speed from servers, especially those that choose to limit your download speed. This behavior will cause the workflow created above to fail because iTunes won’t recognize those parted files when trying to import them. To solve this problem, do the following:

In Firefox Tools menu choose More dTa Tools and then select Preferences.

In tab Advanced and under Temporary files choose a directory to store those dtapart files. See screenshot below to have an idea:

Using a temporary folder to store DownThemAll file parts or segmentsFigure 5 - Using a temporary folder to store DownThemAll file parts or segments

Doing the above, DownThemAll will store those partial files in a separate folder. When it finishes downloading a file it will join its parts and then will move that file to the IndieRockCafe folder I specified in the workflow. Now iTunes will import the MP3.

Note
Folder action workflows are saved in
/Users/YourUserName/Library/Workflows/Applications/Folder Actions

Download
You can download this workflow at:
https://sites.google.com/site/leniel/blog/IndieRockCafe.workflow.zip

Automate tasks in Mac OS with Automator

This one is about an incredible application that comes with Mac OS X. Its name is Automator.

Jesus Christ, this is a life saver app!

This is the description of Automator:Automator Apple Mac OS application icon

With Automator, you can accomplish time-consuming, repetitive manual tasks quickly, efficiently, and effortlessly. Automator lets you skip the complex programming and scripting that is normally required to create automations. Instead, you assemble individual steps into a complete task by dragging these actions into an Automator workflow. Automator comes with a library of hundreds of actions. And with the Watch Me Do action, you can record an action — such as pressing a button or controlling an application without built-in Automator support — and replay it as an action in a workflow.

I have lots of MP3 files that I need to go through analyzing if I really want to keep them in my media library. During the last year I stored all those MP3 in a folder and such a folder is now 55.56 GB and contains 11840 files to be precise. That’s a lot of MP3! I keep postponing this open/listen to task but today I thought I’d start. That’s where Automator fits the job.

In this post I’ll show you how to create a simple workflow that helps moving the MP3 files to iTunes folder /Users/leniel/Music/iTunes/iTunes Media/Automatically Add to iTunes. The folder Automatically Add to iTunes is a special folder that iTunes keeps watching for new files added to it. When a file is added in this folder, iTunes automatically adds it to the media library using MP3 metadata to organize the library. When added files will reside in /Users/leniel/Music/iTunes/iTunes Media/Music.

Let’s create the workflow:

1 - Go to the Applications folder and select Automator.

2 - You’ll be presented with the following screen to choose a template for your workflow. Select Service as the template.

Types of templates available to create an Automator workflow
Figure 1 - Types of templates available to create an Automator workflow

3 - In Service receives selected, select audio files. “In” select Finder.

4 - Now select Files & Folders in Library list and then select Move Finder Items under the Actions list. Drag this action to the workflow area in the right.

5 - In Move Finder Items select the folder where you want the files to be moved to. You also have the option of showing the action when the workflow runs.

6 - Go to the File menu and select Save. Give it an appropriate name as Add to iTunes and you’re done.

The following screenshot shows the Service workflow configured:

Add to iTunes.workflow configured according to the six steps described above 
Figure 2 - Add to iTunes.workflow configured according to the six steps described above

Now, let’s use this service workflow. To do this, go to Finder and open any folder that contains audio files such as MP3. Right click the MP3 file and voila. Now there’s an extra context menu option called Add to iTunes. What a marvelous thing.

Add to iTunes context menu option in Finder when right clicking MP3 file(s)
Figure 3 - Add to iTunes context menu option in Finder when right clicking MP3 file(s)

What happens when Add to iTunes is clicked? The workflow we created will be executed and the selected file(s) will be moved to the folder specified in the workflow, in this case /Users/leniel/Music/iTunes/iTunes Media/Automatically Add to iTunes.

As you see this is totally life saver.

I can play the MP3 in iTunes and if I decide that I want to keep it in my media library I just have to select Add to iTunes.

The possibilities with Automator are endless given the amount of options in its Library and Actions lists and workflow template types.

I hope you could get an idea of what Automator can do.

Updated on 12/16/2010

If you make a slight change in step 3 above you can have this workflow add not only audio files but even entire folders to iTunes and better yet, iTunes will ask if you want to replace existing files so that you don’t end up with duplicate files in your iTunes library. This is great.

So what do you have to do? Instead of audio files, select files or folders. Just this. Save the workflow. Now right click over any folder in Finder and you’ll see that you get a new menu option under Services called Add to iTunes.

Add to iTunes context menu option in Finder when right clicking a folderFigure 4 - Add to iTunes context menu option in Finder when right clicking a folder

Notes
Service workflows are saved in /Users/YourUserName/Library/Services.

In prior versions of Mac OS, there was an option to save the workflow as a plug-in. This was necessary so that you could have a context menu option (right-click) in Finder to run the workflow. I tried to go this way but this option isn’t available in Mac OS X 10.6.4. I realized that I had to create a Service workflow when I read this question at Apple’s Support site: No Automator plug-in in 10.6?

Download
You can download this workflow at:
https://sites.google.com/site/leniel/blog/AddtoiTunes.workflow.zip

References
Automator - Your Personal Automation Assistant

Automator - Learn by example

Automator at Apple’s Mac OS – All Applications and Utilities

Automator article at Wikipedia