Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Copy file with name and timestamp  RSS feed

 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read and then copy files into a subfolder, and I need to rename the file names by adding the timestamp to the file name, for example like: "filename1_25.06.14_15:00:00.txt". How can I do that? Here is what I have:



is it right what i tryed, because i get nothing with timestamp in file after running this. Any suggestions please.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your recursive call to copyDirectory which actually copies the files is done on line 109, but you don't actually modify the file name until after you have called this method, so you are passing the original file name to the method that does the copying.
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your answer, that means it should be written in new method? after the call copyDirectory method?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Roberts wrote:thank you for your answer, that means it should be written in new method? after the call copyDirectory method?

No. Your call to copyDirectory on line 109 passes in

as the name of the new file, but children[i] contains the original name. you should be using the new name (the one you create on lines 111-120) instead of children[i].
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i edited this children[i] by new File(targetLocation, children[i]) with result, but still have nothing get
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Roberts wrote:i edited this children[i] by new File(targetLocation, children[i]) with result, but still have nothing get

Have you tried printing out the value of result (and your other variables) to see if it was what you expected ?
You are also ignoring any exception that is thrown by having an empty exception block. You should have at least a call to e.printStackTrace().
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried it this way



but get Test4\subfolder\filename_1_06-08-2014_11:04:28.csv (The filename, directory name, or volume label syntax is incorrect)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Roberts wrote:but get Test4\subfolder\CUBF-SW07_TGE23_CUBF-SW00_06-08-2014_11:04:28.csv (The filename, directory name, or volume label syntax is incorrect)

The error messafe suggests you are using Windows and in Windows that is an invalid file name. I'll leave it to you to work out why.
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, i got it. I do it for all files that in the directory with

where with for loop it must go throw all files, but it gives me only one file, and not other, in stacktrace it points on this line . Could you please tell me, if i use wrong children.length in for loop?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Roberts wrote:in stacktrace it points on this line

What's the actual exception that's being thrown ? Copy and paste the whole stack trace into here - there's usually a lot of useful information in there.
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NullPointerException
at loading.Table_data.copyDirectory(Table_data.java:161) and this 161 line is

for (int i = 0; i < children.length; i++) {

with first iteration it works, but when it comes to second length i get children.length is null im debug modus
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that line is returning an NPE then 'children' is a null reference.

You need to check the code which sets 'children'. If it is valid to set it to null then you need to specifically handle that case if not you have an error somewhere.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on your original code children should never be null, so you need to post the latest version of your copyDirectory method if you can't find the problem yourself.
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much Joanne and Tony for your support, it works now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!