Forum Moderators: coopster & phranque

Message Too Old, No Replies

chdir not working on windows 2000 Pro

chdir fails

         

raybandude

4:37 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



I have some test code below that I run from my Explorer as follows:

[localhost...]

It runs through but instead of showing the folder that it changed to as p:\edi\projects\Notifications\energy_data, it shows the result as C:/Server/Apache2/cgi-bin. In other words, I can't get chdir to work. If I run this from DOS, it works fine.

It does print "Hello World" so I know it finds perl and can run it.

What am I doing wrong? Why will chdir not working when I run it from the browser?

Appreciate any replies.

Thanks.
=================================================
#!/perl/bin/perl.exe -w
use File::Spec::Functions;
use lib "p:/edi/projects/Notifications/energy_data"; # LL,06OCT05.
use Cwd;
chdir('p:\edi\projects\Notifications\energy_data' or die "Cannot cd to new directory: $!\n");
use lib "p:/edi/projects/perlargus/hubdcs";
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Template;
use changes_utils;
use constant MAX_NEW_LINES => 10;
use constant NEW_LINES_BEGIN_AT => 1000000;
use constant TEMPLATE_FILE => 'changes_editor.html';

chdir('p:\edi\projects\Notifications\energy_data' or die "Cannot cd to new directory: $!\n");
my $dir = getcwd();
print "Content-type: text/html\n\n";
print "<h2>Hello, World!</h2>\n";
print "$dir\n"

bennymack

5:22 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



Change:
chdir('p:\edi\projects\Notifications\energy_data' or die "Cannot cd to new directory: $!\n");

to

chdir('p:\edi\projects\Notifications\energy_data') or die ("Cannot cd to new directory: $!\n");

raybandude

6:56 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



Thanks for that tip... but, it still won't work. It seems like it will not chdir to a folder on the network... but, it will chdir to another folder on my c: drive.

I wonder why that is? If I run the software through dos, it changes to a folder on the network without a problem.

Very strange... any other ideas? Anyone?

rocknbil

5:10 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, are you getting the output from the die statement, and what's in $!? If you are, maybe you don't have permissions to change to that directory.

Basically win2k/nt or all other forms of windblows servers pose all sorts of difficulties. I don't know what the problem is, but a way around it is to dispense with the tools that work everywhere else and use something that will work on your server. Unless you have a **specific** need to change directories, from what I can tell from the last few lines of your script,

chdir('p:\edi\projects\Notifications\energy_data' or die "Cannot cd to new directory: $!\n");
my $dir = getcwd();
print "Content-type: text/html\n\n";
print "<h2>Hello, World!</h2>\n";
print "$dir\n"

Can be emulated with (and I know this works on 2k/NT:)

$mypath = 'p:\edi\projects\Notifications\energy_data';

## Note the call to the little error sub below. This will easily print the error stored in $! to the browser.

opendir(DIR, $mypath) or &error("Cannot read directory $mypath: $!\n");
@listarray = readdir(DIR);
close (DIR);

## Some would suggest using join here. But this
## dispenses with non-file data, such as ".", "..",
## or folders, returns only files.

foreach $f (@listarray) {
if (-f "$mypath\\$f") { $dir .= "$f <br>\n"; }
}

print "Content-type: text/html\n\n";
print "<h2>Hello, World!</h2>\n";
print "$dir\n";

## Many prefer Carps's fatals to browser, this works
## as well for most purposes and is more terse:
sub error {
my $err = shift(@_);
print "content-type: text/html\n\n";
print "An error has occurred: $err.";
exit 0;
}

If you try that and STILL get the error, it's something else, most likely permissions as I said.