473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trouble unlinking files...

Hi,
I am having a little bit of trouble with unlinking a file.

I would like to unlink it from another directory. The file name contains special characters in it and I have not the ability to change that.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. sub FileRemove(@)
  2. {
  3. my $RemovalFile;
  4. my @goners = @_;
  5.  
  6. foreach $RemovalFile(@goners)
  7.  {
  8.  chomp $RemovalFile;
  9.  print "$RemovalFile \n";
  10.  unlink("\"$RemovalFile\"");
  11.  
  12.  my $status = DoesFileExist($RemovalFile);
  13.  
  14. if ($status)
  15.     {
  16.     `rm "\"$RemovalFile\""`;
  17.      DoesFileExist($RemovalFile);
  18.     }
  19.   }
  20. }
  21.  
  22.  
Mar 11 '13 #1

✓ answered by RonB

If you do the encode/decode, you need to specify the correct encoding.

I ran a test using your script but without doing the encoding and it worked fine for me.
Expand|Select|Wrap|Line Numbers
  1. D:\test\bytes>dir
  2.  Volume in drive D is DATA
  3.  Volume Serial Number is B676-6CE5
  4.  
  5.  Directory of D:\test\bytes
  6.  
  7. 03/11/2013  03:30 PM    <DIR>          .
  8. 03/11/2013  03:30 PM    <DIR>          ..
  9. 03/11/2013  03:29 PM               473 abc---±.txt
  10. 03/11/2013  03:29 PM               659 def--±.txt
  11. 03/11/2013  03:05 PM               392 starlight849.pl
  12.                3 File(s)          1,524 bytes
  13.                2 Dir(s)  194,470,682,624 bytes free
  14.  
  15. D:\test\bytes>starlight849.pl
  16. abc---▒.txt
  17. def--▒.txt
  18.  
  19. D:\test\bytes>dir
  20.  Volume in drive D is DATA
  21.  Volume Serial Number is B676-6CE5
  22.  
  23.  Directory of D:\test\bytes
  24.  
  25. 03/11/2013  03:33 PM    <DIR>          .
  26. 03/11/2013  03:33 PM    <DIR>          ..
  27. 03/11/2013  03:05 PM               392 starlight849.pl
  28.                1 File(s)            392 bytes
  29.                2 Dir(s)  194,470,682,624 bytes free
  30.  

8 2295
RonB
589 Expert Mod 512MB
First, get rid of the prototype.

Using chomp on $RemovalFile is a little odd because that step should have been done with building the array of filenames.

Why are you surrounding the filename with quotes in the unlink statement? Do your filenames actually begin and end with double quotes? I doubt it and that is the most likely reason the unlink is failing.

Perl will tell you why it failed to unlink the file, if you had asked it.

change:
Expand|Select|Wrap|Line Numbers
  1. unlink("\"$RemovalFile\"");
to:
Expand|Select|Wrap|Line Numbers
  1. unlink $RemovalFile or die "failed to unlink '$RemoveFile' $!"
;

Why do you have a DoesFileExist() sub when Perl handles that for you with the -e file test operator?
Expand|Select|Wrap|Line Numbers
  1. my $status = -e $RemovalFile;
Mar 11 '13 #2
Here is the error. A file or directory in the path name does not exist. at line 176, <$REMOVELIST> line 3
The file path is right... However the file name contains ± character in it... I was able to get it working doing the rm command but I would prefer to use unlink..

Could you explain to me why you think I should not use prototypes?

I followed the suggestions you made. Here is my modified code.

Expand|Select|Wrap|Line Numbers
  1. sub FileRemove
  2. {
  3. my $RemovalFile;
  4. my @goners = @_;
  5.  
  6. foreach $RemovalFile(@goners)
  7.  {
  8.  print "$RemovalFile \n";
  9.  unlink($RemovalFile) or die "failed to unlink $RemovalFile $!";
  10.  my $status = -e $RemovalFile;
  11.  
  12. if ($status)
  13.     {
  14.     `rm "\"$RemovalFile\""`;
  15.      DoesFileExist($RemovalFile);
  16.     }
  17.   }
  18. }
  19.  
Mar 11 '13 #3
RonB
589 Expert Mod 512MB
Could you explain to me why you think I should not use prototypes?
Before reading the info linked below, can you explain how prototypes are used in perl and why you think you should use them here?

Far More than Everything You've Ever Wanted to Know about Prototypes in Perl

To handle the special characters, you need to encode/decode the filename.

Here's a link to a solution of a very similar problem to yours.
http://stackoverflow.com/questions/1742279
Mar 11 '13 #4
When I run the encode, decode on the filename it turns the ± to a ? and still fails to remove the file.
Mar 11 '13 #5
RonB
589 Expert Mod 512MB
Please post a short but complete script that demonstrates the problem and a couple sample filenames that fail to be deleted due to the special characters.
Mar 11 '13 #6
ok here is my sample script.
Here is the output
Ive been playing around with the different formats of encoding but still haven't got the syntax right.

Expand|Select|Wrap|Line Numbers
  1. abc---±.txt
  2. failed to unlink abc---?.txt A file or directory in the path name does not exist. at test.pl line 23.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Encode;
  6.  
  7. my @goners = ('abc---±.txt','def--±.txt');
  8. my ($status) = ModelRemove(@goners);
  9.  
  10. sub ModelRemove
  11. {
  12. my $RemovalFile;
  13. my @goners = @_;
  14.  
  15. foreach $RemovalFile(@goners)
  16.  {
  17.  print "$RemovalFile \n";
  18.  $RemovalFile = encode("gb2312", decode("utf-8", $RemovalFile));
  19.  unlink($RemovalFile) or die "failed to unlink $RemovalFile $!";
  20.   }
  21. }
  22.  
Mar 11 '13 #7
RonB
589 Expert Mod 512MB
If you do the encode/decode, you need to specify the correct encoding.

I ran a test using your script but without doing the encoding and it worked fine for me.
Expand|Select|Wrap|Line Numbers
  1. D:\test\bytes>dir
  2.  Volume in drive D is DATA
  3.  Volume Serial Number is B676-6CE5
  4.  
  5.  Directory of D:\test\bytes
  6.  
  7. 03/11/2013  03:30 PM    <DIR>          .
  8. 03/11/2013  03:30 PM    <DIR>          ..
  9. 03/11/2013  03:29 PM               473 abc---±.txt
  10. 03/11/2013  03:29 PM               659 def--±.txt
  11. 03/11/2013  03:05 PM               392 starlight849.pl
  12.                3 File(s)          1,524 bytes
  13.                2 Dir(s)  194,470,682,624 bytes free
  14.  
  15. D:\test\bytes>starlight849.pl
  16. abc---▒.txt
  17. def--▒.txt
  18.  
  19. D:\test\bytes>dir
  20.  Volume in drive D is DATA
  21.  Volume Serial Number is B676-6CE5
  22.  
  23.  Directory of D:\test\bytes
  24.  
  25. 03/11/2013  03:33 PM    <DIR>          .
  26. 03/11/2013  03:33 PM    <DIR>          ..
  27. 03/11/2013  03:05 PM               392 starlight849.pl
  28.                1 File(s)            392 bytes
  29.                2 Dir(s)  194,470,682,624 bytes free
  30.  
Mar 11 '13 #8
You're right it works... It was a problem in the file that was populating my removal list. Thanks so much for all the help Ron, and helping me think. :)
Mar 12 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: James Jiao | last post by:
Hi all I am having a little trouble uploading files (html form with post) with php... I am trying to copy the file from its temporary upload directory to the destination directory, however,...
8
by: phpfrizzle | last post by:
Hi there, I have built a CMS, and in one part you have to be able to delete images. Structure like this: _root --> folder "img" --> file "1.jpg" --> file "2.jpg" --> folder "CMS" ...
3
by: Stangonline.com | last post by:
Ok, im familiar with typical troubleshooting of this type on problem, but I do not do any programming myself. Im hoping you guys can help me with an install problem that I am having. I have an...
2
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
4
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all...
1
by: Ray | last post by:
I have a main form and a subform linked by "ID". I have a means for searching for a client and displaying the info in the subform. However, if the client is not found from the main form I would...
3
by: Jules | last post by:
i'm working with translators that just want to translate "web pages" and not deal with resource files. i'd like to have a file structure that looks sort of like this: / <- root directory where...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
3
by: Dean Richardson | last post by:
Hi, I'm having trouble uploading files via a PHP script. Whenever I upload a file greater than 10K, the file gets corrupted. However, text files upload OK. When I check the FTP Server log I...
10
by: =?Utf-8?B?SnVhbg==?= | last post by:
Hi! I want to use ASP to download big files using ADODB.STREAM. It works very fine with files smaller than 80 MB. On the Webserver I can see that memory allocation and the process w3wp is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.