Solved

removing  last characters of a file name java

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 “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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 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