New to perl and asking for help.
I need to execute the following:
1. Extract text between double quotes from text file located in subdirectory of INI directory
2. Search in directory TestData for the file with name that matches the text extracted in 1.
3. Copy the file from 2 to directory where text file from 1 is located
4. Repeat the same for all text files in subdirectories of INI directory.

Thanks a lot for your help.

That sounds like a lot of work. This may get you started on Step number 1. I'm assuming the text files contain only file names (one per line) between double quotes. If they contain other text as well you may have to fix it to skip the unwanted text. My platform is Windows.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010; #Lets you say
use File::Find;
my $inidir = "C:\\Users\\David\\Programming\\INI"; #INI directory on my computer is in C:\Users\David\Programming
# my $testdir = "C:\\Users\\David\\Programming\\TestData";
my @directories_to_search = ($inidir);
find(\&wanted, @directories_to_search);

sub wanted {
    my $f = $File::Find::name;
    if ((-e $f) && (-T $f)) { #File exists and is a text file
        say "Reading $f";
        process_file($f);
    }
}

sub process_file {
    my $f = shift @_;
    open FH, '<', $f || die "cannot open file for reading: $!";
    while (<FH>) {
        my ($filename) = m/"(.+)"/;
        say "We want a copy of $filename";
    }
    print "\n\n";
}

That sounds like a lot of work. This may get you started on Step number 1. I'm assuming the text files contain only file names (one per line) between double quotes. If they contain other text as well you may have to fix it to skip the unwanted text. My platform is Windows.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010; #Lets you say
use File::Find;
my $inidir = "C:\\Users\\David\\Programming\\INI"; #INI directory on my computer is in C:\Users\David\Programming
# my $testdir = "C:\\Users\\David\\Programming\\TestData";
my @directories_to_search = ($inidir);
find(\&wanted, @directories_to_search);

sub wanted {
    my $f = $File::Find::name;
    if ((-e $f) && (-T $f)) { #File exists and is a text file
        say "Reading $f";
        process_file($f);
    }
}

sub process_file {
    my $f = shift @_;
    open FH, '<', $f || die "cannot open file for reading: $!";
    while (<FH>) {
        my ($filename) = m/"(.+)"/;
        say "We want a copy of $filename";
    }
    print "\n\n";
}

Thanks a lot d5e5, I appreciate your help. This will get me started in right direction.

This article has been dead for over six months. Start a new discussion instead.