Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Common file operations

Reply
Thread Tools

Common file operations

 
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      10-25-2004
I'm trying to find Perl equivalents for some common operations in
other languages. I'm also looking for some style guides.

1. If I have a partial file name, how do I get the complete path?
I tried canon and glob, but both returned the partial name.

2. If I have a directory name and a file specification, ho do I find
all files in that directory matching the specification. File::Find
and issuing an ls command seem like overkill. I could use readdir
if I don't need a recursive search, but I was hoping for an
equivalent of SysFileTree in OS/2.

Any advice? Thanks.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-25-2004

Quoth "Shmuel (Seymour J.) Metz" <(E-Mail Removed)>:
> I'm trying to find Perl equivalents for some common operations in
> other languages. I'm also looking for some style guides.


The canonical style guide is perlstyle...

> 1. If I have a partial file name, how do I get the complete path?
> I tried canon and glob, but both returned the partial name.


File::Spec->rel2abs

> 2. If I have a directory name and a file specification, ho do I find
> all files in that directory matching the specification.


From what you say below ITYM 'all files in that directory or any
subdir'? If you don't, you can use glob (or <>).

> File::Find and issuing an ls command seem like overkill. I could
> use readdir if I don't need a recursive search, but I was hoping
> for an equivalent of SysFileTree in OS/2.


File::Find or one of the modules built on it (e.g. File::Find::Rule) are
the only way (well, beyond rewriting File::Find yourself from readdir,
of course). Perl's interfaces are Unix-based; the only primitive for
reading directory entries in Unix is readdir.

OTOH if you don't care about portability you can probably find (or
fairly easily write: see perlxstut, perlxs; or Inline::C) an interface
to SysFileTree (that is, if you're actually working on OS/2 as opposed
to simply using it an an example).

Ben

--
We do not stop playing because we grow old;
we grow old because we stop playing.
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      10-25-2004
On Mon, 25 Oct 2004 15:52:30 -0300, "Shmuel (Seymour J.) Metz"
<(E-Mail Removed)> wrote:

> 1. If I have a partial file name, how do I get the complete path?
> I tried canon and glob, but both returned the partial name.


I don't trust you! (Minimal) examples?

Also, see 'perldoc -q glob'.

> 2. If I have a directory name and a file specification, ho do I find
> all files in that directory matching the specification. File::Find
> and issuing an ls command seem like overkill. I could use readdir


Then just use File::Find. Ever heard about regexen?

> if I don't need a recursive search, but I was hoping for an
> equivalent of SysFileTree in OS/2.


Then glob() should do. Again, you should show some actual example
proving that it doesn't. See also

perldoc -f glob
perldoc File::Glob


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-25-2004
Shmuel (Seymour J.) Metz wrote:
> I'm trying to find Perl equivalents for some common operations in
> other languages. I'm also looking for some style guides.
>
> 1. If I have a partial file name, how do I get the complete path?


Your question is not clear to me. Or maybe it is actually two questions?

First part would be to get the list of all filename, which match the given
partial name. Please note, typically this is not a 1:1 relationship but a
1:n because a given partial name can be completed into many actual names.
glob() will do that for you.

And the second part would be how to prepend the CWD to the file names found.
That's a trivial job for the double quote operator or the concatenation
operator ".".

> I tried canon and glob, but both returned the partial name.


Where did you find a function "canon"? glob() is the right tool.
Please show us the actual code, then we may be able to find out why you
didn't get the results you expected.

> 2. If I have a directory name and a file specification, ho do I find
> all files in that directory matching the specification.


What is a file specification in your terminlogoy?

> File::Find
> and issuing an ls command seem like overkill. I could use readdir
> if I don't need a recursive search, but I was hoping for an
> equivalent of SysFileTree in OS/2.


If you don't want to recurse into subdirectories then use readdir() (and
maybe stat() and other file information utilities to determine if each file
matches the specification).
If you want to recurse then use File::Find (and maybe stat() and other file
information utilities to determine if each file matches the specification).

jue


 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      10-26-2004
In <(E-Mail Removed)>, on 10/26/2004
at 12:42 AM, Michele Dondi <(E-Mail Removed)> said:

>I don't trust you! (Minimal) examples?


If you don't trust me, then what's the point of giving you examples?

>Also, see 'perldoc -q glob'.


"No documentation found"

>Then just use File::Find.


I will if what I wanted doesn't exist.

>Ever heard about regexen?


No.

>Then glob() should do.


No.

>Again, you should show some actual example proving that it doesn't.


I might, for someone who isn't prejudiced. Why should I bother for
someone who already has made up her mind?

> perldoc -f glob


No documentation found for "perlfunc"

> perldoc File::Glob


I don't see anything there that wan't already in my de3ad tree.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      10-26-2004
In <ywffd.5216$LT1.3686@trnddc09>, on 10/25/2004
at 10:56 PM, "J rgen Exner" <(E-Mail Removed)> said:

>Your question is not clear to me. Or maybe it is actually two
>questions?


First, some background. In OS/2 there is no[1] concept of mounting a
directory; instead, file systems are referred to by drive letter,
there is a current dirve and each drive letter has an associated CWD.
A file specification of the form[2] d:\name.ext is interpreted as
d:\cwd\name.ext, a name of the form name.ext is interpreted as being
in the cwd of the current drive and a name of the form \path\name.ext
is interpreted as being in the specified directory on the current
drive. I'd like to allow the user to specify any of the forms and
convert them to d:\path\name.ext.

>First part would be to get the list of all filename, which match the
>given partial name. Please note, typically this is not a 1:1
>relationship but a 1:n because a given partial name can be
>completed into many actual names.


Of course.

>And the second part would be how to prepend the CWD to the file
>names found. That's a trivial job for the double quote operator or
>the concatenation operator ".".


Simple but not trivial, because I have to first test whether the drive
is already their and whether the absolute path is already there.

>Where did you find a function "canon"?


Sorry, that's a typo. It's the canonpath method of the File:Spec
module. It's in the online Perl documentation and also in the
Dromedary.

>Where did you find a function "canon"? glob() is the right tool.


No, glob() expands wildcards but does not fill in implied components.

>Please show us the actual code,


extproc G:\emx\bin\perl -STw
#!/usr/bin/perl -Tw
#
use constant TRUE => 1;
use constant FALSE => '';
use File::Spec qw(catdir catfile splitdir splitpath);
#ARGV=('m*','xyz');
my ($dir,$file)=@ARGV;
print '@ARGV=(',join(',',@ARGV),")\n";
print '$dir =',"$dir\n";
$dir =~ s/^\'?([^\']*)\'?$/$1/;
print '$dir =',"$dir\n";
print '$file=',"$file\n";
$file =~ s/^\'?([^\']*)\'?$/$1/;
print '$file=',"$file\n";
my @dirs=glob($dir);
print '@dirs=(',join(',',@dirs),")\n";
unless (scalar(@dirs) == 1) {
print "$dir matches multiple directory names:\n";
die ' ', join("\n ",@dirs),"\n"
}
die "$dir is an invalid directory name.\n" unless -d $dir;

>What is a file specification in your terminlogoy?


I'd probably want to support the OS/2 conventions, which use * and ?
as wildcards, with ? matching a single component. I haven't decided
whether to restrict the input a bit or to translate into a regular
expression. d:\m?file.* would match, e.g., d:\cwd\mafile.cmd,
d:\cwd\mbfile.exe, d:\cwd\mcfile.html.

>If you want to recurse then use File::Find (and maybe stat() and
>other file information utilities to determine if each file matches
>the specification).


That's what I wanted to avoid

Thanks.

[1] Well, yes, but that doesn't count. And it leaves the same
underlying problem.

[2] OS/2 uses \ as a separator rather than /.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
Shmuel (Seymour J.) Metz
Guest
Posts: n/a
 
      10-26-2004
In <251020041625004613%(E-Mail Removed)>, on 10/25/2004
at 04:25 PM, Jim Gibson <(E-Mail Removed)> said:

>What computer language allows you to enter a partial file name and
>get a complete path?


REXX.

>Or do you mean you have a partial or relative path
>representation and you want to get the full, absolute path name?


Yes.

>If the latter, check out the File::Spec::Unix module or its brethren.


File::Spec::canonpath doesn't do it.

>[answered elsewhere in this thread]


Not really. What was elsewhere in the thread was that I could write
code to do it, using File::Find, which I had said I'd like to avoid. I
was looking for an equivalent to

if file = '' then file = 'forward+*'
CALL SysFileTree file, files, FOS

which returns an array of file names, including drive and directory
information.

[1] Well, close enough.

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to (E-Mail Removed)

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-27-2004
"Shmuel (Seymour J.) Metz" <(E-Mail Removed)> wrote in
news:417edf60$2$fuzhry+tra$(E-Mail Removed) :

> In <(E-Mail Removed)>, on 10/26/2004
> at 12:42 AM, Michele Dondi <(E-Mail Removed)> said:
>
>>I don't trust you! (Minimal) examples?

>
> If you don't trust me, then what's the point of giving you examples?


He does not trust the assertions you make without providing test cases that
exhibit the problems you mention.

>>Also, see 'perldoc -q glob'.

>
> "No documentation found"


As it turns out, he was right not to trust you:

Found in C:\Perl\lib\pod\perlfaq5.pod
Is there a leak/bug in glob()?
Due to the current implementation on some operating systems, when you
use the glob() function or its angle-bracket alias in a scalar context,
you may cause a memory leak and/or unpredictable behavior. It's best
therefore to use glob() only in list context.

Why doesn't glob("*.*") get all the files?
Because even on non-Unix ports, Perl's glob function follows standard
Unix globbing semantics. You'll need "glob("*")" to get all (non-
hidden)
files. This makes glob() portable even to legacy systems. Your port may
include proprietary globbing functions as well. Check its documentation
for details.

You can also consult perldoc -f glob and perldoc perlop.

>>Then just use File::Find.

>
> I will if what I wanted doesn't exist.


You haven't described what you want.

>>Ever heard about regexen?

>
> No.


Then you shoud read

perldoc perlre
perldoc perlretut

etc so on and so forth.

>>Again, you should show some actual example proving that it doesn't.

>
> I might, for someone who isn't prejudiced.


Feel free to insist on not asking questions _the_ smart way. I think all of
us are prejudiced against people who describe their problems in inexact
verbal form rather than in Perl (by the way, have you read the posting
guidelines for this group. They are posted here regularly).

> Why should I bother for someone who already has made up her mind?


Are you related to Arnold Schwarzenegger by any chance? I would read the
name of the person I am corresponding with a little more carefully if I
were you.

>> perldoc -f glob

>
> No documentation found for "perlfunc"


Then you must have messed up your installation. Fix it soon. Programming
without documentation is not a good idea.

>> perldoc File::Glob

>
> I don't see anything there that wan't already in my de3ad tree.


???

Sinan
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-27-2004
"Shmuel (Seymour J.) Metz" <(E-Mail Removed)> wrote in
news:417edfa1$3$fuzhry+tra$(E-Mail Removed) :

> use constant TRUE => 1;
> use constant FALSE => '';


Please don't do this. Perl already has a well defined notion of true and
false. Don't invent new things.

In fact, this reminds me of my favorite comp.lang.c FAQ:

http://www.eskimo.com/~scs/C-faq/q10.2.html

Sinan.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-27-2004
Shmuel (Seymour J.) Metz <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)>, on 10/26/2004
> at 12:42 AM, Michele Dondi <(E-Mail Removed)> said:



>>Also, see 'perldoc -q glob'.

>
> "No documentation found"



Then go find it!

(you have a broken perl installation)


>>Ever heard about regexen?

>
> No.



You must live a sheltered life.


>>Then glob() should do.

>
> No.



How do you know what glob() can and can't do if you cannot
access the docs for glob()?

Or do you just like typing "No" a lot?


>>Again, you should show some actual example proving that it doesn't.

>
> I might, for someone who isn't prejudiced. Why should I bother for
> someone who already has made up



Because there are hundreds of other people here besides the two of you,
and maybe one of them would help you if you helped them to help you...


> her mind?



s/her/his/;


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
java.lang.NoSuchMethodError: wm.common.session.Common.getCustRptListFromMax Denny Java 1 05-01-2008 07:33 AM
how to use io operations with open/save file dialog box in file menu for simple notepad boris Java 1 07-22-2007 04:13 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
Binary File Operations Jeremy Robbins Perl 0 07-14-2004 09:45 PM



Advertisments