I tried alot but couldnt able to do that please help me to do that

What you didn't try to do is describing your code to yourself; I'll do that for you:

You create VEHICLES and change into this empty directory. Fine. Inside this directory you create a directory and change into this empty dirctory.

Then, inside this subdirectory, you try to chdir into the directory VEHICLES (which is what the variable $dir holds). This subdirectory doesn't exist in VEHICLES/CARS. Bummer. This part of your code silently fails. Your current working directory stays the same (VEHICLES/CARS).

You iterate to the next element in @files.
You create the next directory, BIKES, and change directory into that. Again, you try to chdir into VEHICLES which doesn't exist.

You end up with

VEHICLES/CARS/BIKES

which is your current working directory after the loop ends.

First thing you should do is checking all your operations for success:

$dir = "VEHICLES"; @files = ('CARS', 'BIKES'); mkdir ($dir) or die "can't mkdir $dir: $!"; chdir ($dir) or die "can't chdir to $dir: '$!'"; foreach $file(@files) { mkdir $file or die "(loop) can't mkdir $file: '$!'"; chdir $file or die "(loop) can't chdir to $file: '$!'"; chdir $dir or die "(loop) can't chdir to $dir: '$!'"; }

Output:

(loop) can't chdir to VEHICLES: 'No such file or directory' at try.pl +line 10.

Aha! there's no such file or directory inside the loop, when you try to chdir to VEHICLES. You realize the two chdir statements inside the loop are bogus and eliminate them:

$dir = "VEHICLES"; @files = ('CARS', 'BIKES'); mkdir ($dir) or die "can't mkdir $dir: $!"; chdir ($dir) or die "can't chdir to $dir: '$!'"; foreach $file(@files) { mkdir $file or die "(loop) can't mkdir $file: '$!'"; }

Then you run your file again. Output now:

can't mkdir VEHICLES: File exists at try.pl line 3.

So, you should check for existence before making a directory. See -X for the various file testing operators.

$dir = "VEHICLES"; @files = ('CARS', 'BIKES'); if(not -d $dir) { mkdir ($dir) or die "can't mkdir $dir: $!"; } chdir ($dir) or die "can't chdir to $dir: '$!'"; foreach $file(@files) { if (not -d $file) { mkdir $file or die "(loop) can't mkdir $file: '$!'"; } }

Now your code runs and produces the expected structure:

VEHICLES VEHICLES/BIKES VEHICLES/CARS

See chdir, mkdir, not, -X, or, die for more information about the added bits.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Directory Structure. by shmem
in thread Directory Structure. by Nansh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.