Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Search and Copy a filepath

by mikasue (Friar)
on Oct 31, 2006 at 21:59 UTC ( #581581=perlquestion: print w/ replies, xml ) Need Help??
mikasue has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks.

I seek your advice and help on the following situation.

I have a program that allows the user to open an image file from a File Open box in Windows. The problem is my program expects the file to be in my \Images folder. If the user opens a file in the c:\windows directory my program fails becuase it is looking for the image in the \Images folder.

So, I can either copy the file over as soon as the user selects it or I can have my program read from any directory instead of just my \Images folder.

Which method is better?

My first problem is, I do not know how to implement either option.

So, I have researched ways to copy the file to my Images folder immediately after the user selects it and found File::Copy. My problem here is, I do not know how to compare the filepaths. I thought maybe s/// but that replaces the pathname. I need to compare them or really search for the substring \MYPROGRAM\IMAGES in the path selected by the user and then if they do not match copy file #1 to file #2.

I think I can use Find::File to search my directory for the basename($imgfile) filename. If it is not there I can use File::Copy to copy it there.

What do you monks advice?

Comment on Search and Copy a filepath
Re: Search and Copy a filepath
by jdporter (Canon) on Oct 31, 2006 at 22:07 UTC
    I have a program...

    So, this is a Perl program you wrote? Let's see it.

    I can either copy the file over as soon as the user selects it or I can have my program read from any directory instead of just my \Images folder.

    Which method is better?

    The latter, by far. You should not impose irrelevant restrictions on your users, and you should not go copying around files — especially large files — and especially not without your users' knowledge.

    We're building the house of the future together.
Re: Search and Copy a filepath
by Joost (Canon) on Oct 31, 2006 at 22:36 UTC
Re: Search and Copy a filepath
by mickeyn (Priest) on Nov 01, 2006 at 05:28 UTC
    you can use FindBin to find your program's path:
    use FindBin qw($Bin);
    now you can search for relative paths starting from $Bin (e.g. $Bin/IMAGES).

    HTH,
    Mickey

Re: Search and Copy a filepath
by doowah2004 (Monk) on Nov 01, 2006 at 09:00 UTC
    If you have a "File Open Box" then you should already have the file path. What ever method you are calling to launch the open file dialog will most likely return the path to the file. For example in Win32::GUI you could use:
    my $file = Win32::GUI::GetOpenFileName( -owner => $Window, # Main window for modal dialog -title => "Open an image file", # Dialog title -filter => [ # Filter file 'JPG file (*.jpg)' => '*.jpg', 'All files' => '*.*', ], -directory => ".", # Use current directory );

    You would probably not want to use the directory flag in your case because you stated that the image could be anywhere. My point is that you can now use $file to open the image, it contains the path to your file. My guess is if you are using some other package/method that it too would return the path to the file.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://581581]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-09-20 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls