On Thu, 21 Nov 2002, Tim Booher wrote:

> I don't know why I am having such a tough time creating a directory.
>  
> I have the following code, but for some reason I can't create a
> directory:
>  
> #!/usr/bin/perl -w
> # Microsoft Windows Win32
>  
> use strict;
> use File::Find;

The find function of File::Find should be used when you want to 
traverse the entire directory structure. From the example output you have 
given it seems like you only want the current directory's listing.
You can use the glob operator instead
perldoc -f glob
perldoc File::Glob

>  
> my $i;
> my $cs;
> my @myArray;      # array to hold matches
> my $Month;        # variable to hold month value (i.e. '2002_10')
> my $NewDirName;   # variable to hold the string to point to the new
> directory location
>  
> find(\&Display,'.');
>  
> sub Display {
>             # please let me know if there is a better way to do this
>             # I am just trying to extract the 'month' out of the file
> name
>             # I think I am doing extra work . . .
>             @myArray = ($File::Find::dir =~ /^\.\/(\d{4}_\d{2})/);
>             $Month = $1;

Use variables like $1, $2 only inside a condition check like this
if ($File::Find::dir =~ /.../) {
  $Month = $1;
}

These variables are not cleared, if the current pattern match fails they 
will still contain values from the previous capture.

>             $NewDirName = "/imgs/" . $Month . "/";
>             if (!(-e $NewDirName)) {                              # make
> a new directory
>                   chdir "D:\\";                                   # go
> to D:
>                   mkdir $NewDirName;                              # make
> the directory

Why chdir to "D:\\" to create the directory, why not call it like this
mkdir ("D:\\$NewDirName") instead.

>                   print "created " . $NewDirName . "\n";          #
> report success

You can print that only if mkdir succeeds. Check the return value of mkdir
perldoc -f mkdir

>             }
> }
>  
> My Directory structure is like:
> 07/13/2002  09:53 AM    <DIR>          1999_07
> 07/13/2002  09:53 AM    <DIR>          1999_08
> 07/13/2002  09:53 AM    <DIR>          1999_09
> 07/13/2002  09:53 AM    <DIR>          1999_10
> 07/13/2002  09:53 AM    <DIR>          1999_12
> 07/13/2002  09:53 AM    <DIR>          2000_01
> .. . .
>  
> One of the errors I keep getting is:
> >Can't cd to (./) 2001_10 : No such file or directory
>  
> any ideas would be really appreciated . . .
>  
> tim


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to