CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Creating directories and files????

 



Vincent Jansen
Deleted

Mar 3, 2000, 1:45 AM

Post #1 of 9 (1708 views)
Creating directories and files???? Can't Post

Hi,

Can anybody help me creating new directories and files?

Thanks
Vincent


darian
Deleted

Mar 3, 2000, 1:55 AM

Post #2 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

mkdir 'mydir', 0777

will make a directory called mydir in the current directory and change the rights to world writable

open(FILE, ">myfile.txt") or die ("Can't create myfile.txt");
print FILE "My Text.\n";
close(FILE);

this will create a file called myfile.txt in the current directory and place the line:

My Text

on a line.


Vincent Jansen
Deleted

Mar 3, 2000, 4:54 AM

Post #3 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

it seems rather easy.
Thanks very much


Cretep
User

Mar 3, 2000, 6:37 AM

Post #4 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

That's perl for you.


Cure
User

Mar 3, 2000, 11:02 AM

Post #5 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

Hi Vincent

To Create a Directory and change its permissions at the same time then do the following::

$dir = "directory";
mkdir $dir,"0777";
chmod(0777, "$dir");

To create a file in a current working directory do the following.

open (DB, ">myfile.txt") | | die "Can't create myfile.txt. $!\n";
close(DB);

Cure

[This message has been edited by Cure (edited 03-03-2000).]


japhy
Enthusiast

Mar 5, 2000, 6:55 PM

Post #6 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

There's no need to put $dir in quotes if it's the ONLY thing in quotes. And putting an octal number in quotes is A Bad Thing. And you don't need to chmod() a directory after you mkdir() it. And you should check the return calls of mkdir(), chmod(), etc.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$dir = "whatever";
# DO ONE OF THE FOLLOWING
mkdir $dir, 0777;
# or
mkdir $dir, "777";
# or
mkdir $dir, oct("0777");
</pre><HR></BLOCKQUOTE>

Of course, after mkdir(), you should check $!:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


mkdir $dir, $mode or die "can't mkdir $dir with permissions $mode: $!";
</pre><HR></BLOCKQUOTE>


Cure
User

Mar 5, 2000, 7:22 PM

Post #7 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

Hi japhy

If a user wants to create a directory annd set its permssions at the same time
then i had to do this in order to accompilsh that I do the following->

$dir = "directory";
mkdir $dir,0777;
chmod(0777, $dir);

when I used this as you suggested:
$dir = "whatever";
mkdir $dir, 0777;

it creates a directory but SETS ITS PERMISSION to 755 not 777


and when i do this:
$dir = "whatever";
mkdir $dir, "777";
it creates a directory but SET ITS PERMISSION TO 411 NOT 777

I know its strange but thats what it does for me.


Cure



japhy
Enthusiast

Mar 6, 2000, 6:15 AM

Post #8 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

Oops, I'm sorry, I forgot the oct() in the "777" example.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


mkdir $dir, 0777;
mkdir $dir, oct("777");
mkdir $dir, oct("0777");

# BUT DON'T DO

mkdir $dir, oct(0777);
</pre><HR></BLOCKQUOTE>

Perl converts octal and hexadecimal numbers to decimal when it sees them as numbers (not strings).

As for mkdir() not making your directories 0777, that is because your umask() setting is 0022. To get around that, you CAN use chmod() after you've made the directory, or can use Perl's umask() function:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$old = umask 0; # $old is previous umask()
mkdir $dir, 0777;
umask $old;
</pre><HR></BLOCKQUOTE>


Cure
User

Mar 6, 2000, 9:04 AM

Post #9 of 9 (1708 views)
Re: Creating directories and files???? [In reply to] Can't Post

THXS JAPHY


Cure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives