Solved

removing  last characters of a file name java

Posted on 2015-01-21
3
91 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now