View Full Version : Changing directories

08-13-2002, 02:32 PM
Okay, hopefully there is a simple answer to this...there usually is, right? I am saving data as .csv files (comma-separated) to be opened later in Excel. That part works. But when I change directories using chdir(), it DOES NOT work if I use "C:\". However, it DOES work if I use another drive that I am mapped to, such as "S:\". Why is that, and how can I fix it? Btw, when chdir() doesn't work, it still saves to the cgi-bin directory. Here's the test code...very short:

use CGI qw( :standard );


print "Opening file for output:\n";
open (FILE, ">file.csv") or die ("Can't find file.csv : $!");
print "Outputting to file.\n";
print (FILE "This,is,a,comma\nseparated,file\n");
close FILE or die "Can not close file.csv : $!";

08-13-2002, 11:26 PM
You don't really need to change the working directory here, perhaps you could try something like this instead....

use CGI qw( :standard );

$file = "C:/Intranet/file.csv";

print header; # It shouldn't work without this..
print "Opening file for output:\n";
open (FILE, ">$file") or die ("Can't find $file: $!");
print "Outputting to file.\n";
print FILE "This,is,a,comma\nseparated,file\n";
close FILE or die "Can not close $file: $!";

By the way I noticed you were using single backslashes for the path. Since in Perl a single backslash means it should be expecting a metacharacter, it won't interpret it correctly. In a Windows environment you have two options for file separators. You can either do a double backslash or a single forward slash - i.e. *NIX style. I go for the second since it's more portable..

Anyway, hope I helped a bit.

08-14-2002, 01:57 PM
You’re a genius! Thank you, thank you. And that's exactly what I needed anyway, because I would like the user to be able to specify where they want the file to be placed.