Ask a Question related to PERL Beginners, Design and Development.
-
Perlwannabe #1
Still need unlink help!! God help me.
Still having problem with unlink. My original problem began with deleting
files from a list. I seem to have fixed the problem reading the list but
the unlink does not work.
Here is the test script that I am working from:
my $file = 'listitems.txt';
open my $fh, $file or die "Cannot open $file: $!";
while($Line = <$fh>) {
chomp($Line);
my $del = "*$Line*.*";
my $complete = "c:/testdir/$del";
unlink glob $complete or die "unlink failed: $!";
}
A sample of the contents of the 'listitems.txt' file is:
item997
item996
item999
item983
The directory c:/testdir/ contains many files, but I want to delete the
files with any *item###*.* from the directory. So if the directory has
3000 files I want to delete every file that has item997 in it.
So file "testitem997document.txt" will be deleted as well as
"real1tem997file.doc" as well as "killitem983.txt.doc".
The sample script I posted aboveis returning the following error:
unlink failed: at testscript.pl line 7, <$fh> line 1.
Working in Windows XP and ActivePerl 5.8.0. BTW...this is perl.beginners
so be kind and understanding. :-)
Perlwannabe Guest
-
Unlink and variables
Hello, I created a form to upload files. The formfield contents are stored as records in an Ascii file "data.dat"; for reasons I am not using a... -
Still need unlink help!! God help me. - Revisited
<20030808202717051099.GyazMail.neuroball@usa.net> <YW50aWdvbmU=.45ab06a65368617cf57750f9f6311818@1060402929.cotse.net> -
Help with Unlink please
> Steve Grazzini wrote at Wed, 06 Aug 2003 23:38:00 -0400: Neither of these worked. I am beginning to think that there is something wrong with... -
problem using unlink()
Hi, I have a problem using unlink(); on a system using FreeBSD and Apache... where my local system using Windows and Apache experiences no... -
unlink() in a loop???
I have a script that allows users to upload pics and enter personal info into Database (not pictures i only store filename in db). The form passes... -
Bob X #2
Re: Still need unlink help!! God help me.
"Perlwannabe" <perlwannabe@antigone.cotse.net> wrote in message
news:YW50aWdvbmU=.7af697ee56fdcc9b2afb1e89f798017d @1060387302.cotse.net...Can you post more of the script? As it stands $fh is nothing.> Still having problem with unlink. My original problem began with deleting
> files from a list. I seem to have fixed the problem reading the list but
> the unlink does not work.
>
> Here is the test script that I am working from:
>
> my $file = 'listitems.txt';
> open my $fh, $file or die "Cannot open $file: $!";
> while($Line = <$fh>) {
> chomp($Line);
> my $del = "*$Line*.*";
> my $complete = "c:/testdir/$del";
> unlink glob $complete or die "unlink failed: $!";
> }
>
Normally I would open a filehandle like so:
my $file = 'listitems.txt';
open(FH, "<" . $file) || die "Failed to open $file: $!";
Bob X Guest
-
Oliver Schnarchendorf #3
Re: Still need unlink help!! God help me.
On Fri, 8 Aug 2003 22:16:38 -0400 (EDT), perlwannabe wrote:
okay... in ascii the hex of 0d0a works out to character #13 and #10> I tried both the suggested methods without success. But, I mightuv found a
> problem. I tried all of these methods with no success and looked at the
> input file "listitems.txt" and found something interesting when using a
> hexeditor. Each line ends with 0D0A, so in a hex editor the previous file
> looks like:
which works out to CR and LF.
CRLF is the normal Carriage Return Linefeed that is used to separate
lines on windows.
It seems that everybody here pointed you into the right direction...
but there seems to be something else wrong than the provided code (or
your code for that matter). It seems that the data you use is the
problem.
To really find out what the problem is... you need to learn how to
debug your code. A starting point would be:
perldoc perldebtut<Return>
or
perldoc perldebug<Return>
you should type that on the dos/command prompt in windows.
Add the following lines to your program under the perl location line:
use warnings;
use diagnostics;
use strict;
It will check automatically for stractural errors that are often made
and will give you (hopefully) meaningful error/warning messages.
Another thing to do is to print everything to the screen.
You define a variable? Print it.
You use a variable? Print it before it's use and after it's use.
BTW. Write at first in PSEUDOCODE and than translate it into perl. (I
will add comments instead)
E.g.:
#!/usr/bin/perl
use warnings;
use diagnostics;
use strict;
# Declaration and initialization of $file
my $file = 'listitems.txt';
print $file."\n";
# Open $file with a filehandle of FH, die if problems arise
open FH, $file or die "Cannot open $file: $!\n";
# Go through the while loop as long as FH returns lines
while(<FH>)
{
# print line that is returned from FH
print $_;
# Remove trailing return from line
chomp;
# Set $del to the globing pattern
my $del = "*".$_."*.*";
print $del."\n";
# Set $complete to the path and append the globbing pattern in $del
my $complete = "c:/testdir/$del";
print $complete."\n";
# unlink a list of files that are returned by the globing command or
fail
my $deleted_files = unlink (glob $complete) or die "unlink failed: $!";
# print out the number of the 'unlinked' files
print $deleted_files."\n";
}
So... following this you are opening a file and for each line that the
file contains you are trying to unlink all files returned by 'glob
$complete'.
What you should do, if you have still problems after working through
perldebtut and perldebug, is the following:
Write a test program with the same care you use for real programs. Add
PSEUDO CODE or COMMENTS of what you want the code to do. Add debug
helps (print to screen, use Data::Dumper for comples structures)...
thanks
/oliver/
Oliver Schnarchendorf Guest
-
Perlwannabe #4
Re: Still need unlink help!! God help me.
> On Fri, 8 Aug 2003 22:16:38 -0400 (EDT), perlwannabe wrote:
<SNIP, SNIP>> okay... in ascii the hex of 0d0a works out to character #13 and #10>> I tried both the suggested methods without success. But, I mightuv
>> found a problem. I tried all of these methods with no success and
>> looked at the input file "listitems.txt" and found something
>> interesting when using a hexeditor. Each line ends with 0D0A, so in a
>> hex editor the previous file looks like:
> which works out to CR and LF.
>
> CRLF is the normal Carriage Return Linefeed that is used to separate
> lines on windows.
>
> It seems that everybody here pointed you into the right direction...
> but there seems to be something else wrong than the provided code (or
> your code for that matter). It seems that the data you use is the
> problem.
>
> To really find out what the problem is... you need to learn how to
> debug your code. A starting point would be:
>
> perldoc perldebtut<Return>
> or
> perldoc perldebug<Return>
>
> you should type that on the dos/command prompt in windows.
>
> Add the following lines to your program under the perl location line:
>
> use warnings;
> use diagnostics;
> use strict;
>
> It will check automatically for stractural errors that are often made
> and will give you (hopefully) meaningful error/warning messages.
>
> Another thing to do is to print everything to the screen.
>
> You define a variable? Print it.
> You use a variable? Print it before it's use and after it's use.
>
> BTW. Write at first in PSEUDOCODE and than translate it into perl. (I
> will add comments instead)
>
Oliver, thank you for the response. I did indeed comment every single
variable, but deleted it from my sample script for simplicity. I have
been wrestling with this problem for a while. I did run your code and got
the following error:
C:\testdir>perl testfile.pl
listitems.txt
item997
*item997*.*
c:/testdir/*item997*.*
Uncaught exception from user code:
unlink failed: at testfile.pl line 27, <FH> line 1.
You mention a "perl location line" what is that? Is there something I
have to declare for the program to work? I read something about
File::glob but I don't think that has anything to do with it.
BTW...I have also hard coded the location as follows:
$location = 'c:/testdir/*item997*.*';
unlink glob $location;
THIS WORKS!!! That is why this stupid problem is killing me. I can write
a script that will get me the location 'c:/testdir/*item997*.*' by using
variables, but when I try to unlink the program bombs. You can see by the
printout of your script that it is indeed returning
"c:/testdir/*item997*.*" for your variable $complete, but for whatever
reason, it won't unlink $complete. It doesn't even continue to process
the remaining lines in the file, it bombs after reading the first line of
'listitems.txt'
Perlwannabe Guest
-
Oliver Schnarchendorf #5
Re: Still need unlink help!! God help me.
On Sat, 9 Aug 2003 00:22:09 -0400 (EDT), perlwannabe wrote:
great... ;) I love to remote debug other peoples script (yup, thqt was> I did indeed comment every single
> variable, but deleted it from my sample script for simplicity. I have
> been wrestling with this problem for a while. I did run your code and got
> the following error:
>
> C:\testdir>perl testfile.pl
> listitems.txt
> item997
> *item997*.*
> c:/testdir/*item997*.*
> Uncaught exception from user code:
> unlink failed: at testfile.pl line 27, <FH> line 1.
irony).
--- script starts
#!/usr/bin/perl
use warnings;
use diagnostics;
use strict;
# Declaration and initialization of $file
my $file = 'listitems.txt';
print $file."\n";
# Open $file with a filehandle of FH, die if problems arise
open FH, $file or die "Cannot open $file: $!\n";
# Go through the while loop as long as FH returns lines
while(<FH>)
{
# print line that is returned from FH
print $_;
# Remove trailing return from line
chomp;
# Set $del to the globing pattern
my $del = "*".$_."*.*";
print $del."\n";
# Set $complete to the path and append the globbing pattern in $del
my $complete = "c:/testdir/$del";
print $complete."\n";
# find a list of all files fitting $complete
my @filenames = glob $complete;
# print out each filename and delete them one by one... and hopefully
finding the error
foreach (@filenames)
{
print "Deleting ".$_."... ";
my $result = unlink ($_) or die "has failed. Error: $!\n";
print "was successful!\n" if $result;
}
}
---script ends
Btw. everything was okay in the previous script until the 'unlink'
command was reached. Did you look into permission problems? Are you
trying to delete files that are maybe locked by IIS?
'#!/usr/bin/perl' is the perl location line, because it tells the shell> You mention a "perl location line" what is that?
were it can find the program that it needs to run the script (on unix
that is).
I hope that this script finds the culprit for you.
thanks
/oliver/
Oliver Schnarchendorf Guest
-
David #6
Re: Still need unlink help!! God help me.
"Perlwannabe" <perlwannabe@antigone.cotse.net> wrote in message
news:YW50aWdvbmU=.61f1f3ab11a3e097c9a73e074eb62e78 @1060395398.cotse.net...that's not the problem. in fact, you expect to see this in Windos. they are> <SNIP, SNIP>>> >> >>
> >> The directory c:/testdir/ contains many files, but I want to delete
> >> the files with any *item###*.* from the directory. So if the
> >> directory has 3000 files I want to delete every file that has item997
> >> in it.
> >>
> > glob returns the relative path of the files so if you are in a
> > different
> > directory (other than c:/testdir) executing the script, it will not
> > find the files. try fully qualify the path:
> >
> > #!/usr/bin/perl -w
> > use strict;
> >
> > open(FH,"listitems.txt") || die $!;
> > unlink map{chomp; glob "c:/testdir/*$_*.*" } <FH> or die $!;
> > close(FH);
> >
> > __END__
> >
> > or:
> >
> > [x]$ perl -lne 'unlink glob "c:/testdir/*$_*.*" or die $!'
> > listitems.txt
> This suggestion returned the error:
>
> Died at testscript.pl line 4, <FH> line 15.
>
> I tried both the suggested methods without success. But, I mightuv found a
> problem. I tried all of these methods with no success and looked at the
> input file "listitems.txt" and found something interesting when using a
> hexeditor. Each line ends with 0D0A, so in a hex editor the previous file
> looks like:
>
> item9970D0Aitem9960D0Aitem9990D0Aitem9830D0A
>
> but when I look at the file with notepad it looks like:
>
> item997
> item996
> item999
> item983
>
> Could this be the culprit?
just CRLF. tell us a few things:
* where's listitems.txt? (full path please)
* where's testscript.pl? (full path please)
* where are you executing testscript.pl? (full path please)
* you are trying to delete files inside c:/testdir/ correct?
as you might have notice by now, the script is very simple and there isn't a
lot of places where it can go wrong. the location of those files are
critical to the success your script.
david
David Guest
-
Perlwannabe #7
Re: Still need unlink help!! God help me. - Revisited
In my zeal for making things simple I may have created a problem. OK,
here is the lowdown:
the real file names I am trying to delete from the directory are rather
lengthy and complicated and look like:
partial_qn_ri_pub_default_asp__Online_qdisp_bn_030 42997-0-0
partial_qn_ri_pub_default_asp__Online_qdisp_bn_030 42995-0-0
partial_qn_ri_pub_default_asp__Online_qdisp_bn_030 42882-0-0
.. . .
I cut the names down in my previous posts to 'item997' for ease and
simplicity, this was a mistake on my part as I am a true "beginner." It
appears that unlink does not like something in the real file name and is
having trouble deleting such a long file name.
If I drop to command prompt and type "del *997*.* the file
"partial_qn_ri_pub_default_asp__Online_qdisp_bn_03 042997-0-0" is deleted.
So I tried to use the unlink and it did not work. However, I then renamed
the lengthy file name to "5item997.txt" and used the unlink to "*997*.*"
AND IT WORKED!!!
This means that there is something with the original filename (the really
long one) that the unlink command does not like...perhaps the length, or
the underscores, or the - (dashes), etc.
Sorry for the problem. All of the previous responses worked and everyone
was helpful. My description was lacking...a mistake I will not make
again.
Is there some magical command that unlink needs to delete such a long file
name? Are there characters in the filename that is giving unlink a
problem?
Man, I am so close I can taste it. Again, thank you for all of your help.
Perlwannabe Guest
-
Dave Arnold #8
Re: Still need unlink help!! God help me. - Revisited
In article
<YW50aWdvbmU=.b5f5cdebb1363e9b5422d22aeff7ce2e@106 0449982.cotse.net>,
perlwannabe <perlwannabe@antigone.cotse.net> wrote:
[Snip][Snip]> partial_qn_ri_pub_default_asp__Online_qdisp_bn_030 42997-0-0
> partial_qn_ri_pub_default_asp__Online_qdisp_bn_030 42995-0-0
> partial_qn_ri_pub_default_asp__Online_qdisp_bn_030 42882-0-0Just a suggestion, but could it be because unlink is taking the delete> So I tried to use the unlink and it did not work. However, I then
> renamed the lengthy file name to "5item997.txt" and used the unlink to
> "*997*.*" AND IT WORKED!!!
"*.*" literally and expecting the file to contain a dot, whereas the shell
"knows" about DOS extensions and removes files without them?
Dave.
Dave Arnold Guest



Reply With Quote

