Renaming a bunch of .javas

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
tipo test
Posts: 66
Joined: Sun May 23, 2010 12:15 pm UTC

Renaming a bunch of .javas

Postby tipo test » Tue Nov 16, 2010 9:07 am UTC

So basically I need to rename 80 sourcecode files following a fixed pattern, and change the public class names of each first line so they still work. How would you do that?

I haven't experience on file handling apps so I can't code a solution myself! I work on Ubuntu btw.

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Renaming a bunch of .javas

Postby Xanthir » Tue Nov 16, 2010 9:13 am UTC

tipo test wrote:I haven't experience on file handling apps so I can't code a solution myself!

Congratulations; you've found an excellent reason to get experienced in this matter. This is a good starter project for file handling.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
tipo test
Posts: 66
Joined: Sun May 23, 2010 12:15 pm UTC

Re: Renaming a bunch of .javas

Postby tipo test » Tue Nov 16, 2010 9:27 am UTC

Course, but I'm bit on a rush. If there's a quick/easy Python solution or something please tell me guys... otherwise feel free to forget my request.

archeleus
Posts: 240
Joined: Wed Sep 29, 2010 1:49 pm UTC
Location: Valenvaryon
Contact:

Re: Renaming a bunch of .javas

Postby archeleus » Tue Nov 16, 2010 9:31 am UTC

Well if it was me I'll write a small program in C++ or an even smaller python script.

Anyway here's an helpful function and here is how you can do it

Code: Select all

DIR *dp;
struct dirent *dirp;
dp = opendir(path.c_str()); /* Path has to be assigned, you can get it using getcwd() */
while((dirp = readdir(dp)) != NULL){
      /* Get the files and rename 'em */
      /* Just so you know, you can get names using dirp->d_name */
    }


As for python, dig around in the os module:

Code: Select all

os.rename(src, dst)
I write a blog rant here.

User avatar
tipo test
Posts: 66
Joined: Sun May 23, 2010 12:15 pm UTC

Re: Renaming a bunch of .javas

Postby tipo test » Tue Nov 16, 2010 9:59 am UTC

My main issue is about modifying the .javas content itself... I can easily change the file names using several apps, but what about changing automatically the class name here?

Code: Select all

public class r1e1{
public static void main (String[] args){
}}

The classnames for each file are r1e1, r1e1, [...], r1e80.
Last edited by tipo test on Tue Nov 16, 2010 10:43 am UTC, edited 1 time in total.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Renaming a bunch of .javas

Postby Sc4Freak » Tue Nov 16, 2010 10:06 am UTC

This is something that you really should use an IDE for. Even if you change the names of the source files and the names of the classes, those classes are going to show up elsewhere in your code too. Depending on your codebase, you can't just do a blind text-based search & replace, either, because that's error prone and you might end up replacing something you didn't want replaced. If your codebase is large or you have a class with a fairly common word as a name, there's a good chance you'll end up breaking something or silently introducing a bug somewhere if you use a text-based search-and-replace.

Any half-decent IDE will allow you do a context-sensitive search and replace of a class, which will properly parse your code and fix it as necessary. If you're using Eclipse the default shortcut is Alt + Shift + R, if I remember correctly. Other IDEs will have different refactoring tools/capabilities. Although, granted, doing this 80 times might be a bit of a pain, I suspect using your IDE would still be faster and more safe than writing a script to do a text-based search and replace.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Renaming a bunch of .javas

Postby Emu* » Tue Nov 16, 2010 3:57 pm UTC

if you have numbers in your classnames, you're doing it wrong.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
tipo test
Posts: 66
Joined: Sun May 23, 2010 12:15 pm UTC

Re: Renaming a bunch of .javas

Postby tipo test » Wed Nov 17, 2010 8:36 am UTC

Emu* wrote:if you have numbers in your classnames, you're doing it wrong.

really? I think its quite reasonable for homework...

Sc4Freak wrote:This is something that you really should use an IDE for.

Okay I'll keep it in mind.

x

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Renaming a bunch of .javas

Postby Emu* » Wed Nov 17, 2010 9:04 am UTC

One day you'll need to do something and remember "I've done this before!". How will you find the right class to reuse/copy from without proper names?
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
spdqbr
Posts: 171
Joined: Sat Mar 08, 2008 1:41 am UTC
Location: A shaker of salt

Re: Renaming a bunch of .javas

Postby spdqbr » Fri Nov 19, 2010 5:04 am UTC

Well, barring using something like Eclipse's refactor->rename functionality (much safer than what I'm about to do) you could always appeal to the powers of bash.

All this does is change Test1.java to Test01.java and so on, but the theory is sound. Backup your code base before you try something like this...

Code: Select all

spdqbr@MyMachine:~/temp$ ls
Test1.java  Test2.java

spdqbr@MyMachine:~/temp$ cat *
public class Test1{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}
public class Test2{
    public static void main(String[] args){
        System.out.println("Hello World 2");
    }
}

spdqbr@MyMachine:~/temp$ i=1; for file in *; do sed -e "s/public class Test$i/public class Test0$i/g" $file > ${file/$i/0$i}; i=$(( i+1 )); done

spdqbr@MyMachine:~/temp$ ls
Test01.java  Test02.java  Test1.java  Test2.java

spdqbr@MyMachine:~/temp$ cat *
public class Test01{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}
public class Test02{
    public static void main(String[] args){
        System.out.println("Hello World 2");
    }
}
public class Test1{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}
public class Test2{
    public static void main(String[] args){
        System.out.println("Hello World 2");
    }
}

spdqbr@MyMachine:~/temp$
In questions of science, the authority of a thousand is not worth the humble reasoning of a single individual.

Galileo

trakof
Posts: 12
Joined: Sat Jul 17, 2010 8:33 pm UTC

Re: Renaming a bunch of .javas

Postby trakof » Fri Nov 19, 2010 6:57 am UTC

Like Sc4Freak said, this is a horrible idea, but if you really need to here's a python script that could work.

Code: Select all

import os, re

directory = "c:\\tmp\\files\\"   #path to files
d = os.listdir(directory)
i=0
for f in d:
    newfilename = str(i) #some pattern for new names, I'm just using the loop count
    filename = f[:-5] #old file name - .java
    i+=1
   
    print f
   
    oldfile = open(directory+f,"r")
    newfile = open(directory+newfilename+".java","w")
   
    text = oldfile.read()
   
    reg = filename #regex of old file name
    output = re.sub(reg,newfilename,text) #replace all occurances of file name
   
    newfile.write(output) #write the new file
   
    newfile.close()
    oldfile.close()
   
    os.remove(directory+f) #delete old file


It could potentially break your code depending on the file names and it will delete any empty files. Make sure you back up your files before doing anything like this.
It also wont work if any of the classes reference another class.

User avatar
tetsujin
Posts: 426
Joined: Thu Nov 15, 2007 8:34 pm UTC
Location: Massachusetts
Contact:

Re: Renaming a bunch of .javas

Postby tetsujin » Fri Nov 19, 2010 8:50 pm UTC

Bash?

Code: Select all

for f in *.java; do base=$(basename $f .java); newbase=$(whatever $base); perl -pe "s/\\b$base\\b/$newbase/g;" -i $f; mv $f ${newbase}.java; done


Did I miss something? I guess a case-insensitive filesystem could cause you some trouble there - in that case I guess the thing to do would be to pull $base out of the file itself by grepping for the class definition. And there's the possibility that a class name could be referred to in another context - for instantiation, for instance...
---GEC
I want to create a truly new command-line shell for Unix.
Anybody want to place bets on whether I ever get any code written?


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests