Perl- Output file is always 0 byte


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl- Output file is always 0 byte
# 1  
Old 06-30-2010
Perl- Output file is always 0 byte

Hi all,

I am new to perl programming. However i have a script that connects to the database and spools that into an output file.
Strange thing is that sometimes this script works and sometimes the ouput spool file is always 0 byte.
I have verified the sql query and the query always returns some value not sure why perl is not able to output this value to my spool file

- providing a Snapshot of my Code
Code:
open(Spool, "> $filename") or die "Error opening $filename\n";

while( $Conn->FetchRow() ) {

     my $Temp =$Conn->Data("STATUS");
     print "Value is $Temp";  --> $Temp has always some value in it
-- Approach 1
    print Spool $Temp; --> This does not work output file is 0 byte
--- Approach 2
    printf(Spool "%s\n", $Conn->Data("STATUS" )); --> This does not work output file is 0 byte
     }
     
   $Conn->Close();
   
   close Spool;


I have used a similar code before and their it seems to work

Code:
 open(Spool, "> $filename") or die "Error opening $filename\n";
  my $blank = "| ";
  my $lineCtr = 0;
  printf(Spool "****************Biztalk Check****************\n");
while( $Conn->FetchRow() ) {


    printf(Spool "\n%s %s %s %s\n",
    $Conn->Data("COMP"),
    $Conn->Data("SI"),
    $Conn->Data("num_swifts"),
    $Conn->Data("STATUS")
   );
     }
     
   $Conn->Close();
   
   close Spool;

Could anyone suggest what is wrong here ?
# 2  
Old 06-30-2010
There is no problem in the statements used in both the approaches.
1. check that zero file size is created as soon as you ran the perl script. Or output or previous run.
2. Check the file was created by another user you dont have permission to over write/append it.
# 3  
Old 06-30-2010
I am executing it from my local machine and i have write access to the share where i spool the file, the file is created 0 byte so it rules out the permission issue
# 4  
Old 06-30-2010
Can you post your entire Perl program ?

tyler_durden
# 5  
Old 06-30-2010
Here is the entire code

Code:
use Win32::ODBC;
use Time::Local;
use Getopt::Std;




my $cString = sprintf("Driver=SQL Server;Server=szrh721;Database=pagesSys_5701SR4;Trusted Connection=yes");   
my $filename="C:\\Perl\\scriptt\\DB.txt";
#my $filename="P:\\DB.txt";
unlink($filename);
$dbQuery = "BEGIN

SET NOCOUNT ON

DECLARE
\@V_BATCHJOBID INT ,
\@V_jobaccountid INT ,
\@V_jobaccountstatusid INT,
\@V_PCName VARCHAR(50),
\@V_JobName VARCHAR(50),
\@V_Description VARCHAR(50),
\@V_ErrorMessage VARCHAR(3000),
\@V_StatusTimeStamp DATETIME ,
\@V_Flag_Stuck VARCHAR(2),
\@V_Limit varchar(2)
-- One hour
SET \@V_Limit =1


DECLARE CUR_PEND CURSOR FOR 
SELECT  JobType.BATCHJOBID,
	PCStatus.jobaccountid,
	CurrStatus.jobaccountstatusid,
	JobType.PCName,
        JobType.Name,
        PCStatus.Description,
        PCStatus.ErrorMessage,
        PCStatus.StatusTimeStamp
  FROM (SELECT BJ.PCName, BJ.BATCHJOBID AS BATCHJOBID ,BJ.Name as JobName, BJT.Name, BJT.TimeLimit
          FROM BatchJob BJ (nolock), BatchJobType BJT (nolock)
         WHERE BJ.BatchJobTypeID = BJT.BatchJobTypeID
           AND (BJ.BatchJobStatusID = 1 OR BJ.BatchJobStatusID = 3)
           AND BJ.BatchJobID =
               (SELECT Min(BJ2.BatchJobID)
                  FROM BatchJob BJ2 (nolock)
                 WHERE BJ2.PCName = BJ.PCName
                   and (BJ.BatchJobStatusID = 1 OR BJ.BatchJobStatusID = 3))) JobType
  LEFT OUTER JOIN (SELECT Distinct Jas.PCName,JAS.jobaccountstatusid,JAS.jobaccountid,
                                   Acs.Description,
                                   Jas.StatusTimeStamp,
                                   Jas.ErrorMessage
                     FROM AccountStatus Acs (nolock), JobAccountStatus Jas (nolock)
                    WHERE Jas.StatusID = Acs.StatusID
                      AND Jas.StatusTimeStamp =
                          (SELECT Max(Jas2.StatusTimeStamp)
                             FROM JobAccountStatus Jas2 (nolock)
                            WHERE Jas.PCName = Jas2.PCName)) PCStatus ON JobType.PCName =
                                                                         PCStatus.PCName
  LEFT OUTER JOIN (SELECT BJ.PCName, Count(*) as c
                     FROM BatchJob BJ (nolock)
                    WHERE (BJ.BatchJobStatusID = 1 OR
                          BJ.BatchJobStatusID = 3)
                    Group by PCName) Assigned ON JobType.PCName =
                                                 Assigned.PCName
 JOIN (select jobaccountstatusid from JobAccountStatus(nolock)) CurrStatus 
on CurrStatus.jobaccountstatusid=PCStatus.jobaccountstatusid
OPEN CUR_PEND

FETCH NEXT FROM CUR_PEND INTO 
\@V_BATCHJOBID  ,
\@V_jobaccountid ,
\@V_jobaccountstatusid,
\@V_PCName ,
\@V_JobName ,
\@V_Description ,
\@V_ErrorMessage ,
\@V_StatusTimeStamp 

	WHILE \@\@FETCH_STATUS = 0
	BEGIN
     --- Find current pending reports
SET \@V_Flag_Stuck= (SELECT COUNT(b.BatchJobID)
 FROM   BatchJob b (nolock)
 WHERE  b.NextDate <= getdate()
 AND b.Active = 1
 AND b.BatchJobStatusID <= 3 
 AND B.BATCHJOBID <> \@V_BATCHJOBID)



-- FORMAT FOR OUTPUT
/*
       select @v_batchjobid as Batchjobid,
	      @V_StatusTimeStamp as VisibleTime,
	      BJ2.STARTDATE as ActualStartTime,
	      @V_PCName as PCNAME,
	      datediff(mi,BJ2.STARTDATE,getdate())as RunTime from  BatchJob BJ2 
	      WHERE BJ2.BATCHJOBID= @V_BATCHJOBID 
              and  datediff(mi,BJ2.STARTDATE,getdate()) > 2 */

 select  'JOB ID: '+cast(\@v_batchjobid as varchar(12))+
	 ' STARTED AT: '+  CAST(  \@V_StatusTimeStamp AS VARCHAR(20))+
	 ' ON '+    \@V_PCName +
	 ' HAS BEEN RUNNING FOR MORE THAN '+  CAST(  datediff(hh,BJ2.STARTDATE,getdate()) AS VARCHAR(8))+
         ' Hour(s). CURRENTLY '+
          CAST (\@V_Flag_Stuck AS VARCHAR(10)) +
         ' WAITING ' as STATUS from  BatchJob BJ2 (nolock)
	      WHERE BJ2.BATCHJOBID= \@V_BATCHJOBID 
              and  datediff(mi,BJ2.STARTDATE,getdate()) >= \@V_LIMIT



	FETCH NEXT FROM CUR_PEND INTO 
	\@V_BATCHJOBID  ,
	\@V_jobaccountid ,
	\@V_jobaccountstatusid,
	\@V_PCName ,
	\@V_JobName ,
	\@V_Description ,
	\@V_ErrorMessage ,
	\@V_StatusTimeStamp

        END

CLOSE CUR_PEND
DEALLOCATE CUR_PEND

END";


############################################################
# connect
my $Conn;
$Conn = new Win32::ODBC($cString);
die "Unable to connect to database" . Win32::ODBC::Error(  ) . "\n"
  unless (defined $Conn);

#printf("Using %s database on %s \n\n", $configData->{"db"}, $configData->{"server"} );

#printf("%s\n\n", $sql);

if( $Conn->Sql($dbQuery ) )  
  {
  print "Query failed. \n";
  print "Error: " . $Conn->Error() . "\n";
  print $dbQuery;
  $Conn->Close();
  exit(1);
  }    
  
  my $temp="";

  open(Spool, ">>$filename") or die "Error opening $filename\n";
 
while( $Conn->FetchRow() ) 
{
     my $Temp =$Conn->Data("STATUS");
     #print "Value is $Temp";
     print Spool "This line goes to the file.\n"; #Testing 
     printf(Spool "%s",$Conn->Data("STATUS" ));
 }
     
    $Conn->Close();
    close Spool;
   
   
   ###Read Spool file if it has something to say
   
   open(Spool, "> $filename") or die "Error opening $filename\n";
    while (<spool>) {}
    my $lineCtr =  $.;
   print $lineCtr;
   
   if (@linctr >0) {
   #Send Email using VB code
   
   system("start C:\\My-Scripts\\Email_Batch.vbs"); 
   
 }
    
   #unlink($filename);
   exit(0);



---------- Post updated at 02:27 PM ---------- Previous update was at 02:08 PM ----------

Sorry, I found my Mistake, After spooling to the file , i again open the file for writing , this creates a new file which is 0 byte..
Apologies for wasting everyones time :-(
# 6  
Old 06-30-2010
Your problem is near the end of the script:

Code:
 ###Read Spool file if it has something to say
   
   open(Spool, "> $filename") or die "Error opening $filename\n";
    while (<spool>) {}
    my $lineCtr =  $.;
   print $lineCtr;
   
   if (@linctr >0) {
   #Send Email using VB code
   
   system("start C:\\My-Scripts\\Email_Batch.vbs"); 
   
 }
    
   #unlink($filename);
   exit(0);

U have to use:
Code:
open(Spool, "> $filename") or die "Error opening $filename\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output after a perl script gives a file with size zero.

Hi, I have a unix shell script which generates a flat file after connecting to Teradata servers to fetch tables and views and also picks up modified unix scripts from the specified paths. Later on the script calls a perl script to assign a value based on the type of object in the flat file which... (2 Replies)
Discussion started by: yohasini
2 Replies

2. Programming

Mod Perl 2 with byte range help

I am writing a mod perl 2 download module and I am facing the same issue as this guy. mp2 / Apache byterange filter | ModPerl | ModPerl If I remove the check for EOS in byterange_filter.c and recompile Apache2, the byte range filter is executed and the result is correct but I am not sure that... (0 Replies)
Discussion started by: metalbone
0 Replies

3. Shell Programming and Scripting

Creating output file using Perl

As an simple example, I have the following files fin1.zv being a one column set of number 1 90 2 80 3 60 4 30 5 20 fin2.zv is another file like this 1 10 20 30 40 50 2 60 70 80 90 0 3 90 80 70 60 50 4 40 30 20 10 0 5 10 20 30 40 50 (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

taking output in csv file from perl

Hi, I am new to perl I need to connect from linux server to oracle database and i need to query the database and take result into csv file. i try to do but i am getting this error: #!/usr/bin/perl use DBI; BEGIN { $ENV{ORACLE_HOME} = '/home/oracle/product/8.1.7'; ... (1 Reply)
Discussion started by: prakash.gr
1 Replies

5. Shell Programming and Scripting

Remove a byte(Last byte from the last line)

Hi All Can anyone please suggest me how to remove the last byte from a falt file .This is from the last line's last BYTE. Please suggest me something. Thank's and regards Vinay (1 Reply)
Discussion started by: vinayrao
1 Replies

6. Shell Programming and Scripting

perl redirect output to file ..not working

here is simple perl script i wanted for my net connection ... just to check if default gateway is pingable or not if not write in log file but problem is that i can not write in file i can print on STDOUT but not in file ...why so ?? same thing was there when i was tying to write on sockets... (7 Replies)
Discussion started by: zedex
7 Replies

7. Shell Programming and Scripting

Check if 2 files are identical byte-to-byte?

In my server migration requirement, I need to compare if one file on old server is exactly the same as the corresponding file on the new server. For diff and comm, the inputs need to be sorted. But I do not want to disturb the content of the file and need to find byte-to-byte match. Please... (4 Replies)
Discussion started by: krishmaths
4 Replies

8. Shell Programming and Scripting

Append Output to another file in Perl

Hi All, I am writing a Perl script such that the output from "perl myscript.pl file1" to be appended to another file name called file2. I tried out with the below code but couldn't work. Can any expert give me some advice? open(OUTPUT, 'perl myscript.pl file1 |'); close OUTPUT;... (7 Replies)
Discussion started by: Raynon
7 Replies

9. Shell Programming and Scripting

formating array file output using perl

Hello, I am trying to output the values in an array to a file. The output needs to be formated such that each array value is left jusified in a field 8 character spaces long. Also, no more than 6 fields on a line. For example: @array= 1..14; Needs to be output to the file like so: 1 ... (4 Replies)
Discussion started by: seismic_willy
4 Replies

10. Shell Programming and Scripting

Cut output to same byte position

Hi folks I have a file with thousands of lines with fixed length fields: sample (assume x is a blank space) 111333xx444TTTLKOPxxxxxxxxx I need to make a copy of this file but with only some of the field positions, for example I'd like to copy the sample to the follwing: so I'd like to... (13 Replies)
Discussion started by: HealthyGuy
13 Replies
Login or Register to Ask a Question