Thursday, September 8, 2011

Obtaining TEMP or TMP env variable on Windows using Perl

I had this problem where I needed to obtain certain log files from the temporary folder of Windows/Linux system in my perl automation script.
On Windows OS using command line we can find out this directory using %temp% command. However, to obtain the same in perl you need to make use of the system variable ENV. I made use of the following snippet to transfer this value and consume it in my code.

sub getSystemTempPath
{
my ($os_name , $os_arch);

$os_name = "$Config{osname}";
$os_arch = "$Config{archname}";

if ($os_name =~ m/MSWin32/)
{
foreach $key (keys(%ENV))
{

#printf("%-10.10s: $ENV{$key}\n", $key);
if ($key =~ m/TEMP/)
{
$retval=$ENV{$key};
return($retval);
}
}
}
else
{
return("/tmp");
}
}

Labels: , , , ,