Perl script issue: print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script issue: print
# 1  
Old 06-09-2009
Perl script issue: print

Can someone tell me what I'm doing wrong with my perl script? I am new to Perl. This isn't even the final script, I'm just testing to see if it will print the directories with the files in it. For some reason my output is only printing the 1st, 6th, and last entries of the @sub_dir array. Each of the sub_dir folders has 4 test files in it (Test1-4.txt).

My intent is to ultimately copy multiple files from source to destination--since perl won't let me do it as easily as in shell. TIA Smilie

Code:
#!perl.exe

use strict;
use File::Copy;

my @sub_dir = ("sqr","src","user","nvision","winword","crw","excel","maint","projects","scripts","data");
my $source = "C:\\Test";
my $destination = "C:\\Test2";

foreach (@sub_dir) 
   {
   my $dir1 = glob($source . "\\" . $_ . "\\*");
   my $dir2 = glob($destination . "\\" . $_);
   
   print "$dir1\n";
   }


Here is my output:

C:\Scripts>perl refresh_test.pl
C:\Test\sqr\Test1.txt
C:\Test\sqr\Test2.txt
C:\Test\sqr\Test3.txt
C:\Test\sqr\Test4.txt

C:\Test\crw\Test1.txt
C:\Test\crw\Test2.txt
C:\Test\crw\Test3.txt
C:\Test\crw\Test4.txt

C:\Test\data\Test1.txt
# 2  
Old 06-09-2009
My first suggestion is your return the results of glob to an array instead of a scalar. And use forward slashes in the paths, which Windows supports:

Code:
#!perl.exe

use strict;
use warnings;
#use File::Copy;

my @sub_dir = ("sqr","src","user","nvision","winword","crw","excel","maint","projects","scripts","data");
my $source = 'C:/Test';

foreach (@sub_dir) 
   {
   my @dir1 = glob("$source/$_/*");
   print $_\n" for @dir1;
   }

# 3  
Old 06-09-2009
Great! That worked perfectly.. THX!

Here is my finalized perl multiple file copy script for reference (if anyone else has the same issues)

Code:
#!perl.exe

use strict;
use File::Copy;

my @sub_dir = ("sqr","src","user","nvision","winword","crw","excel","maint","projects","scripts","data");

print "\nEnter source directory";
chomp (my $source_dir = <STDIN>);

print "Enter destination directory";
chomp (my $dest_dir = <STDIN>);

foreach (@sub_dir) 
   {
   my @source = glob("$source_dir/$_/*");
   my $destination = "$dest_dir/$_";
   
      foreach my $file (@source)
      {
      print "copying files from $source_dir to $dest_dir... please wait.\n";
      copy ($file, $destination);
      }
   }

# 4  
Old 06-09-2009
You should add some error handling to that just in case.

Code:
copy ($file, $destination) or do {
   print qq{Unable to copy '$file' to '$destination': $!\n};
};

Or whatever is appropriate
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl script print the lines between two pattern

i have a file as below sample.pl parameter1 argument1 argument2 parameter2 I want out as below argument1 argument2 that is , i want to print all the lines between parameter1 & parameter 2. i tried with the following if($mystring =~ m/parameter1(.*?)parameter2/) (2 Replies)
Discussion started by: roopa
2 Replies

2. Shell Programming and Scripting

Issue regarding dos2unix perl script

Hi All, I have pearl script which will check and convert the file: INFO("dos2unix_cmds".$#{$dos2unix_cmds}); if ( $#{$dos2unix_cmds} == 0 ) { my $convert_cmd = $$dos2unix_cmds; my $rename_cmd = $$dos2unix_cmds; --conversion going here else INFO ("No need to... (8 Replies)
Discussion started by: saps19
8 Replies

3. Shell Programming and Scripting

Perl script issue

Hi All, I have a perl script which I am using in Windows environment. There is one more file called "functions.txt" which is having all the functions defined to used in my perl script. And the path for this function file is defined in my perl script. Howeever sometimes I am getting below error... (4 Replies)
Discussion started by: gr8_usk
4 Replies

4. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

5. Shell Programming and Scripting

how to print out data from mysql table in perl script

I'm having trouble with this code. if i do .\read.pl -u user it prints out 2010-12-20 12:00:00 host1 <cmd>a 2010-12-20 12:00:01 host1 <cmd> <execute> 2010-12-20 12:00:02 host1 <cmd>b 2010-12-20 12:00:03 host1 <cmd>c however, if i enter .\read.pl -h host1 it should... (3 Replies)
Discussion started by: kpddong
3 Replies

6. Shell Programming and Scripting

Perl Script - Print Content of next line

Good evening to you all perl experts I need your help I have a very simple script where I´m trying to print a line from status.dat file. The script will find the line containing "servicestatus", and I want to print the content of the next line. For example, the file contains this text:... (6 Replies)
Discussion started by: zarahel
6 Replies

7. Shell Programming and Scripting

Perl Print Problems in script

Hi Perl Gurus, perl -e 'print "http://www.site@domain.com"' The output of the above is : http://www.site.com" I want to print http://www.site@domain.com without using escape sequence before '@' like '\@'. Is there any way to do this in perl? Thanks, Som (1 Reply)
Discussion started by: som.nitk
1 Replies

8. Shell Programming and Scripting

Perl Script issue. What am I doing wrong?

#!/usr/local/bin/perl open (MYFILE, 'logs_report'); while (<MYFILE>) { $rec=$_; chomp ($rec); @arr=split(/ /,$rec); print $rec,"\n" if ($arr!~/OK/); open (MYF, '>data.txt'); print $rec,"\n" if ($arr!~/OK/); close (MYF); (14 Replies)
Discussion started by: SkySmart
14 Replies

9. Shell Programming and Scripting

Perl Script Issue - Please Help * Thanks!!!

Please help me with my script please. I am trying to do the following: 1. Read files for the current directory 2. Open and read from nbe files files only 3. Read only the lines with the results pattern 4. Split the line and print 3rd field Please indicate what line I need to modify. ... (8 Replies)
Discussion started by: jroberson
8 Replies

10. Shell Programming and Scripting

perl - why is the shell script executed before the print command?

i'm writing some simple scripts to help me learn perl. why does the print command get called after the shell script is executed? the purpose of the shell script is to simply echo to the screen "script run". which is does, but before the print command, you can clearly see the shell script is... (3 Replies)
Discussion started by: mjays
3 Replies
Login or Register to Ask a Question