Home: Perl Programming Help: Beginner:
How to export a whole txt file into a specified folder



snowpl10
New User

Jun 9, 2017, 12:11 PM


Views: 2578
How to export a whole txt file into a specified folder

I have a question about reading and exporting a whole txt file to a specific folder. For example, I use the code below, but in the new exported txt files, they all have just one line: GLOB(0x7fb0c...).
How to copy the content from $simdata to $outfile and export? The reason that I wanted to do this is that I can export the different files to different places or renaming them, as they are over-written in the old directory. Thanks for your help.

open my $simdata, '<', '/theOldPath/file.txt' or die $!;
open my $outfile, ">", "/theNewPath/file.txt" or die $!;
print $outfile "$simdata\n";


Chris Charley
User

Jun 9, 2017, 1:04 PM


Views: 2573
Re: [snowpl10] How to export a whole txt file into a specified folder

Probably your best bet is File::Copy.


snowpl10
New User

Jun 9, 2017, 1:05 PM


Views: 2571
Re: [Chris Charley] How to export a whole txt file into a specified folder

Thanks, I will look at the function. Will this influence the original file?


Chris Charley
User

Jun 9, 2017, 1:08 PM


Views: 2569
Re: [snowpl10] How to export a whole txt file into a specified folder

move 'moves' the file to the destination. copy makes a copy of the file in the destination.


snowpl10
New User

Jun 9, 2017, 1:22 PM


Views: 2563
Re: [Chris Charley] How to export a whole txt file into a specified folder

Thanks, this function is pretty convenient. I put it here, so to verify it is correct?

use File::Copy;
copy("sourcefile","destinationfile") or die "Copy failed: $!";


Laurent_R
Veteran / Moderator

Jun 10, 2017, 12:35 AM


Views: 2545
Re: [snowpl10] How to export a whole txt file into a specified folder

Yes, that's correct, provided you replace the "sourcefile" and "destinationfile" strings with what you actually need, i.e. proper file names prefixed by the relevant (relative or absolute) paths.