Writing to a file in a different directory?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starlight849
    New Member
    • Jun 2009
    • 82

    Writing to a file in a different directory?

    I have a simple script that opens a directory, reads through all the file names and then prints the output to a text file in a different directory. It looks something like this.
    My Question is how to write to the file using $OutputDir for my path instead of including the entire path in the manner that I'm using now.
    Any suggestions on how to do this or how to improve the script are greatly appreciated.
    Thanks

    Code:
    #!/usr/bin/perl
     use strict;
     use warnings;
    
    my $InputDir = '/home/files';
    my $OutputDir = '/home/output';
    opendir (DIR, $InputDir) or die "Couldn't open: $!;
           my $listing = `ls -l $InputDir`;
    
    open (MYFILE, '>/home/output/Output.txt') or die "Couldn't open: $!";
     print MYFILE "$listing\n";
     close (MYFILE);
    closedir(DIR);
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Change:
    Code:
    open (MYFILE, '>/home/output/Output.txt')
    To:
    Code:
    open (MYFILE, '>', "$OutputDir/Output.txt")

    Comment

    Working...