views:

67

answers:

1

Hi,

I have names of all files in output.txt but I do not have folderpath in output.txt file. For Example, if filename = test.txt and its folderpath=/usr/local/bin than in output.txt file, I only have filename as test.txt

output.txt files has many entries for filename, what I am trying to do is:

  1. Move all files present output.txt to some another folder say '/usr/local/test/data/', so my question is how can I do that using Perl Script ?

  2. Delete all the files which are present in output.txt file from the actual location, using their folderpath, so for example, script should first move test.txt to some other folder location, say, /usr/local/test/data/test.txt and delete test.txt from its actual location using folderpath information, i.e., script should delete /usr/local/bin/test.txt

Any suggestions would be highly appreciated.

Updates: Below scripts reads the output.txt file and gets size of each file and total size of all the files present in the output.txt

my $folderpath = 'the_path';
open my $IN, '<', 'path/to/infile';
my $total;
while (<$IN>) {
    chomp;
    my $size = -s "$folderpath/$_";
    print "$_ => $size\n";
    $total += $size;
}
print "Total => $total\n";

Courtesy: RickF Answer

Output.txt

2304_2328_Test Data November 19 2003 - Test Tele.xls
2344_2341_Data Dummy Test 12-17.doc
2343_2342_Dummy Test Test 12-17.doc

My output.txt file also have some files whose name contain special characters, for example, 63551_106640_63551 IBM&#253;Software Delivery&Fulfillment(Div-61) Data IPS 08-20-2010 v3.xlsm

If you see above the file has encoded value of &#253 but my filename has actual special character symbol for that and so will copy or move take my this file also into account and move the files.

A: 

One suggestion would be to use perl's rename function. You can use this to move files like so:

use 5.012;
use warnings;

my $folderpath = '/some/where/';   # is it really "/usr/local/bin"? be careful now!!
my $tofolder   = '/usr/local/test/data';  # must have permissions to use this and $folderpath

my @files = ('test.txt', 'someotherfile.txt');

for my $file (@files) {
    rename "$folderpath/$file", "$tofolder/$file"
        or warn "Couldn't move $file";
}

/I3az/

draegtun
`File::Copy` has a `move` function that may be safer.
Platinum Azure
@Platinum Azure: Can you give an example.
Rachel
Re: @Platinum: http://perldoc.perl.org/File/Copy.html
RickF
hwo to handle files with special characters in their name ? Will copy take into account special characters file ?
Rachel
This will only work when moving files along the same disk, won't it?
Powertieke
Yes `File::Copy` is the more universal way to go. My `suggestion` was *teach someone to fish* answer hence including link to perldoc (http://perldoc.perl.org) and the query over perms etc. The (original) question was described as Unix only solution.. so I was hoping OP would see the `rename` footnote: *For a platform independent move function look at the `File::Copy` module.*
draegtun