Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating directories within directories

Status
Not open for further replies.

DarkJedi

IS-IT--Management
Dec 13, 2001
10
GB
Hey guys, I'm trying to figure out why mkdir isn't working for me. I've got an HTML form sending info to my cgi program (written in perl) like this


Html part of the form is

<INPUT TYPE=text NAME=filedir>


the Cgi goes something like this

my $basedir = &quot;c:/test&quot;

use CGI;
my $reg = new CGI;
my $filedir = $reg->param(&quot;filedir&quot;);

mkdir (&quot;$basedir/$filedir&quot;,0777);


and so on and so on.

My problem is, if in the text box I write something, I get the new directory like I'm supposed to. BUT... If i write

newdir/anotherdir

or

newdir\anotherdir

it doesnt work at all. it wont even create the first directory, let alond the directory withing that directory.

Any ideas anyone???
 
I expect the Perl implementation of 'mkdir' is similar to the UNIX util. If that is the case, then you will not be able to create nested directories. You'll need to do them one at a time. There is probably a module to simplify this. Maybe someone knows which one???? Sans a module, simply split the path on the '/' or '\' and loop for each new dir level.
 
How do I set that up boating? I'm not exactly.... umm.... brilliant in perl. =)
 
Change the first line to point at your Perl installation. This was hastily put together, but it runs and gives a decent starting chunk.

Code:
#!/usr/local/bin/perl/
$newPath = 'some/new/path/';
@pathParts = split(/\//,$newPath);
foreach $part (@pathParts)
{
$makeThis .= $part.'/'; # add next dir to the mkdir argument
unless (mkdir($makeThis,0777)) { print &quot;Failed to make $makeThis\n&quot;; }
}

'hope this helps......
 
This one is even faster:
using &quot;mkdir -p&quot;, you might be able to create any level
of child dirs.

$dst = &quot;/my/path/to/the/files&quot;;

($_ = `/bin/mkdir -p $dst 2>&1`) or $err = 0;
 
I also found this one (standard ActivePerl) [wink]:
[tt]
use File::path;
mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
[/tt]
AD AUGUSTA PER ANGUSTA

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top