Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

removing  last characters of a file name java

Posted on 2015-01-21
3
Medium Priority
?
120 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 1000 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 37

Assisted Solution

by:Kimputer
Kimputer earned 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

773 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