Re: A monk in training with a problem
by tachyon (Chancellor) on Jun 12, 2001 at 02:24 UTC
|
You seem to be asking the Monks to write your code for you. Please have a look at this.
Here is a quick guide on how to do it:
you will need to open the dir/s perlfunc:opendir
read the contents of the dir/s perlfunc:readdir
use a conditional construct perlman:perlfunc
and a match type regexp perlfunc:m
rename your files perlfunc:rename
open your database text file perlfunc:open
print to your DB text file perlfunc:print
close you database file perlfunc:close
tachyon
| [reply] |
|
#!/usr/bin/perl -w
use diagnostics;
use strict;
sub getgoodfiles
{
@4by6 = glob(/* 4 by 6.tif\);
@screenres = glob(/* screen res.tif\);
@thumbnail = glob(/* thumbnail.jpg\);
sort @4by6;
sort @screenres;
sort @thumbnail;
}
would this work for getting all the files i need and organizing them?
| [reply] [d/l] |
|
That's a good start as semi-pseudocode, but your example will never compile :
- you're using strict, which requires variables to be declared with my. You fail to do this.
- 4by6 is not a valid variable name, as it starts with a number.
- the parameters you pass to glob are incorrect. Glob expects string values, and you've provided some freeform text that resembles an unterminated regex.
- sort will not change the parameters it's passed, thus requiring, e.g., my @screenres=sort @screenres.
However, you can just as easily write @screenres=sort glob("* screen res.tif");
- Where are your files being stored? Unless a path is specified, glob uses the root directory as a default (I tested this under WinMe, does this hold true for *nix?). Specify your path in glob : my @screenres=sort glob("c:/example/images/* screen res.tif");
Better yet, separate your path out into its own variable for maintenance purposes:
my $imagepath= "C:/example/images/";
my @screenres=sort glob("$imagepath* screen res.tif");
You are doing some good things, which most beginners miss :
- using strict
- using diagnostics (useful for new perl coders)
- splitting work into subs
I hope this makes some sense.
for the first step of your request :
A) The name is changed to something like 000014b6.tif, 00001sr.jpg, and 00001tn.jpg. (and possibly 000018b10.tif)
I would recommend using an underscore between the file name and the picture type, so 00001_4b6.tif and 00001_8b10.tif, and so on. This will assist you in determining where picture names stop, and picture types begin.
Also, check out the rename command, but since you are so new to perl, please back up all your pictures before commencing with renames :
Good luck!
Update a little more experiment with glob seems to reveal its output is always sorted. Can anyone confirm this?
| [reply] [d/l] [select] |
Re: A monk in training with a problem
by bikeNomad (Priest) on Jun 12, 2001 at 02:11 UTC
|
Perhaps something like this (untested) code would work:
#!/usr/bin/perl -w
use strict;
my $dir = $ARGV[0]; # first command line argument
opendir D, $dir or die "can't open $dir: $!\n";
# for each file in directory:
my $file;
while ($file = readdir(D))
{
# only process files called "* screen res.jpg"
next unless $file =~ /(.*) screen res.jpg/;
# but only process them if the corresponding thumbnail exists and is
+a file ($1 is the base name)
next unless -f "$1 thumbnail.jpg";
# now do the family of renames.
rename $file, "${1}sr.jpg"
or die "can't rename \"$file\" as \"${1}sr.jpg\": $!\n";
rename "$1 thumbnail.jpg", "${1}tn.jpg"
or die "can't rename \"$1 thumbnail.jpg\" as \"${1}tn.jpg\": $!\n";
rename "$1 8 by 10.tif", "${1}8b10.tif";
rename "$1 4 by 6.tif", "${1}4b6.tif";
# open the text file for write and close it again
# to make a 0-length file
open T, ">${1}.txt" or die "can't open text file ${1}.txt: $!\n";
close T;
}
closedir D;
update: hmm, "teach a man to fish" vs. "give a man a fish"? Added comments for clarification. | [reply] [d/l] |
|
Well, at least nothing's getting unlinked. :)
UPDATE:
wiz, check out The books I use for a list of really solid paper resources.
TGI says moo
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
Re: A monk in training with a problem
by TGI (Parson) on Jun 12, 2001 at 02:07 UTC
|
File::Find is a really handy way to recurse directory structures and 'do stuff' to the files you find.
BTW, I expect you'll have better results if you post an attempt at writing some code.
TGI says moo
| [reply] |
Re: A monk in training with a problem
by tachyon (Chancellor) on Jun 12, 2001 at 17:17 UTC
|
No this won't work. It does not compile for a start but (to your credit) it looks like you have taken the time to do some reading and presented some code so you will now find help is forthcoming :-)
Here is some code to explain sort to you
#!/usr/bin/perl
use strict;
@a = qw(z x c v b n m); # make a little array
sort @a; # sorts @a but result discarded!
print "@a\n"; # thus @a not sorted here. Here's why:
print sort @a; # prints the sorted @a array
print "\n@a\n"; # damn, still not stored sorted though!
@a = sort @a; # assigns sorted result to @a
print "@a\n"; # as @a now contains sorted result...
# this works!
Questions. As you mantion Win2000 I presume you using Win32 (Windows 95/98/ME/NT/2000) to develop your code. If not what are you using? Do you have perl installed, and if so which distribution? -> ie ActiveState perl. If you have ActiveState perl use explorer to go to c:\perl\html\index.html and click-click -> this is the Perldoc. Are the spaces in the file names real? Spaces are non portable (ie most operating systems except Win32 don't like them - it is better_to_use_an_char_to_separate_your_words_to_make_the_filenames_easier_to_read. What do the xxxxx represent. Are they letters, numbers, spaces, _ chars, other characters (ie .) or some mix of these. Which mix?
Post the answers and then we can look at achieving your goal in a step by step manner.
tachyon | [reply] [d/l] |
Re: A monk in training with a problem
by tachyon (Chancellor) on Jun 12, 2001 at 18:04 UTC
|
Here is some commented code for you to perform what you were aiming for with your pseudo code. Set the $base_dir var and it will run as is.
cheers
tachyon
#!/usr/bin/perl -w
use strict;
# specify the full path to the image files in $base_dir
# do not use \ on win32 use / Perl will convert like magic!
# \ is the escape char. Just *don't use it* for paths
# use / and let Perl take care of the conversion.
my $base_dir = 'c:/windows';
# first let's just read all the files in the dir
# and print them out, this is how we do it:
opendir (DIR, $base_dir) or die "Unable to open dir: $base_dir Perl sa
+ys: $!\n";
while (my $file = readdir DIR) {
print "found file $file\n";
}
closedir DIR;
# OK so now we can read all the files in a dir
# we can look at doing stuff with them.
# What we do is to stuff the files into the
# arrays you wanted using "push"
# we push them into the array if they match
# the criteria you seem to want
# Note that within a match m/ / the . char has a special
# meaning (it matches any single char) so to match a
# literal . we "escape" it with a backslash (backwhack)
my (@image4by6, @screenres, @thumbnail); # pacify strict
opendir (DIR, $base_dir) or die "Unable to open dir: $base_dir Perl sa
+ys: $!\n";
while (my $file = readdir DIR) {
push @image4by6, $file if $file =~ m/4 by 6\.tif/;
push @screenres, $file if $file =~ m/screen res\.tif/;
push @thumbnail, $file if $file =~ m/thumbnail\.jpg/;
}
closedir DIR;
# now lets print out our sorted arrays
# via a function that we define (a subroutine)
print "\n\nImage4by4 array contains:\n";
print_sorted( @image4by6 );
print "\n\nScreenres array contains:\n";
print_sorted( @screenres );
print "\n\nThumbnail array contains:\n";
print_sorted( @thumbnail );
exit;
# this sub takes one argument - an array
# the array is passed to the sub in the
# magical @_ array. We sort this array
# storing the result in @sorted
# we then use a loop to iterate over
# each element assigning it to $file
# we print each file in turn plus a newline \n
sub print_sorted {
my @sorted = sort @_;
foreach my $file (@sorted) {
print "$file\n";
}
}
| [reply] [d/l] |
Re: A monk in training with a problem
by scottstef (Curate) on Jun 12, 2001 at 17:43 UTC
|
wiz,
You sound like you are trying to undertake a fairly LARGE and complex problem for someone at our level.
I would recommend shelling out $30 for the llama book.
Being new to perl without any prior programming experience, I found this book to be indespensible.
While this book has very little "orignal technical" material in it, the way the material is presented has been easy for a blockhead such as me to follow.
There are two functions I have found to be invaluable as a newbie:
use strict; use diagnostics;
I always used the strict, I just learned about the diagnostics. This produces wonderfully verbose warning/error messages so I at least know where to begin looking for fixes.
Good luck, I admire your ambition as I have yet to try anything that bold since I started playing with perl. %^) | [reply] |
Re: A monk in training with a problem
by nysus (Parson) on Jun 12, 2001 at 01:56 UTC
|
| [reply] |