script is printing output correct but not the actual output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonathan184
    New Member
    • Nov 2006
    • 154

    script is printing output correct but not the actual output

    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.

    Code:
    #!/usr/bin/perl
    $| = 1;
    
    use File::Copy;
    
    ######################### Splitting Files ################################
    my $read = "Files read from dir";
    my $split = "Files split in 1000 records each";
    my $del = "Source file deleted";
    my $move = "Source files moved to /home/tibco/tibco/Susan/split_finish";
    
    open(LOG, ">>/home/baddabing/Susan/splitlog.log") || die (" error recording log: $!");
    my $time_now = localtime;
    
    my $srcdir = "/home/baddabing/Susan/dropoff";
    my $dest = "/home/baddabing/Susan/split_finish";
    
    for (;;) {
    	opendir(DIR, $srcdir) or die "Can't open $srcdir: $!";
    	@files = grep {!/^\.+$/} readdir(DIR);
    	close(DIR);
    
    	if (!@files) {
    		print "No files in dir.\n\n";
    		last;
    	}
    
    	print "Time: $time_now ----- $_[0]", "\n";
    	print LOG "Time: $time_now ----- $_[0]", "\n";
    
    	##### Reading Header from File #####
    	$file = "@files";
    	print "Source File is $file \n\n";
    
    	open(INFILE, "< $srcdir/$file") or die (" Could not open File $!");
    	$line1 = <INFILE>;
    	close (INFILE);
    	print " This is the header: $line1 \n";
    
    	#UNIX Split Command Running
    	system "split -l 500 $srcdir/int_sap* $srcdir/int_sap*";
    	print "Time: $time_now ----- $_[1]", "\n";
    	print LOG "Time: $time_now ----- $_[1]", "\n";
    	sleep 2;
    
    	system "rm /home/tibco/tibco/Susan/dropoff/*.txt";
    	print "Time: $time_now ----- $_[2]", "\n";
    	print LOG "Time: $time_now ----- $_[2]", "\n";
    	sleep 1;
    
    	system "mv /home/baddabing/Susan/dropoff/* /home/baddabing/Susan/split_finish";
    	print "Source files were moved to /home/baddabing/Susan/split_finish\n\n";
    	print LOG "Time: $time_now ----- $_[3]", "\n";
    	sleep 1;
    
    ######################### Rename Files ####################################
    
    	my $path2 = "/home/tibco/tibco/Susan/split_finish";
    	opendir(DIR, $path2) || die "can't opendir $path: $!";
    	@files2 = grep {!/^\.+$/} readdir(DIR);
    	close(DIR);
    
    	$i = 1;
    	foreach (@files2) {
    		@_ = split(/\./,$_);
    
    		my $name = "$_";
    
    		my $name3 = "$_[0]";
    		chomp($name);
    		print "This is the name $name \n\n";
    
    		open(INFILE2,"< $dest/$name") || die(" Could not open INFILE2 File!");
    		$header = <INFILE2>;
    		chomp($header);
    		print "This is the infile header: $header \n ";
    
    		my $name2 = "$name$i";
    
    		print "************* $name2 ********** \n\n\n";
    
    		if($header eq "$line1") {}
    		else {
    			open(OUTFILE2, "> $dest/$name2") || die ("Could not OUTFILE2 open file $!");
    			print OUTFILE2 "$line1";
    			print OUTFILE2 "$header";
    			while (<INFILE2>) {
    				print OUTFILE2 "$_";
    			}
    			close OUTFILE2;
    		}
    
    		close INFILE2;
    		unlink($name);
    		print "File $name Removed \n\n";
    
    		my $new_name = $name3.$i.'.txt';
    
    		system "rn $dest/$name2 $new_name";
    		print "$new_name renaming is completed \n\n";
    		sleep 2;
    
    		$i++;
    		close LOG;
    	}
    	print "\n\n ---------------- All files processed ------------------\n\n";
    }
    Last edited by miller; Feb 22 '07, 01:31 AM. Reason: Code Formatting
  • docsnyder
    New Member
    • Dec 2006
    • 88

    #2
    @jonathan184

    Try calling
    Code:
    rename("$dest/$name2", $new_name) or print "$!\n";
    instead of
    Code:
    system "rn $dest/$name2 $new_name";
    Greetz, Doc

    Comment

    • jonathan184
      New Member
      • Nov 2006
      • 154

      #3
      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.

      Comment

      • jonathan184
        New Member
        • Nov 2006
        • 154

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

        Originally posted by docsnyder
        @jonathan184

        Try calling
        Code:
        rename("$dest/$name2", $new_name) or print "$!\n";
        instead of
        Code:
        system "rn $dest/$name2 $new_name";
        Greetz, Doc

        Comment

        Working...