/ / STDERRのリダイレクトが閉じない-windows、perl、file-io

STDERRのリダイレクトが閉じない-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;
}
}

回答:

回答№1は1

readdir パスではなくファイル名を返します。

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

回答№2の場合は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);