Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 3rd, 2008, 12:25 AM
Newbie
 
Join Date: Jul 2008
Posts: 12
Default Can i create a subdirectory using mkdir command?

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
Reply
  #2  
Old July 3rd, 2008, 03:56 AM
rickumali's Avatar
Newbie
 
Join Date: Dec 2006
Location: Arlington, MA, USA
Age: 40
Posts: 20
Default

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.
Reply
  #3  
Old July 3rd, 2008, 04:58 AM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

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
Reply
  #4  
Old July 3rd, 2008, 07:45 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

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
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles