Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Directory change and system

Reply
Thread Tools

Directory change and system

 
 
Bill H
Guest
Posts: n/a
 
      08-12-2007
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-12-2007
On Aug 12, 12:48 pm, Bill H <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-12-2007
Bill H <(E-Mail Removed)> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-12-2007
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
 
Reply With Quote
 
Bill H
Guest
Posts: n/a
 
      08-13-2007
On Aug 12, 6:39 pm, Gunnar Hjalmarsson <(E-Mail Removed)> 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

 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      08-13-2007
>>>>> "Bill" == Bill H <(E-Mail Removed)> 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
<(E-Mail Removed)> <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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.Delete Throws "the directory is not empty" codeboy ASP .Net 1 06-08-2009 12:08 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
System.Security.SecurityException: Error de solicitud de permiso de tipo System.Net.WebPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. Luis Esteban Valencia ASP .Net 0 07-14-2005 01:43 PM
os.system('cd dir1 ... and executing next os.system command in that directory (dir1) Irmen de Jong Python 2 08-12-2003 12:43 PM



Advertisments