Bulk rename files

January 31, 2008

So, this was the simple problem I was facing:

I have a set of mp3 files, all starting containing a particular pattern, and wanted to rename it all for whatever devious reason I had. I wanted these files to not contain the pattern anymore. For example, 000 – blah.txt and 122 – bleh.txt should be renamed to “blah.txt” and “bleh.txt”.

The options to achieve this were aplenty:

1) Wonder how convenient it would’ve been if Windows had ’sed’. However, I didn’t have, nor did I have the inclination to install Cygwin on my Windows laptop, and run sed through that. So, onto different pastures.

2) Download some free software from the web, there are many that do bulk rename.

3) Write a script of your own.

The existence of this blog entry makes the choice I made quite obvious. The reasons for my choice range from an acute affinity towards Ruby, to a decent amount of free time … :P

Here’s a script that I wrote, if you’re good at regular expressions, this might be a decent help. A list of the same can be found here.

What does one achieve?

Basically, you can rename a set of files under a particular directory, ending with a certain extension and containing a particular pattern. The resulting files will not contain the pattern anymore, but the extension would be preserved.

What to do?

1) Install Ruby
Extremely simple to install. Can be found here

2) Go to the directory where these files exist, and copy paste the above script as a RenameFiles.rb ruby file.

3) Run the command through the command line, for example: ruby RenameFiles.rb .txt [\d]+[\s]*[-]+[\s]*

Disclaimers and Warnings:

This script is obviously quite rudimentary, and has not been extensively tested either. Use at your own risk, I’m not responsible for script writing laziness on your part ! … :D

##
#
# RenameFiles.rb
#
# Function to rename files under a directory
#
# @param ext -> rename files ending with this extension
# @param pattern -> rename files containing this pattern (to be removed)
#
# @result -> Renamed files: Extension preserved, Pattern removed
#
# Note: This file has to be under the directory containing the files to be renamed
#
# A sample regular expression might be: [\d]+[\s]*[-]+[\s]* which looks for:
# [any number atleast once]->[any no. of spaces]->[hyphen]->[any no. of spaces]
#
# @author: Manoj
#
##

def RenameFiles( ext, pattern )

  #
  # Create regular expressions for input strings
  #
  pattern = Regexp.new( pattern )
  ext = Regexp.new( ext + "$" )

  #
  # Reporting variables
  #
  @extensionFlag = false
  @patternFlag = false
  @count = 0

  #
  # Go through all files in the "current" directory
  #
  Dir.foreach(Dir.pwd) { 

    |x|
    #
    # Check if the current file carries the reqd. extension
    #
    if ( x =~ ext ) then 

      @extensionFlag = true
      rptString = x.clone
      rptString = rptString.gsub!(pattern, '')

      #
      # Was any substitution/rename made?
      #
      if ( rptString != nil )

        @count = @count + 1
        File.rename(x, rptString  )
        @patternFlag = true

      end

    end
  }

  #
  # Report statistics ...
  #
  if @extensionFlag == false
    puts "\nNo files with required extension"
  elsif @patternFlag == false
    puts "\nNo files with required pattern"
  else
    puts "\nRenamed " + @count.to_s + " files"
  end

end

#
# Generate usage error, if insufficient params
#
if ( ARGV[0] == nil || ARGV[1] == nil )
  puts "\nUsage: RenameFiles.rb   \n"
else
  RenameFiles( ARGV[0].to_s, ARGV[1].to_s )  

end

anonymity …

January 10, 2008

Contrary to the obvious, the title means something quite different in the following context … :P

Just so that I might not forget how I did this, I log here, a way to achieve anonymous classes in C# ( a feature quite inexplicably missing from the language!

Now, C# obviously supports anonymous methods albeit via a twisted mechanism using delegates ( Java surprisingly doesn’t support anonymous methods … anyone wants to bring out a superset language, this is the time!! ).

In the following trivial scenario, one has an interface, consisting of 2 methods, which can be dynamically (anonymously) instantiated by any other class. Let’s consider Java here, so that the shortcoming in C# is multiplied, and my solution seems worthwhile! :)

For instance:

public interface MyInterface
{
       public void Method1( Object value );
       public Object Method2();
}

Say, I have another class: consists a method accepting an object of our interface defined above.

For instance:

public class HungryClass
{
       MyInterface _myObject;   public void HungryMethod( MyInterface myObject )
       {
              // for lack of anything else interesting
              _myObject = myObject;
       }
}

Now, I have my lazy class, consisting of a lazy method in charge of supplying the object (of our interface) above.

    For instance:
public class LazyClass
{
       public void LazyMethod()
       {
              // Call hungry method as it's hungry
              HungryMethod(
                     new MyInterface()
                     {
                            public void Method1( Object value )
                            {
                                   // blah ...
                            }
                            public Object Method2()
                            {
                                   // bleh ...
                            }
                   }
              );
       }
}

Well, the point of the above loquacity was obviously to show that anonymous classes (as in the LazyMethod()) is an option, and in most big programs, can be a huge pain reliever (especially, in massive auto-generated files).

C# DOESN’T SUPPORT ANONYMOUS CLASSES.

Since C# doesn’t support anonymous classes, you need to create a dummy concrete class every time we want to pass an object of the interface.

For instance:

public DummyMyInterface : MyInterface
{
       public void Method1( Object value )
       {
              // blah ...
       }
       public Object Method2()
       {
              // bleh ...
       }
}

and then pass an object of this class. Can you imagine creating a huge set of such concrete classes when the need occurs, especially in the auto-generated case. Well, if you can’t … trust me, it’s a major pain in the .. ahem .. fingers.

SOLUTION: (voi la!)

In order to support the same, I use delegates in C# (similar to function pointers in C, C++). The point lies in understanding that delegates themselves are types, and we can define variables of the same. And delegates are the only way in which you can achieve any kind of anonymity in C# (anonymous methods). Put 2 and 2 together, in addition to getting 4, you also get the following:

In C#, you define MyInterface as follows:

public class MyInterface
{
       // Delegates for as many methods (with similar signatures ofcourse)
       public delegate void Method1Delegate( Object value );
       public delegate Object Method2Delegate();   // Define variables for each of the above (delegate) types
       Method1Delegate _m1;
       Method2Delegate _m2;// Default Constructor (unfortunate adherence)
       public MyInterface()
       {
              _m1 = null;
              _m2 = null;
       }// Parametrized Constructor (the crux)
       public MyInterface( Method1Delegate m1, Method2Delegate m2 )
       {
              _m1 = m1;
              _m2 = m2;
       }// In order to maintain consistency (backward compatibility) with the
       // existing system, define the following virtual functions.
       public virtual void Method1( Object value )
       {
              // Just in case the delegate wasn't defined
              if ( _m1 == null )
                     throw new Exception( "method undefined" );_m1( value ); // delegatize baby !
       }
       public virtual Object Method2()
       {
              // Just in case the delegate wasn't defined
              if ( _m2 == null )
                     throw new Exception( "method undefined" );return _m2(); // delegatize baby !
       }
}

Now, our lazy class looks like this:
————————————–

public class LazyClass
{
       public void LazyMethod()
       {
              // Call hungry method as it's hungry
              HungryMethod(
                     new MyInterface()
                     {
                            delegate( Object value )
                            {
                                   // blah ...
                            },
                            delegate()
                            {
                                   // bleh ...
                            }
                     }
              );
       }
}

Because of the above instantiation, our delegates will be correctly instantiated. And whenever invokes the methods, Method1 & Method2, oblivious of the underlying implementation, the correct behavior will be exhibited (since those methods are in turn defined to invoke the delegates). Also, it’s obvious from the above that any class can also choose to override the default behaviour of our virtual methods. Hence, reduced pain, good looking code (aha, subjective eh?), and less code change propogation (if any at all) throughout the code base!

If the guys reading have a cleaner solution, let me know, and you’ll have the opportunity to change my code base yourself ! … :)


Fan-bloody-tastic … !

April 7, 2007


All my woes of not being able to transfer ideas on a whiteboard to a computer have been removed … feels like one of the happiest days of my life, I could almost cry .. :D

Picture courtesy:
Vineet’s whiteboard + My awesome camera + Picasa (and I expect better quality pictures with controlled lighting … ). The small version really doesn’t do justice, click for a better view!


Ji(o)kes RVM

November 26, 2006


Now, I don’t have anything against Jikes RVM (for the uninitiated, the Jikes Research Virtual Machine is a Virtual Machine for Java, written in Java that enables researchers to test optimizations of their own … enough of that !!). However, the documentation on the virtual machine is really less as compared to the amount of information/code in the virtual machine !!!

So, as usual, I turn to the reliable Google, for some respite … and this is what it returns !! … (much to my chagrin)

So, hey … I think, now that anyways Jikes is turning out to be a joke for me, why not click on the Did you mean ?? … link, thinking that it might churn up something interesting. Well !! … it did spit out something alrite ! … looks like this …

Basically, even Google laughs at my plight !! … so I might as well too … :D


Predication ! …

November 16, 2006

for my reference, maybe completely mistaken !! …

predicated instructions:

Usually, you have a branch instruction followed by a certain number of instructions dependent on whether that branch is taken or not. Basically, what you do is, you check the history of branch predictions, deduce on basis of your history whether the current branch will be taken or not,. and move ahead accordingly. One of the interesting points with this is that, you’re not basing the decision on whether to take or not-take this branch on the actual value on which the branch is based on, but on the history of predictions before this. How to convert this in a way that, the branch actually depends on the value? … and more importantly, why would we want to do this?

Imagine a branch instruction:
if (b > 0)
a = b;
else
a = 0;

Now, this would be converted into 3 instructions; 1 branch and 2 writes. Imagine a new type of instruction, say, CMOV (a , b). What this would do is, depending on whether b greater 0 or not, it will allocate a to b or a to 0. We can see that, this would mean that the control dependence is transformed into a data dependence, and whether the instruction is executed or not, is decided not in the inital phase of the pipeline, but in the latter stage of it (where the register write occurs). Such an instruction can completely remove branch instructions in simple code sequences.

However, something like this would be too expensive in a bigger scenario, because you would need such instructions for every such instruction within a particular control flow (execution path). Hence, in some architectures, an entire set of instructions can depend on a certain predicate. When the predicate is false, the instruction becomes a no-op.

further details later …