Solved

removing  last characters of a file name java

Posted on 2015-01-21
3
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 36

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

632 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