By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,777 Members | 1,755 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,777 IT Pros & Developers. It's quick & easy.

Can i create a subdirectory using mkdir command?

P: 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
Jul 2 '08 #1
Share this Question
Share on Google+
3 Replies


rickumali
P: 20
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.
Jul 3 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 3 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
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.
Jul 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.