!Needed: File Modifier!
I need to make a program which will modify all files of a particular extension in a directory and all of its subdirectories.
For example, the file before modification might look like this (when viewed in notepad)
onelineofrandomtext
(
multiplelinesofindentedtext
)
//commentshere
after modification, it might look something like this:
onelineofrandomtext
(
addedlinesoftext
multiplelinesofindentedtext
)
//commentshere
//additionalcommentshere
Basically, the program will need to add extra lines of text in front of the third line, and it will need to append text to the end of the file. The most difficult part of this will be making it do this for ALL files within a directory structure with a similar extension.
Can this be done?
How can it be done?
Is anyone willing to help?
Please ask for more information if needed.
Kind regards,
ubnco
[975 byte] By [
ubncoa] at [2007-11-26 16:52:06]

> Can this be done?
Yes
> How can it be done?
Java IOI would suggest you do some reading.
> Is anyone willing to help?
If you ask specific questions then you will get some help here. But make sure your show that you are willing to put some effort into to it.
Use this method to access files of certain extension in a single directory
// File f = new File("directory_path");
File[] entries = f.listFiles(new FilenameFilter()
{
public boolean accept(File f, String s)
{
return s.endsWith(".ext");
}
});
where ".ext" is the desired extension. Then you would have to recurse into subdirectories and read the contents of each directory as above. Use e.g. RandomAccessFile to edit the files.
Jukka
Thanks for the help; I need to modify 15,000+ files in multiple ways! Dont want to do it manually.
I suppose that my first task would be generating a list of the filesnames needed. The problem here is that I need to go into multiple levels of subdomains. That file extension code will help.
> I suppose that my first task would be generating a> list of the filesnames needed. The problem here is> that I need to go into multiple levels of subdomains.> That file extension code will help.Well, just iterator through each level that you come
See the [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html]File[/url] class.
A generic crawler (recursion is your friend):
import java.io.File;
import java.io.IOException;
class test {
public static void processDir(File dir) {
System.out.println("Processing " + dir.getAbsolutePath());
String[] files = dir.list();
if (files==null) {
return;
}
for (int i = 0; i < files.length; i++ ) {
File file = new File(dir.getAbsolutePath() + System.getProperty("file.separator") + files[i]);
if (file.isDirectory()) {
processDir(file);
} else {
processFile(file);
}
}
}
public static void processFile(File file) {
if (file.getName().endsWith(".java") ) {
System.out.println("Filename: " + file.getAbsolutePath() );
}
}
public static void main(String [] args) {
String dirName = args[0];
File dir = new File(dirName);
if (dir.exists()) {
processDir(dir);
} else {
System.err.println(dirName + " doesn't exist.");
}
}
}
Wow, thank you kevjava. That was exactly what I was looking for. A slight modification of your code creates an increadible long list of all of the file names and directories in under a second. Now I just need to insert 3 lines of text after the line containing the { symbol. I also need to append a line to the end of the file.
here is an example file that needs to be changed (exact; between the % signs):
startoffile%
"LightmappedGeneric"
{
// Original shader: BaseTimesLightmap
"$basetexture" "Glass/glasswindow005a"
"$surfaceprop" "glass"
"%keywords" "c17industrial"
"$envmaptint" "[ .25 .25 .25 ]"
"$envmap" "env_cubemap"
"$envmapcontrast" 1
"$envmapsaturation" .25
}
%endoffile
I guess I will need to open file streams using the processFile method posted by kevjava?
I would use the Scanner class to scan through the files and input them, and then use a FileOutputStream to reprint the file, or the [url=http://java.sun.com/j2se/1.4.2/docs/guide/nio/]java.nio[/url] packages.
If it were me, I would copy the file to a backup name first, or use the createTempFile() method first to write to a temporary name. Then, read your file in, parse and modify, write to the temporary name, and you can rename the files (using the File.renameTo() method) to the right spots once everything has been determined to have succeeded.
Actually, it would be ideal to create a parallel directory structure with the modified files. Would this be more work than just using the method you suggested? If it is, its easy enough to copy the files BEFORE running the program.
That's doable. In processDir(), you would put in a line of code at the beginning to create a directory of the same name in your parallel structure. I think it's the File.mkdir() method that you would use.
I need to take the file directory (dir) and turn it into what I need for newdir.mkdirs(); to make the correct directory.
public static void processDir(File dir, File newdir) {
System.out.println("Processing " + dir.getAbsolutePath());
newdir.mkdirs();
String[] files = dir.list();
if (files==null) {
return;
}
for (int i = 0; i < files.length; i++ ) {
File file = new File(dir.getAbsolutePath() + System.getProperty("file.separator") + files);
if (file.isDirectory()) {
processDir(file);
} else {
processFile(file);
}
}
}
Right.
Something like:
public static void processDir(File dir, File newdir) {
System.out.println("Processing " + dir.getAbsolutePath());
newdir.mkdirs();
String[] files = dir.list();
if (files==null) {
return;
}
for (int i = 0; i < files.length; i++ ) {
File file = new File(dir.getAbsolutePath() + System.getProperty("file.separator") + files [ i ]);
// Build the new new directory path.
File newNewdir = new File(newdir.getAbsolutePath() + System.getProperty("file.separator") + files [ i ]);
if (file.isDirectory()) {
// Pass in the new directory name.
processDir(file, newNewdir);
} else {
processFile(file);
}
}
Hah! Got it. Now I have an entirely new directory structure. I will just work on the files now. Thanks a lot to kevjava yet again.
Quick question:How can I check to see if the last element of a vector contains a certain string?if (lines.lastElement())
ubncoa at 2007-7-21 16:52:55 >
