Creating new folder in PERL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patelxxx
    New Member
    • May 2007
    • 135

    Creating new folder in PERL

    Can someone guide me how to write a code to open a 'new folders' in PERL from a drop down menu.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Moved to Perl from Member Intros.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by patelxxx
      Can someone guide me how to write a code to open a 'new folders' in PERL from a drop down menu.

      What have you tried so far?

      Comment

      • patelxxx
        New Member
        • May 2007
        • 135

        #4
        The PERL code:

        [CODE=perl]
        use Cwd;
        $path = getcwd();
        $path =~ s/\\/\//sg;
        print "Enter the directory name\n";
        $dir = <STDIN>;
        mkdir($dir);
        $new = $path."\/".$dir;
        opendir(DIR, $new) || die ("cannot open the path");
        mkdir("$new/hai");
        [/CODE]

        I'm trying to create a new folder in a 'Drop Down Menu', would I have to use 'JavaScript' to do this too?
        Last edited by miller; May 24 '07, 04:08 PM. Reason: Code Tag and ReFormatting

        Comment

        • prn
          Recognized Expert Contributor
          • Apr 2007
          • 254

          #5
          Hi patelxxx,

          The code you posted does create a directory ("folder") based on the user's input and then creates another folder inside it called "hai". Is that not what you wanted it to do?

          I should point out one, presumably unintended, effect of your current code: if someone runs this code and answers "foo" for the name of the directory to be created, your code as written includes the newline from <STDIN> in the variable $dir. Assuming you don't want the new directory to be called "foo\n" (displayed on my system and probably many others as "foo?") you should write:
          [code=perl]$dir = <STDIN>;
          chomp $dir;
          mkdir($dir);[/code]
          where chomp removes the newline from the end of $dir.

          Otherwise, what is your question?

          You say you're "trying to create a new folder in a 'Drop Down Menu'," What do you mean by that. If you're trying to create a web page that includes a drop-down menu, then it doesn't make sense to create directories in your local filesystem? If you are trying to include a function in a drop-down menu, then something like your current code works for that, but how you include it in what kind of menu is quite unclear. Why are you asking about JavaScript? JavaScript is used within a web page to control web browser activities and is not really supposed to allow your browser to modify your local filesystem. (That would be a terribly bad security hole.)

          Overall, I get the feeling that you are trying to do something very different from what your posts actually say you are trying to do. This may be a language problem.

          Paul

          Comment

          • patelxxx
            New Member
            • May 2007
            • 135

            #6
            I have a "Dropdown Menu" on a website (not live yet) where users can select their folders to upload files on to, I would like an option for "New Folder" (i.e. add/creat a new folder) and once this option is selected a "new" folder is created. It's this part I assume is done in JavaScript.

            The actual website has actual been written in PERL?

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Maybe this will help get you started:

              Code:
              use CGI;
              use strict;
              use warnings;
              
              my $q = new CGI;
              print $q->header(),$q->start_html();
              my $path = 'path/to/some/folder/';
              $path =~ s|\|/|g;
              my $dir = $q->param('folder');
              #vaildate the value of $dir to make sure the form has not been altered
              $path.=$dir;
              mkdir($path) or die "Unable to create folder: $!";
              mkdir("$path/hai") or die "Unable to create folder: $!";
              I wouldn't use CWD and getcwd() unless you really know what you are doing. Instead hard code the value of the start directory like I did above. The CGI module is for parsing form data which is what I think you want to do, no javascript need be involved:


              Comment

              • patelxxx
                New Member
                • May 2007
                • 135

                #8
                Thank you Kevin for your tips. Will give it a try tomorrow morning, when I'm back at work.

                cheers

                Ravi

                (patelxxx)

                Comment

                • abu mubeen
                  New Member
                  • May 2016
                  • 2

                  #9
                  Hi Mr.prn,

                  I have one small query

                  How to create a Perl script for multiple folders in desktop.

                  Comment

                  Working...