read path from a file

Discussion in 'Perl Misc' started by keepyourstupidspam@yahoo.co.uk, Apr 6, 2007.

  1. 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
     
    , Apr 6, 2007
    #1
    1. Advertising

  2. Guest

    On Apr 6, 1:28 pm, Purl Gurl <> wrote:
    > keepyourstupidspam wrote:
    > > 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

    >
    > Your example is not a path. I count seven paths.
    >
    > > Here is my code that reads the Path from the file
    > > foreach my $line (@lines) {

    >
    > > if ($line =~ /PATH=/) {
    > > $path = substr($line,5);

    >
    > 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
     
    , Apr 6, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 6, 1:48 pm, wrote:
    > On Apr 6, 1:28 pm, Purl Gurl <> wrote:
    >
    >
    >
    >
    >
    > > keepyourstupidspam wrote:
    > > > 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

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

    >
    > > > Here is my code that reads the Path from the file
    > > > foreach my $line (@lines) {

    >
    > > > if ($line =~ /PATH=/) {
    > > > $path = substr($line,5);

    >
    > > 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- 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
     
    , Apr 6, 2007
    #3
  4. J. Gleixner Guest

    wrote:

    > 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 ( <FILE> ) {
    >
    > 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;
    }

    >
    > }
    > close FILE;
     
    J. Gleixner, Apr 6, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yang Xiao
    Replies:
    4
    Views:
    38,517
    czahrobsky
    May 30, 2010
  2. Replies:
    1
    Views:
    587
    Oliver Wong
    Jul 13, 2006
  3. Sreejith K
    Replies:
    24
    Views:
    1,024
    Sreejith K
    Mar 24, 2009
  4. \A_Michigan_User\

    Various path/path/files.asp all #include this 1 file

    \A_Michigan_User\, Jul 10, 2005, in forum: ASP General
    Replies:
    1
    Views:
    231
    Jeff Cochran
    Jul 10, 2005
  5. Alex Dowad
    Replies:
    4
    Views:
    272
    Michel Demazure
    May 1, 2010
Loading...

Share This Page