We help IT Professionals succeed at work.
Get Started

stripping elements of different length from a file name java

Richard Teasdale
Last Modified: 2016-01-27
Hi: As a company we get a lot of orders sent in csv format by email. I use  a third party  program called 'attachment save'  which runs in outlook and saves all attachments to a particular directory. Since it is not unknown for the files from different customers to have the same name, the program adds the date. Thus an attached file j031251.csv  is saved as J0312512016-01-27 12-11.csv .
I then use java to pick out files from a certain customer (in this case always has "wp201" in his filename:

for (int i = 0; i < files.length; i++) {
            fileName = files(i).getName();   // (i put in brackets not parenthesis)
            //rename file to take date & time off    
             String abc = new String(fileName);
             String xyz = new String(abc.substring(0,(abc.length()-20))+abc.substring((abc.length()-4),abc.length()))  ;            
            //end rename file to take date & time off    
                File currentFile = new File(dir + "\\" + abc);
                           success = currentFile.renameTo(new File(dew + "\\" + xyz));
                    System.out.println("File Copied: "+ currentFile);
                else System.out.println("NOT Copied: " + currentFile);          
Which strips out the date and puts the file in a specific folder. It does this by counting back from the end of the file; easy as the date is always 16 characters.
However, now another customer wants special treatment; but his file is the same as a lot of other peoples files. 'Attachment save' will allow me to insert the senders address like so: "J031251XXXcustomer.co.ukYYY2016-01-27 12-11.csv", where I insert XXX and YYY as delimiters, but can choose anything. Is there a way of stripping out everything after the XXX but BEFORE the ".csv"? This is because , of course, customer addresses will be of different length and file types will be different.
Watch Question
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE