read path from a file

G

Guest

Hi, I am reading a path from a file and want to set $ENV{'PATH'} with
this path. Then I call a command which will use this path, this
command happens to be gmake.exe but it fails.

Here is the path in the file:


PATH=C:\Program Files\Microsoft Visual Studio 8\VC\bin;C:\Program Files
\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft
Visual Studio 8\Common7\Tools\Bin;C:\Program Files\Microsoft Visual
Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio 8\VC
\PlatformSDK\Bin;C:\bin\Perl\bin;C:\WINDOWS\system32



Here is my code that reads the Path from the file


foreach my $line (@lines) {

if ($line =~ /PATH=/) {
$path = substr($line,5);
$ENV{'PATH'} = "$path";
print "$path\n";
print "$ENV{'PATH'}"
}

}


But gmake.exe will fail, It will not fail If I hardcode the path in
the perl file but I need to read it from a file instead.

Anyone know what I am doing wrong?

Enda
 
G

Guest

Your example is not a path. I count seven paths.



Your code does not open and read a file. Your code loops
through an array of unknown content.

Purl Gurl


Ok, I was leaving that to the imagination here is all the code..


my $pathFile="paths.dat";

open FILE, "<$pathFile" or die "ERROR $!";
my @lines = <FILE>;


foreach my $line (@lines) {

if ($line =~ /PATH=/) {
$path = substr($line,5);
$ENV{'PATH'} = "$path";
print "$path\n";
print "$ENV{'PATH'}"
}

}
close FILE;


Thanks,
Enda
 
G

Guest

Ok, I was leaving that to the imagination here is all the code..

my $pathFile="paths.dat";

open FILE, "<$pathFile" or die "ERROR $!";
my @lines = <FILE>;

foreach my $line (@lines) {

if ($line =~ /PATH=/) {
$path = substr($line,5);
$ENV{'PATH'} = "$path";
print "$path\n";
print "$ENV{'PATH'}"
}

}

close FILE;

Thanks,
Enda- Hide quoted text -

- Show quoted text -

I solved it by using chop to remove the last character from the string
I read from the file:

my $pathFile="paths.dat";

open FILE, "<$pathFile" or die "ERROR $!";
my @lines = <FILE>;


foreach my $line (@lines) {

if ($line =~ /PATH=/) {
$path = substr($line,5);
chop($path);
$ENV{'PATH'} = "$path";
print "$path\n";
print "$ENV{'PATH'}"
}

}
close FILE;



Bye,
Enda
 
J

J. Gleixner

I solved it by using chop to remove the last character from the string
I read from the file:

perldoc -f chomp
my $pathFile="paths.dat";

open FILE, "<$pathFile" or die "ERROR $!";
my @lines = <FILE>;


foreach my $line (@lines) {

If all you're doing is looking for that line, then there's no need to
read the entire file into @lines;

for my $line ( said:
if ($line =~ /PATH=/) {
$path = substr($line,5);
chop($path);
$ENV{'PATH'} = "$path";
print "$path\n";
print "$ENV{'PATH'}"
}

A bit shorter...
if( $line =~ /^PATH=(.*)/ {
$ENV{'PATH'} = $1;
print $ENV{'PATH'};
last;
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top