#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    21
    Rep Power
    0

    Output .Resx From .CS


    Hi All,

    .CS contains string within double quotes and I am trying to extract these strings into .resx file.
    The existing code output the .resx but with only one string whereas .CS file contains more than one strings in quotes.
    Can you please provide any reference to achieve this?

    Code:
    use strict;
    use warnings;
    use File::Find;
    use XML::Writer;
    use Cwd;
    
    #user input: [Directory]
    my $wrkdir = getcwd;
    system "attrib -r /s";
    print "Processing $wrkdir\n";
    find( \&recurse_src_path, $wrkdir );
    
    sub recurse_src_path
    {
    	my $file  = $File::Find::name;
    	my $fname = $_;
    	my @lines;
    	my $line;
    	if ( ( -f $file ) && ( $file =~ /.*\.cs$/i ) )
    	{
    		print "..";
    		open( FILE, $file ) || die "Cannot open $file:\n$!";
    		while ( $line = <FILE> )
    		{
    			if ( $line =~ s/\"(.*?)\"/$1/m )
    			{
    				chomp $line;
    				push( @lines, $line );
    				my $nl = '0';
    				my $dataIndent;
    				my $output = new IO::File(">Test.resx");
    
    				#binmode( $output, ":encoding(utf-8)" );
    				my $writer = XML::Writer->new(
    											   OUTPUT      => $output,
    											   DATA_MODE   => 1,
    											   DATA_INDENT => 2
    				);
    				$writer->xmlDecl("utf-8");
    				$writer->startTag('root');
    				foreach my $r ($line)
    				{
    					print "$1\n";
    					$writer->startTag( 'data', name => $_ );
    					$writer->startTag('value');
    					$writer->characters($1);
    					$writer->endTag('value');
    					$writer->startTag('comment');
    					$writer->characters($1);
    					$writer->endTag('comment');
    					$writer->endTag('data');
    				}
    				$writer->endTag('root');
    				$writer->end;
    				$output->close();
    			}
    		}
    		close FILE;
    	}
    }

    Thanks,
    ShailShin
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    You're reopening your output file for every line that matches your regex pattern which means that you're over writing the file each time.

    Your foreach my $r ($line) block is "looping" over a scalar which means that it only has 1 iteration. Putting those statements in a foreach block that only iterates once doesn't make any sense.

    Using File::Find::Rule would be a better choice to filter on the .cs files.
    Last edited by FishMonger; July 10th, 2014 at 10:29 AM.

IMN logo majestic logo threadwatch logo seochat tools logo