Directory change and system
Directory change and system
am 12.08.2007 18:48:21 von 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
Re: Directory change and system
am 12.08.2007 18:56:56 von Paul Lalli
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
Re: Directory change and system
am 12.08.2007 23:47:05 von xhoster
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
Re: Directory change and system
am 13.08.2007 00:39:34 von Gunnar Hjalmarsson
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
Re: Directory change and system
am 13.08.2007 03:12:55 von Bill H
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
Re: Directory change and system
am 13.08.2007 05:38:20 von merlyn
>>>>> "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
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