Directory change and system

B

Bill H

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
 
P

Paul Lalli

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
 
X

xhoster

Bill H said:
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
 
G

Gunnar Hjalmarsson

Bill said:
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.
 
B

Bill H

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

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
 
R

Randal L. Schwartz

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top