• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Script to make file copies with different names

I had a file named "testnow.txt" and i want to make this file 95 copies.. in the same folder
i want to duplicate the "testnow.txt" and rename it like
testnow_1.txt
testnow_2.txt
testnow_3.txt
.
.
.
.
testnow_95.txt

Here is the java code that made this work, but I am not sure how to write the same logic using perl.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
public class filerenametemp {
    public static void main(String[] args) {
          Path src = Paths.get("C:\\temp\\testnow.txt");
          for( int i=1;i<=6; ++i ){
            Path dest =  Paths.get("C:\\temp\\testnow_" + i + ".txt");
            try {
                Files.copy(src,dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
          }
   }
}


Can some one provide me the code with same logic in perl.

Thanks,
0
shragi
Asked:
shragi
  • 2
1 Solution
 
ozoCommented:
use File::Copy;
copy "testnow.txt", "testnow_$_.txt" or die "Copy failed: $!" for 1..95;
0
 
shragiAuthor Commented:
Hi ozo - this solution is good and working,
what if my number is not always 95 and it changes based on parameter.
How do i change script for that.
0
 
shragiAuthor Commented:
never mind i got it - thanks
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now