Connecting Tech Pros Worldwide Help | Site Map

Can i create a subdirectory using mkdir command?

  #1  
Old July 3rd, 2008, 12:25 AM
Newbie
 
Join Date: Jul 2008
Posts: 12
i have made a directory c:/abcd
now i want to gather a list of sub directories from 1 directory and create the same list here.
i am able to print the list of sub directories,
but mkdir $dest/$_;
is not creating the subdirectory.
Please help
  #2  
Old July 3rd, 2008, 03:56 AM
rickumali's Avatar
Newbie
 
Join Date: Dec 2006
Location: Arlington, MA, USA
Posts: 20

re: Can i create a subdirectory using mkdir command?


The short answer is yes, you can create a subdirectory using Perl's mkdir command. Below is a small program that reads the STDIN for a string, and then appends that string to an existing directory. This new directory is passed to the mkdir() command.

Expand|Select|Wrap|Line Numbers
  1. $dest = "C:\\cygwin";
  2. while (<STDIN>) {
  3.    print "Making $dest\\$_\n";
  4.    chop;
  5.    mkdir "$dest\\$_";
  6. }
  7.  
Two things I noticed while coding up this example: 1) Even though I'm on Windows, I was able to run this code using "/" as the path separator, instead of "\" (which has to be escaped with another "\"). I leave this an exercise for you. 2) The chop() is necessary to remove the trailing newline from my input string. I noticed in the original post the presence of $_, and this could be an issue for you. If in doubt, print the "$_" value using print "[$_]", so you can see if your variable has leading or trailing characters.

Good luck.
  #3  
Old July 3rd, 2008, 04:58 AM
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,543

re: Can i create a subdirectory using mkdir command?


Quote:
Originally Posted by rickumali
2) The chop() is necessary to remove the trailing newline from my input string. I noticed in the original post the presence of $_, and this could be an issue for you. If in doubt, print the "$_" value using print "[$_]", so you can see if your variable has leading or trailing characters..
Just a note, to clarify. chop() removes the last character in a string, regardless of what it is, and returns it. If what you are trying to do is remove the newline, then what you want to do is use chomp() which is far less invasive and ONLY removes what is defined in the special variable $/, which contains the current INPUT_RECORD_SEPARATOR.

Regards,

Jeff
  #4  
Old July 3rd, 2008, 07:45 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,097

re: Can i create a subdirectory using mkdir command?


Quote:
Originally Posted by supriyamk
i have made a directory c:/abcd
now i want to gather a list of sub directories from 1 directory and create the same list here.
i am able to print the list of sub directories,
but mkdir $dest/$_;
is not creating the subdirectory.
Please help
Post your perl code. Make sure to use the code tags when posting code.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling perl 5.8.7 on Solaris 8 Kirt Loki Dankmyer answers 0 November 22nd, 2005 04:05 AM