Directory change and system

Discussion in 'Perl Misc' started by Bill H, Aug 12, 2007.

  1. Bill H

    Bill H Guest

    I am using system in script to run ImageMagick top convert a file to a
    different format, but for it to work I have to be in the directory
    that the file is in before doing the system. What would be the best
    way of saving the current directory, change to the new one, doing my
    system and then going back to the old one?

    Bill H
     
    Bill H, Aug 12, 2007
    #1
    1. Advertising

  2. Bill H

    Paul Lalli Guest

    On Aug 12, 12:48 pm, Bill H <> wrote:
    > I am using system in script to run ImageMagick top convert a file to a
    > different format, but for it to work I have to be in the directory
    > that the file is in before doing the system. What would be the best
    > way of saving the current directory, change to the new one, doing my
    > system and then going back to the old one?


    use Cwd;
    my $old_dir = getcwd();
    chdir $new_dir or die "Cannot change to $new_dir: $!";
    system($IM_cmd);
    chdir $old_dir or die "Cannot change back to $old_dir: $!";

    Paul Lalli
     
    Paul Lalli, Aug 12, 2007
    #2
    1. Advertising

  3. Bill H

    Guest

    Bill H <> wrote:
    > I am using system in script to run ImageMagick top convert a file to a
    > different format, but for it to work I have to be in the directory
    > that the file is in before doing the system.


    I haven't used ImageMagick, but that seems like an odd requirement for it
    to make. Are you sure there isn't another way to accomplish the task?

    > What would be the best
    > way of saving the current directory, change to the new one, doing my
    > system and then going back to the old one?


    If you can't figure out how to overcome the need to chdir at all,
    then I'd just add a "cd /whatever/dir;" to the front of the string
    you are passing to system (assuming you are using the single-argument
    form of system). That way you don't have to worry about changing back,
    plus if you ever add threads to your code you don't have to worry
    about the chdir complications.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 12, 2007
    #3
  4. Bill H wrote:
    > I am using system in script to run ImageMagick


    Why don't you use the Perl module Image::Magick as the interface to the
    IM library? Then I guess you wouldn't have that chdir issue.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 12, 2007
    #4
  5. Bill H

    Bill H Guest

    On Aug 12, 6:39 pm, Gunnar Hjalmarsson <> wrote:
    > Bill H wrote:
    > > I am using system in script to run ImageMagick

    >
    > Why don't you use the Perl module Image::Magick as the interface to the
    > IM library? Then I guess you wouldn't have that chdir issue.
    >
    > --
    > Gunnar Hjalmarsson
    > Email:http://www.gunnar.cc/cgi-bin/contact.pl


    Gunnar

    I looked at that and will try that again, but it wasnt working right,
    and for what I need to do, simple conversions from one format to
    another, it seems much easier (and probably faster) just to use a
    system and use convert.

    Bill H
     
    Bill H, Aug 13, 2007
    #5
  6. >>>>> "Bill" == Bill H <> writes:

    Bill> I am using system in script to run ImageMagick top convert a file to a
    Bill> different format, but for it to work I have to be in the directory
    Bill> that the file is in before doing the system. What would be the best
    Bill> way of saving the current directory, change to the new one, doing my
    Bill> system and then going back to the old one?

    Keep in mind that system("date") is essentially:

    defined (my $kid = fork) or die "Cannot fork: $!";
    unless ($kid) {
    # put your chdir here
    exec "date";
    die "date not found"; # just like me last friday night
    }
    waitpid($kid, 0);

    So add your chdir where it says. It affects only the kid.

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Randal L. Schwartz, Aug 13, 2007
    #6
    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. TJS
    Replies:
    1
    Views:
    547
    Eliyahu Goldin
    Jun 23, 2004
  2. =?Utf-8?B?QmFzIEhlbmRyaWtz?=

    Directory and files names in temporary asp.net directory

    =?Utf-8?B?QmFzIEhlbmRyaWtz?=, Jan 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    894
    Juan T. Llibre
    Jan 31, 2005
  3. Irmen de Jong
    Replies:
    2
    Views:
    449
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 12, 2003
  4. Nathan Sokalski
    Replies:
    2
    Views:
    1,157
  5. codeboy
    Replies:
    1
    Views:
    1,596
    Juan T. Llibre
    Jun 8, 2009
Loading...

Share This Page