Hi folks,
I have a script that's two fold (it's actually two scripts). The first part grabs a recursive list of files and folders in a directory structure, and strips out CVS files. It then dumps them to a config file that's just a staight line by line list of the files, which I in turn read back into another script.
I realize this is a pretty newbie question, so I need to precursor this with the fact that I'm very, very new at Perl and most of my scripting skill is based on searches here, google and O'Reilly books. :)
Anyhow, my script outputs files, but puts in the "./" before the filename. So my config file reads as:
./filename.x
./otherfile.x
etc, etc
I'd like to strip out the leading "./" before putting them into the output file, so that I can use the second script to run a "cvs co" on each line in the config file.
Without further ado, here's my script, with the header spam stripped:
use strict;
use Data::Dumper;
use IO::File;
######################################################################
+#####
# Helper sub routine calls
######################################################################
+#####
system ("clear");
print "Building project config.\n\n";
sub find
{
my ( $dir ) = @_;
my $dh;
my @theseFiles;
+
opendir $dh, $dir or return;
while(my $file = readdir $dh )
{
next if $file =~ /^\.{1,2}$/;
my $full_file = "$dir/$file";
if ( -d $full_file )
{
push(@theseFiles,find($full_file));
}
else
{
push(@theseFiles,$full_file);
}
}
closedir $dh;
return @theseFiles;
}
######################################################################
+#####
# Main routine starts here
######################################################################
+#####
## Grab all files ##
my @files = find('.');
## Filter out CVS ##
my @noCvsDirs;
foreach my $file (@files) {
push(@noCvsDirs,$file) unless(($file =~ /\/CVS\//) || ($file =~ /\
+~/));
}
## Save out to config file hwconfig.cfg ##
#my $dump = Data::Dumper->new([\@noCvsDirs])->Purity(1)->Indent(0);
#my $state = $dump->Dump();
my $state = join("\n",@noCvsDirs);
my $fh = new IO::File(">hwconfig.cfg");
die("Checkpoint failed for hwconfig.cfg - $!\n") unless($fh);
my $bytes = $fh->syswrite($state, length($state));
$fh->close();
unless($bytes == length($state)) {
die("Checkpoint failed, incomplete write for hwconfig.cfg\n");
} else {
print "Saved $bytes bytes into file: hwconfig.cfg\n";
}
exit;
If someone could tell me the best way to remove the "./" from each filename that gets pushed to the array, I'd really appreciate it.
Thanks much!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.