Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2008
    Posts
    1

    Unanswered: To perform operations with Directory

    ________________________________________
    Dear All,
    I am new to PERL programming..i am working for one Software Firm..my problem can be devided as follows

    1. I have one folder in any drive it contains both Source files(.C) and Header
    files(.H) i should get copy of this folder into my current working directory

    2. I should separate both .C and .H files

    3. In .C files there are preprocessor directives like #include "CAN.h" i have to convert this into lower case like #include "can.h"



    Can any body help to solve this?? Thanks in advance

  2. #2
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    Sheesh, you couldn't make life simpler and say what OS you're using.

    Code:
    use File::Spec::Functions qw/rel2abs splitpath catfile/;
    use strict;
    
    sub mmm_good {
      my $d = rel2abs($_[0]);
      mkdir $d unless -e $d;
      die unless -d $d;
      return $d;
    }
    
    my(%dirs);
    $dirs{c} = mmm_good('source');
    $dirs{h} = mmm_good('header');
    
    for my $path (map((glob $_), @ARGV) {
      next unless $path =~ /.([chCH])$/ && -r $path;
      my $ext = lc $1;
      open my $in, "<", $path;
      open my $out, ">", catfile($dirs{$ext}, (splitpath($path))[2]);
      while(my $line = <$in>) {
        $line = lc $line if /^\w*#include/;
        print $out $line;
      }
      close $out;
      close $in;
    }
    Note that this will make two directories in your current working directory. It will overwrite files with the same name. It does not scan subdirectories. It globs (interprets *.* and such) according to csh rules, see perldoc -f glob or perldoc File::Glob. If it runs at all (I haven't tested it) it should run on any OS perl runs on. I think you'll probably need at least perl 5.6 to run this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •