Connecting Tech Pros Worldwide Forums | Help | Site Map

script is printing output correct but not the actual output

Member
 
Join Date: Nov 2006
Location: USA
Posts: 126
#1: Feb 21 '07
script is printing output correct but not the actual output.

Basically what the script is doing it taking a 1 flat file

then it is splits the file into smaller files in 1000 record increments

then it creates another file and adds the header the file contents of the old file.

and the file with the header is then renamed to file format i want which is txt.

Now the problem is that the file never renames but when i print it it shows the new file name as the txt file i wanted. but it never changes the physical file.

Hope you guys could please help me the rest of the script works fine . the file splits file and the header and contents are fine. deleting the old files and renaming to txt is where i am having the problem.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $| = 1;
  3.  
  4. use File::Copy;
  5.  
  6. ######################### Splitting Files ################################
  7. my $read = "Files read from dir";
  8. my $split = "Files split in 1000 records each";
  9. my $del = "Source file deleted";
  10. my $move = "Source files moved to /home/tibco/tibco/Susan/split_finish";
  11.  
  12. open(LOG, ">>/home/baddabing/Susan/splitlog.log") || die (" error recording log: $!");
  13. my $time_now = localtime;
  14.  
  15. my $srcdir = "/home/baddabing/Susan/dropoff";
  16. my $dest = "/home/baddabing/Susan/split_finish";
  17.  
  18. for (;;) {
  19.     opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
  20.     @files = grep {!/^\.+$/} readdir(DIR);
  21.     close(DIR);
  22.  
  23.     if (!@files) {
  24.         print "No files in dir.\n\n";
  25.         last;
  26.     }
  27.  
  28.     print "Time: $time_now ----- $_[0]", "\n";
  29.     print LOG "Time: $time_now ----- $_[0]", "\n";
  30.  
  31.     ##### Reading Header from File #####
  32.     $file = "@files";
  33.     print "Source File is $file \n\n";
  34.  
  35.     open(INFILE, "< $srcdir/$file") or die (" Could not open File $!");
  36.     $line1 = <INFILE>;
  37.     close (INFILE);
  38.     print " This is the header: $line1 \n";
  39.  
  40.     #UNIX Split Command Running
  41.     system "split -l 500 $srcdir/int_sap* $srcdir/int_sap*";
  42.     print "Time: $time_now ----- $_[1]", "\n";
  43.     print LOG "Time: $time_now ----- $_[1]", "\n";
  44.     sleep 2;
  45.  
  46.     system "rm /home/tibco/tibco/Susan/dropoff/*.txt";
  47.     print "Time: $time_now ----- $_[2]", "\n";
  48.     print LOG "Time: $time_now ----- $_[2]", "\n";
  49.     sleep 1;
  50.  
  51.     system "mv /home/baddabing/Susan/dropoff/* /home/baddabing/Susan/split_finish";
  52.     print "Source files were moved to /home/baddabing/Susan/split_finish\n\n";
  53.     print LOG "Time: $time_now ----- $_[3]", "\n";
  54.     sleep 1;
  55.  
  56. ######################### Rename Files ####################################
  57.  
  58.     my $path2 = "/home/tibco/tibco/Susan/split_finish";
  59.     opendir(DIR, $path2) || die "can't opendir $path: $!";
  60.     @files2 = grep {!/^\.+$/} readdir(DIR);
  61.     close(DIR);
  62.  
  63.     $i = 1;
  64.     foreach (@files2) {
  65.         @_ = split(/\./,$_);
  66.  
  67.         my $name = "$_";
  68.  
  69.         my $name3 = "$_[0]";
  70.         chomp($name);
  71.         print "This is the name $name \n\n";
  72.  
  73.         open(INFILE2,"< $dest/$name") || die(" Could not open INFILE2 File!");
  74.         $header = <INFILE2>;
  75.         chomp($header);
  76.         print "This is the infile header: $header \n ";
  77.  
  78.         my $name2 = "$name$i";
  79.  
  80.         print "************* $name2 ********** \n\n\n";
  81.  
  82.         if($header eq "$line1") {}
  83.         else {
  84.             open(OUTFILE2, "> $dest/$name2") || die ("Could not OUTFILE2 open file $!");
  85.             print OUTFILE2 "$line1";
  86.             print OUTFILE2 "$header";
  87.             while (<INFILE2>) {
  88.                 print OUTFILE2 "$_";
  89.             }
  90.             close OUTFILE2;
  91.         }
  92.  
  93.         close INFILE2;
  94.         unlink($name);
  95.         print "File $name Removed \n\n";
  96.  
  97.         my $new_name = $name3.$i.'.txt';
  98.  
  99.         system "rn $dest/$name2 $new_name";
  100.         print "$new_name renaming is completed \n\n";
  101.         sleep 2;
  102.  
  103.         $i++;
  104.         close LOG;
  105.     }
  106.     print "\n\n ---------------- All files processed ------------------\n\n";
  107. }

docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#2: Feb 22 '07

re: script is printing output correct but not the actual output


@jonathan184

Try calling
Expand|Select|Wrap|Line Numbers
  1. rename("$dest/$name2", $new_name) or print "$!\n";
instead of
Expand|Select|Wrap|Line Numbers
  1. system "rn $dest/$name2 $new_name";
Greetz, Doc
Member
 
Join Date: Nov 2006
Location: USA
Posts: 126
#3: Feb 22 '07

re: script is printing output correct but not the actual output


Thanks that worked but if i wanted to put it in the same dir as the source.
the renaming part i mean i want it to rename in the same dir and output there.

Right now the rename works and it outputting to the folder level up.
Member
 
Join Date: Nov 2006
Location: USA
Posts: 126
#4: Feb 22 '07

re: script is printing output correct but not the actual output


Nevermind I got it but thanks for all your help I guess i overlooked the simple stuff :)

Quote:

Originally Posted by docsnyder

@jonathan184

Try calling

Expand|Select|Wrap|Line Numbers
  1. rename("$dest/$name2", $new_name) or print "$!\n";
instead of
Expand|Select|Wrap|Line Numbers
  1. system "rn $dest/$name2 $new_name";
Greetz, Doc

Reply