STDERR的重定向未关闭

时间:2015-11-13 20:59:16

标签: windows perl file-io

我将STDERR重定向到错误文件,但如果错误文件为空则无法取消链接。我相信我没有释放STDERR,它使错误文件繁忙而无法删除。你怎么看?谢谢!

$errFile = $outFile . "-error";
open (ERRFILE, '>', $errFile) or die $!;

#Redirect STDERR from the console to the error log
open (STDERR, '>', $errFile) or die $!;

# Do stuff....

close(STDERR);
close(ERRFILE);

#Remove blank error files
opendir(DIR, 'c:\LMITS');
@errFiles = grep /error/, readdir DIR;
closedir DIR;

foreach $errFile (@errFiles) {
    $errFileSize = -s $errFile;
    if ($errFileSize == 0) {
        unlink $errFile;
    }
}

2 个答案:

答案 0 :(得分:1)

readdir返回文件名,而不是路径。

foreach  (@errFiles) {
    my $errFile = 'c:\\LMITS\\' . $_;
    ...
}

答案 1 :(得分:0)

此代码有效但如果我在脚本中移动命令以关闭SDTERR和ERRFILE,则不会删除任何空白的ERRFILE。我现在还好,但我会继续研究,只为了解。

use CQPerlExt;
use POSIX qw(strftime);
use Strict;

my $checkForBlanks;
my $dbConfig;
my $dbConfigRecord;
my $entitydef;
my $errFile;
my @errFiles;
my $errFileSize;
my $fileDate;
my @fieldNames;
my $fieldName;
my $lastSync;
my $outFile;
my $queryDef;
my $queryResults;
my $recordCount = 0;
my $recordType;
my $session;
my $scriptStartTime;
my $swCR;
my $swData;
my $swID;

# ##############################################
# ##### Process administrative items and 
# ##### establish a ClearQuest session
# ##############################################

$scriptStartTime = strftime("%Y-%m-%d %I:%M:%S %p", localtime());
$fileDate = strftime("%Y%m%d_%I%M%S%p", localtime());

#Create and open the output and error files
$outFile = "MSTU_Unclass_Export"."_".$fileDate.".txt"; 
open (OUTFILE, ">", $outFile) or die $!;
$errFile = $outFile . "-error";
open (ERRFILE, '>', $errFile) or die $!;

#Redirect STDERR from the console to the error log
open (STDERR, '>', $errFile) or die $!;

$session = CQSession::Build();
CQSession::UserLogon($session, "uname", "pw", "db", "schema");  

$dbConfigRecord = $session->GetEntity("DB_CONFIG", "33554467");
$lastSync = $dbConfigRecord->GetFieldStringValue("LastSyncDate");

# ##############################################
# ##### Query the database for all SWCRs
# ##### updated after lastSyncDate
# ##############################################

$queryDef = $session->BuildQuery("SWCR");
$queryDef->BuildField("dbid");

@lastSyncDate = ($lastSync);
$operator = $queryDef->BuildFilterOperator($CQPerlExt::CQ_BOOL_OP_AND);
$operator->BuildFilter ("history.action_timestamp", $CQPerlExt::CQ_COMP_OP_GTE,\@lastSyncDate); 

$queryResults = $session->BuildResultSet($queryDef);
$queryResults->Execute();

# ##############################################
# ##### Build a text file with SWCR data associated
# ##### with the dbids returned above
# ##############################################

#Get all of the fieldnames you want to export
$recordType = 'SWCR';
$entitydef = $session->GetEntityDef($recordType);
@fieldNames = @{$entitydef->GetFieldDefNames()};

#Remove any fields you don't want
@fieldNames = grep ! /dbid|history|RecordID|CCObjects|MergeSWCRs|AssociatedIntegrationSet|Level1TestResults|
                     Level2TestResults|Level3TestResults|Level4TestResults|Reviews|WithdrawCR|
                     AssociatedWithdrawnCR|Attachments|AssociatedPRs|OriginatingSolution|AssociatedSWRsFull|
                     AssociatedSWRsDelta|ClonedFrom|ClonedTo|AssociatedComment|ExternalLinks|ratl_mastership/x, @fieldNames;

while ($queryResults->MoveNext() == $CQPerlExt::CQ_SUCCESS) {
        $swCR = $session->GetEntityByDbId("SWCR", $queryResults->GetColumnValue(1));

        #Gather data        
        $swID = $swCR->GetFieldValue("RecordID")->GetValue();
        $swData = "<RecordID>" . $swID . "</RecordID>";
            foreach $fieldName (@fieldNames)
                {
                $checkForBlanks = $swCR->GetFieldStringValue($fieldName);
                if ($checkForBlanks ne ""){
                $swData = $swData . "<" . $fieldName . ">" . $swCR->GetFieldStringValue($fieldName) . "</" . $fieldName . ">";
                                          }
                            }

        #Build file with records seperated by custom line delimiter
        print OUTFILE $swData . "~~lineDelimiter~~\n";
        #Keep track of the amount of records being exported
        $recordCount++;
                                 }

close(STDERR);
close(ERRFILE);
close(OUTFILE);

# ##############################################
# ##### Process administrative items and 
# ##### close ClearQuest session
# ##############################################

#Remove extra carriage return at bottom of export file because this will throw an error when an import is performed
truncate($outFile, (-s $outFile) - 2);

#Add amount of records exported to the export log
open (EXPLOG, ">>", 'Export_Log.txt') or die $!; 
print EXPLOG "$scriptStartTime: $recordCount record(s) written to $outFile for export.\n";
close (EXPLOG);

#Set the LastSyncDate field to the time the export script started
$dbConfigRecord = $session->GetEntity("DB_CONFIG", "33554467");
$session->EditEntity($dbConfigRecord, "Modify");
$dbConfigRecord->SetFieldValue("LastSyncDate",$scriptStartTime);
$dbConfigRecord->Validate();
$dbConfigRecord->Commit();

#Remove blank error files
opendir(DIR, 'c:\LMITS');
@errFiles = grep /error/, readdir DIR;
closedir DIR;

    foreach $errFile (@errFiles) {
        $errFileSize = -s $errFile;
        if ($errFileSize == 0) {
        unlink $errFile;
                       }
                     }

CQSession::Unbuild($session);