Solved

removing  last characters of a file name java

Posted on 2015-01-21
3
95 Views
Last Modified: 2015-01-21
Hi: I have some  third party software that takes a file from an email and puts in a folder, adding date and time.
Thus file 12345.dxe becomes "123452015-01-21 11-58.dxe". Now it does this for many types files but for really sad reasons I need to rename the  dxe file back to 12345.dxe before I can process it further. I need to go through the whole process I am afraid ; I cannot change the process for just these types.
My 'second' process - where I work on the file - is java so does anybody know how to write something like: "for each dxe file in the folder DELETE THE LAST 16 CHARACTERS OF THE NAME then do x"? (where the bit I need is in capitals). The first part of the file name may not be a fixed length but the date and time are always 16 characters.
Thanks!
0
Comment
Question by:ClaytonGlass
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 40561875
I would use a regex for this, as non-matching file names would be unaffected
        String origName = ...;
        String newName = origName.replaceAll("(.*?)\\d{4}-\\d{2}-\\d{2} \\d{2}-\\d{2}(.*)",
                "$1$2");
        File f = new File(origName);

        if (f.renameTo(new File(newName))) {
            System.out.printf("Renamed %s to %s%n", origName, newName);
        } else {
            System.err.printf("Couldn't rename %s to %s%n", origName, newName);
        }

Open in new window

0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 250 total points
ID: 40561889
Just hints and pointers for you, not 100% working code (you should do that yourself).

Since you already know the exact things you want to delete, this code should be sufficient

String abc = new String("123452015-01-21 11-58.dxe");
System.out.println(abc.substring(0,(abc.length()-20))+abc.substring((abc.length()-4),abc.length()));

Open in new window


looping can be done with default code, and adding this if statement (use file name of course)

abc.substring((abc.length()-4),abc.length()) == ".dxe"

Open in new window


If you keep the code simple, it will assume there are NO .dxe files that are shorter than what you stated (i.e. you are 100% sure there will never be a abc.dxe file in that folder). If you aren't sure, you need to add more code to make sure you escape this when it happens.
0
 

Author Closing Comment

by:ClaytonGlass
ID: 40561904
Many thanks to you both for some great suggestions. I can see merits in either and will try both but points awarded equally for great responses. Thanks again
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat not starting 6 68
How do I remove an object from a 3 40
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
web project error add remove 1 27
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question