If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > To perform operations with Directory

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-08, 02:22
Shwethu_Siddu Shwethu_Siddu is offline
Registered User
 
Join Date: Apr 2008
Posts: 1
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
Reply With Quote
  #2 (permalink)  
Old 04-25-08, 23:11
sco08y sco08y is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On