How Should I Check for Directory existance in a server

S

sunilkumar_1480

I have to create a directory in a FTP server but before I create i
should check for the existance of the directory. If the directory does
exist then I should not create the directory. The following is the
code which I have written and I am unsuccessful with reading a
directory in a server

my $dir_name = $ARGV[0];
my $basepath = "c:\\Inetpub\\wwwroot\\$dir_name\\test";
my $count = 0;
my $server="casr.com";
my $user="slb";
my $password="bigfiles";
my $dir="iDistrict/incoming/baselines/TestReports/";
my $success="true";
my $filetran="true";
my $reason;
my $test="true";
my @file_list;
my $ret;
my $ftp=Net::FTP->new($server) or $success="false";
$ftp->login($user, $password) or $success="false";
$ftp->pasv() or $success="false";
$ftp->binary() or $success="false";
$ftp->cwd($dir) or $success='false';
print $ftp->cwd($dir);
# I am unable to open the directory that exists in the server which I
have
# mentioned with $dir name.
opendir(SRC, $dir);
my @dir_files = readdir(SRC);
foreach my $file (@dir_files)
{
if($file eq $dir_name)
{
$ret = 0;
closedir(SRC);
last;
}
}

if($ret ne 0)
{
$ftp->mkdir($folder_name) or $success='false';
}
$ftp->cwd($dir."/".$folder_name) or $success='false';


Can anybody help me how to read the contents of a directory in the
server.
 
B

Bill

I have to create a directory in a FTP server but before I create i
should check for the existance of the directory. If the directory does
exist then I should not create the directory. The following is the
code which I have written and I am unsuccessful with reading a
directory in a server

my $dir_name = $ARGV[0];
my $basepath = "c:\\Inetpub\\wwwroot\\$dir_name\\test";
my $count = 0;
my $server="casr.com";
my $user="slb";
my $password="bigfiles";
my $dir="iDistrict/incoming/baselines/TestReports/";
my $success="true";
my $filetran="true";
my $reason;
my $test="true";
my @file_list;
my $ret;
my $ftp=Net::FTP->new($server) or $success="false";
$ftp->login($user, $password) or $success="false";
$ftp->pasv() or $success="false";
$ftp->binary() or $success="false";
$ftp->cwd($dir) or $success='false';
print $ftp->cwd($dir);
# I am unable to open the directory that exists in the server which I
have
# mentioned with $dir name.
opendir(SRC, $dir);
my @dir_files = readdir(SRC);
foreach my $file (@dir_files)
{
if($file eq $dir_name)
{
$ret = 0;
closedir(SRC);
last;
}
}

if($ret ne 0)
{
$ftp->mkdir($folder_name) or $success='false';
}
$ftp->cwd($dir."/".$folder_name) or $success='false';


Can anybody help me how to read the contents of a directory in the
server.

I think you perhaps are confusing local directories woth the Net::FTP
directories. Read the docs on Net::FTP, and try

my @fnames = $ftp->ls($dir) or (maybe) my @fnames = $ftp->ls;
 
A

A. Sinan Unur

I am no expert, but that smells of a race condition to me. The directory
may be created by another process between your check and attempt to
create it. Why not just try to create it? If creation fails, assume the
directory exists and try changing to that directory. If that fails,
abort.

use strict;
use warnings;
my $dir_name = $ARGV[0];

Check if the directory name is acceptable.

Don't declare all variables at the top of you script like this. Use a
hash for configuration variables:

my %config = (
server => 'casr.com',
user => 'slb',
pass => 'bigfiles',
dir => 'iDistrict/incoming/baselines/TestReports/',
);

Why do you keep on going if the constructor failed? Did you read the
documentation for Net::FTP?

$ftp = Net::FTP->new($config{server})
or die "Cannot connect to $config{server}: $@";

Why do you keep on going if you cannot login? Did you read the
documentation for Net::FTP?

$ftp->login($config{user}, $config{pass})
or die "Cannot login ", $ftp->message;

See above.

See above.

See above.

Why do you repeat the ftp->cwd?
WTF?

Huh?


I think you perhaps are confusing local directories woth the Net::FTP
directories.

I think Bill is right.
Read the docs on Net::FTP

Ditto.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top