#!C:\Interwoven\TeamSite/iw-perl/bin/iwperl use File::Copy; use TeamSite::Config; use XML::DOM; use XML::Parser; use open ':utf8'; use TeamSite::DCRnode; use TeamSite::DCRparser; use Time::localtime; use File::Basename; use Encode; use strict; use warnings; open(LOG ,">C:/Interwoven/TeamSite/tmp/Cloning.log"); my $Workarea = ""; my $fileLine = $ARGV[0]; if ($fileLine ne "") { my @fline = split("####",$fileLine); my @clone = split("~",$fline[1]); for (my $c=1;$c<= $#clone ;$c++) { my $cloneName = $clone[$c]; my $pathSource = $fline[0]; print LOG "Arguments Provided :-\n[$cloneName]\n[$pathSour +ce]\n"; print LOG " The source of the HTMLS:- $pathSource \n"; print LOG " The dest of the HTMLS:- $cloneName \n"; my $path = "Y:/"; $pathSource =~ s/\\/\//g; my @pathSourceArr = split(/\//,$pathSource); my $source_branch =$pathSourceArr[$#pathSourceArr-1]; print LOG " The source of the HTMLS after replacement :- $ +pathSource \n"; my @spiltSource = split("/",$pathSource); $pathSource = "Y:/"; my $templateData = "Y:/"; print LOG "The splitted source path :- \n @spiltSource\n"; for (my $i=3;$i < $#spiltSource ;$i++) { $path .= $spiltSource[$i]."/"; $pathSource .= $spiltSource[$i]."/"; if (($spiltSource[$i] ne "html")&& ($spiltSource[$i] n +e "content") ) { $templateData .= $spiltSource[$i]."/"; } if($spiltSource[$i] eq "WORKAREA") { $Workarea = $spiltSource[$i+1]; } } print LOG "Workarea Name :- [$Workarea]\n"; my $xmlFile = $templateData . "xml/" . $Workarea . "/siteN +ame.xml"; print LOG " siteName xml file Path :-[$xmlFile]\n"; my $AddToMetadata = "Yes"; if(-e $xmlFile) { my $parser=new XML::DOM::Parser; my $doc=$parser->parsefile($xmlFile); my $nodes = $doc->getElementsByTagName("itemID"); my $nname = $doc->toString; my $rootnode= TeamSite::DCRnode->new($nname); my @new_node = $rootnode->get_node_list('itemID'); + # getting the array of all the siteName nodes. my $n = $nodes->getLength; for (my $i = 0; $i <= $#new_node; $i++) { my $att_value = $new_node[$i]->value(); if( $att_value eq $cloneName ) { print LOG " +****** siteName Updation Starts Here(If File exists in Xml) ***** + \n"; $new_node[$i]->set_inner_xml($cloneName); my $pro_node_xml = $rootnode->get_xml(); print LOG "\nNew Meta Data XML :-\n $pro_n +ode_xml \n"; open(FILE,">$xmlFile"); print FILE "<?xml version=\"1.0\" encoding +=\"UTF-8\"?>"; print FILE $pro_node_xml; close(FILE); $AddToMetadata = "No"; print LOG "After checking the siteName\.xm +l AddToMetadata or not :- [$AddToMetadata] \n"; print LOG " +****siteName Updation Ends Here**** "; last; } } if ($AddToMetadata eq 'Yes') { print LOG " +*****siteName Updation Starts Here(If File doesnot exists in the Xml) + ***** \n"; $rootnode->set_inner_xml($rootnode->get_in +ner_xml() . "\n<itemID>$cloneName</itemID>\n"); my $pro_node_xml = $rootnode->get_xml(); print LOG "\nNew Meta Data XML :-\n $pro_n +ode_xml \n"; open(FILE,">$xmlFile"); print FILE "<?xml version=\"1.0\" encoding +=\"UTF-8\"?>"; print FILE $pro_node_xml; close(FILE); print LOG " +*****siteName Updation Ends Here**** "; } } else { print LOG " **** sit +eName Creation Starts Here(IF siteName.xml does not exists) **** + \n"; open(FILE,">$xmlFile"); print FILE "<?xml version=\"1.0\" encoding=\"UTF-8 +\"?>"; print FILE "<content locale=\"$Workarea\">\n<itemI +D>$cloneName</itemID>\n</content>"; close(FILE); print LOG " *****sit +eName Updation Ends Here**** "; } $path .= $cloneName ; $pathSource .= $spiltSource[$#spiltSource]; print LOG "The constructed source and destination path is +:- \n $pathSource and $path \n"; print LOG "The constructed templatedata path is :- \n $tem +plateData \n"; if( -e $path){} else{ mkdir($path,"0777") or die "Unable to create folder: $!"; } #opendir(DIR, $from_dir); #my @dir1 = readdir(DIR); #closedir(DIR); sub copy_recursively { my ($from_dir,$to_dir) = @_; opendir(DIR, $from_dir) or die "Could not open dir '$f +rom_dir': $!"; my @dir = readdir(DIR); for(my $j=2 ; $j <= $#dir; $j++){ my $source = "$from_dir/$dir[$j]"; my $destination = "$to_dir/$dir[$j]"; next if($dir[$j] eq "." || $dir[$j] eq ".."); if (-d $source) { print LOG "The source and destination in the d +irectory section :-\n $source and $destination\n"; mkdir($destination,"0777") or die "mkdir '$des +tination' failed: $!" if not -e $destination; copy_recursively($source, $destination); } else { print LOG "The source and destination in t +he File section :-\n $source and $destination\n"; open(FILE,"<", $source); my @file_content = <FILE>; close(FILE); for(my $u=0;$u<=$#file_content;$u++) { $file_content[$u] = s/logo-$source_bra +nch/logo-$cloneName/g ; print LOG "$file_content[$u]"; } open(FILEOUT,">", $destination); for(my $v=0;$v<=$#file_content;$v++) { print FILEOUT "$file_content[$v]"; } #copy($source, $destination) or die "copy +failed: $!"; my $cmd = "iwextattr -g TeamSite/Templatin +g/PrimaryDCR $source"; my $dcrName = `$cmd`; my $cmdDcrType = "iwextattr -g TeamSite/Te +mplating/PrimaryDocumentType $source"; my $dcrType =`$cmdDcrType`; print LOG "CMD command execueted with the +HTML for DCR Type :- [$cmdDcrType]\n\n$dcrType\n"; if($dcrName ne "" && $dcrType ne "") { my @dcrNameSplit = split("/",$dcrName) +; print LOG "CMD command execueted with +the HTML for DCR Name :- [$cmd]\n\n[$dcrName]\n\n@dcrNameSplit\n"; my $dcrPath = "$templateData"; $dcrPath .= "templatedata/$dcrType/dat +a/$cloneName"; mkdir($dcrPath,"0777")or die "mkdir '$ +dcrPath' failed: $!" if not -e $dcrPath; $dcrPath .= "/"; my $setDCRName = "$cloneName"; for (my $k=1;$k<$#dcrNameSplit ;$k++) +{ $dcrPath .= $dcrNameSplit[$k]; mkdir($dcrPath,"0777")or die "mkdi +r '$dcrPath' failed: $!" if not -e $dcrPath; $dcrPath .= "/"; $setDCRName .= "/$dcrNameSplit[$k] +" } my @splitDcrType = split("/",$dcrType) +; my $PT = $splitDcrType[$#splitDcrType] +."\.xsl"; $dcrPath .= $dcrNameSplit[$#dcrNameSpl +it]; $setDCRName .= "/$dcrNameSplit[$#dcrNa +meSplit]"; print LOG "\n The New DCR Pathh is:- $ +dcrPath\n"; my $dcrSource = "$templateData"; $dcrSource .= "templatedata/$dcrType/d +ata/$dcrName"; print LOG "\n The DCR Pathh is:- $dcr +Source\n"; copy($dcrSource,$dcrPath) or die "copy + of the dcr failed: $!"; my $cmd1 = "iwextattr -s TeamSite/Temp +lating/DCR/Type=$dcrType $dcrPath"; my $new1 = `$cmd1`; my $cmdToSet = "iwextattr -s TeamSite/ +Templating/PrimaryDCR=$setDCRName $destination"; my $newDcrName = `$cmdToSet`; my $cmd2 = "iwextattr -s TeamSite/Temp +lating/PrimaryDocumentType=$dcrType $destination"; my $new2 = `$cmd2`; my $cmd3 = "iwextattr -s TeamSite/Temp +lating/PrimaryPT=$PT $destination"; my $new3 = `$cmd3`; print LOG "\n The New output Type[$new +DcrName][$cmdToSet] is set for the DCR :- $dcrPath\n"; } } } closedir(DIR); return; } sub alter_content_for_logo{ } copy_recursively($pathSource, $path); } } close(LOG); #unlink($fileName); exit;
In this script, we are cloning an entire folder and ceating a new folder. All the folders within folder and files within them are all created in new folder. SOme of the files contain logo of old folder which is renamed to new logo and we have a code for than in this script. which is as follows
sub copy_recursively { my ($from_dir,$to_dir) = @_; opendir(DIR, $from_dir) or die "Could not open dir '$f +rom_dir': $!"; my @dir = readdir(DIR); for(my $j=2 ; $j <= $#dir; $j++){ my $source = "$from_dir/$dir[$j]"; my $destination = "$to_dir/$dir[$j]"; next if($dir[$j] eq "." || $dir[$j] eq ".."); if (-d $source) { print LOG "The source and destination in the d +irectory section :-\n $source and $destination\n"; mkdir($destination,"0777") or die "mkdir '$des +tination' failed: $!" if not -e $destination; copy_recursively($source, $destination); } else { print LOG "The source and destination in t +he File section :-\n $source and $destination\n"; open(FILE,"<", $source); my @file_content = <FILE>; close(FILE); for(my $u=0;$u<=$#file_content;$u++) { $file_content[$u] = s/logo-$source_bra +nch/logo-$cloneName/g ; print LOG "$file_content[$u]"; } open(FILEOUT,">", $destination); for(my $v=0;$v<=$#file_content;$v++) { print FILEOUT "$file_content[$v]"; }
When I am not running this piece of code, all the content in files come up perfect. But when I run the script with this piece of code, content in file does not get filled and all the files are zero size files. What is the reason that files are not getting populated with content ?

Please dont worry about the TeamSite packages used in here. Problem is with the piece of code and the way files are opened, arrayed and written back.

Thanks


In reply to Perlscript is not generating output in files by manishrathi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.