I want to create folder using perl where, in the same folder, a perl script exists. I created FolderCreator.pl which requires an input parameter of folder name
.
unless(chdir($ARGV[0]){ # If the dir available change current , unless
mkdir($ARGV[0], 0700); # Create a directory
chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n"; # Then change or stop
}
This worked fine only if we call the scipt, in the same folder where it resides. If it is called in another folder, if doesn't work.
Eg.
.../Scripts/ScriptContainFolder> perl FolderCreator.pl New
.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl New
First one is working fine but second one doesn't. Is there way create these folders?
If you want to get content of given directory, and only it (i.e. no subdirectories), the best way is to use opendir/readdir/closedir: opendir my $dir, "/some/path" or die "Cannot open directory: $!"; my @files = readdir $dir; closedir $dir; You can also use: my @files = glob( $dir .
If you want to move the directory ./dir1 under ./dir2 call move like this: move("./dir1", "./dir2/dir1");
You can use mkdir function in Perl to create a new directory. You will need to have the required permission to create a directory. #!/usr/bin/perl $dir = "/tmp/perl"; # This creates perl directory in /tmp directory. mkdir($dir) or die "Couldn't create $dir directory, $!"; print "Directory created successfullyn";
Perl | Writing to a File. A filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file. In order to write to the file, it is opened in write mode as shown below: open (FH, ‘>’, “filename.txt”); If the file is existing then it truncates the old content of file with the new content.
Traversing files and directories in Perl can also be done through File::Find module which comes with the Perl language. Find: find () function performs a depth-first search on the mentioned/defined @directories. It calls and invokes the "&wanted" function for each file or sub-directory found in that directory. find () works from top to down.
You can use rmdir function to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it. #!/usr/bin/perl $dir = "/tmp/perl"; # This removes perl directory from /tmp directory.
You can use the FindBin module, which give us the $Bin variable. It locates the full path to the script bin directory to allow the use of paths relative to the bin directory.
use FindBin qw($Bin);
my $folder = "$Bin/$ARGV[0]";
mkdir($folder, 0700) unless(-d $folder );
chdir($folder) or die "can't chdir $folder\n";
I think it works exactly as it is written, except you have a typo, namely missing a closing parenthesis around chdir
.
unless(chdir($ARGV[0])) { #fixed typo
mkdir($ARGV[0], 0700);
chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";
}
The script runs like this:
The starting directory for the script will be the directory it is called from, whatever that directory may be. On *nix that'll be the $ENV{PWD}
variable inside your script. It will make a new folder in any folder it has permission to do so.
I think this behavior is logical, and as it should be. If you want your example to work, do this:
.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl ScriptContainFolder/New
You can also use an absolute path, such as
?> FolderCreator.pl /home/m/me/Scripts/ScriptContainFolder/New
ETA: Oh, and you should of course always, always put this in your scripts, no matter how small:
use strict;
use warnings;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With