For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > writing directory name into a file inside the directory









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author writing directory name into a file inside the directory
Sintral@Gmail.Com

2007-07-12, 9:58 pm

Here's a snippet of my tree:

|-- PresidentialSeatingP4
| |-- 702
| | |-- Photos
| | |-- Templates
| | `-- models
| | |-- Fabrics
| | `-- Frames
| `-- Database
|-- PrimoInternationalP4
| |-- 703
| | |-- Photos
| | |-- Templates
| | `-- models
| | |-- Fabrics
| | `-- Frames
| `-- Database
|-- PrivilegeInternationalP4
| |-- 704
| | |-- Photos
| | |-- Templates
| | `-- models
| | |-- Fabrics
| | `-- Frames
| `-- Database

Each folder name that ends in "P4" is a top level folder. Inside of of
those there will always be two folders; one called Database and the
other will be a 3 digit number, such as 702, 703, or 704 in this case.

The mission:
I need to write the name of the number folder to a file. The filename
must be PREVUE2.CFG. The file must be located inside the folder by
which it got it's name.

Mr. Shawn H. Corey

2007-07-12, 9:58 pm

sintral@gmail.com wrote:
> The mission:
> I need to write the name of the number folder to a file. The filename
> must be PREVUE2.CFG. The file must be located inside the folder by
> which it got it's name.
>
>


Use File::Find to find the directories, then write their names in the
appropriate file.

perldoc File::Find

--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by
doing them."
Aristotle
John W. Krahn

2007-07-12, 9:59 pm

sintral@gmail.com wrote:
> Here's a snippet of my tree:
>
> |-- PresidentialSeatingP4
> | |-- 702
> | | |-- Photos
> | | |-- Templates
> | | `-- models
> | | |-- Fabrics
> | | `-- Frames
> | `-- Database
> |-- PrimoInternationalP4
> | |-- 703
> | | |-- Photos
> | | |-- Templates
> | | `-- models
> | | |-- Fabrics
> | | `-- Frames
> | `-- Database
> |-- PrivilegeInternationalP4
> | |-- 704
> | | |-- Photos
> | | |-- Templates
> | | `-- models
> | | |-- Fabrics
> | | `-- Frames
> | `-- Database
>
> Each folder name that ends in "P4" is a top level folder. Inside of of
> those there will always be two folders; one called Database and the
> other will be a 3 digit number, such as 702, 703, or 704 in this case.
>
> The mission:
> I need to write the name of the number folder to a file. The filename
> must be PREVUE2.CFG. The file must be located inside the folder by
> which it got it's name.



for my $path ( <*P4/[0-9][0-9][0-9]> ) {
open my $fh, '>', "$path/PREVUE2.CFG"
or die "Cannot open '$path/PREVUE2.CFG' $!";
print $fh $path;
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sintral@Gmail.Com

2007-07-12, 9:59 pm

On Jul 12, 12:59 pm, kra...@telus.net (John W. Krahn) wrote:
> sint...@gmail.com wrote:
>
>
>
>
> for my $path ( <*P4/[0-9][0-9][0-9]> ) {
> open my $fh, '>', "$path/PREVUE2.CFG"
> or die "Cannot open '$path/PREVUE2.CFG' $!";
> print $fh $path;
> }
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall


Hey John,
Thanks for the reply. I'm getting this error when I execute that
code:
../perl.pl: line 1: syntax error near unexpected token `$path'
../perl.pl: line 1: `for my $path ( <*P4/[0-9][0-9][0-9]> ) {'

Any suggestions?

John W. Krahn

2007-07-12, 9:59 pm

sintral@gmail.com wrote:
>
> On Jul 12, 12:59 pm, kra...@telus.net (John W. Krahn) wrote:
>
> Hey John,
> Thanks for the reply. I'm getting this error when I execute that
> code:
> ./perl.pl: line 1: syntax error near unexpected token `$path'
> ./perl.pl: line 1: `for my $path ( <*P4/[0-9][0-9][0-9]> ) {'
>
> Any suggestions?


$ bash -c '
for my $path ( <*P4/[0-9][0-9][0-9]> ) {
open my $fh, ">", "$path/PREVUE2.CFG" or die "Cannot open
$path/PREVUE2.CFG $!";
print $fh $path;
}
'
bash: -c: line 1: syntax error near unexpected token `$path'
bash: -c: line 1: `for my $path ( <*P4/[0-9][0-9][0-9]> ) {'


You are trying to get the shell to run your Perl script. You need to use perl
instead!

The first three lines of 'perl.pl' *should* be:

#!/usr/bin/perl
use warnings;
use strict;


Assuming of course that your perl interpreter is located at /usr/bin/perl.




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com