CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
remove/unlink

 



six
stranger

Feb 3, 2001, 2:56 PM

Post #1 of 15 (2610 views)
remove/unlink Can't Post

I'm wanting my administration script to find and then delete, from the server, a 'shop' if I no longer need it.

I use the root_path to get to the directories now without error but cant get the script to delete the files and then the directories.

Could someone point me in the right direction


|_cgi-bin/shops
|
|shop_name
|
|_/Admin
|__*.* # 9 administration files
|_/Data
|__*.* # 6 data files
|_/Library
|__*.* # 11 library files

|shop_name
|
|_/Admin
|__*.* # 9 administration files
|_/Data
|__*.* # 6 data files
|_/Library
|__*.* # 11 library files

I've got snippets of code laying around that I've tried, but nothing seems to get the job done. I'm probably missing something easy



Jasmine
Administrator

Feb 3, 2001, 3:08 PM

Post #2 of 15 (2608 views)
Re: remove/unlink [In reply to] Can't Post

The answer to your questions are in our Frequently Asked Questions forum:

How do I delete a file?

How do I delete a directory?

First, delete the files, then you can unlink the directory. Some servers won't let you delete directories unless the directory doesn't contain any files.

(This post was edited by Jasmine on Feb 3, 2001, 2:11 PM)


six
stranger

Feb 3, 2001, 6:02 PM

Post #3 of 15 (2604 views)
Re: remove/unlink [In reply to] Can't Post

Hi Jasmine

I had seen those posts before and tried using them, along with other snippets thru out the forums.

sub all_gone
{
$shopp_direc = "$sc_root_path/shops";
$re_shop = "$in{'re_shop'}";
unlink('$shopp_direc/$re_shop/Admin_files/*') || &CgiDie("I am sorry, but I was unable to delete the files in the all_gone subroutine. The value I have is $shopp_direc/$re_shop/Admin_files.");
rmdir('$shopp_direc/$re_shop/Admin_files');
&admin_delpage;
}

I've also tried

sub all_gone
{
$shopp_direc = "$sc_root_path/shops";
$re_shop = "$in{'re_shop'}";
unlink "$shopp_direc/$re_shop/Admin_files/*" || &CgiDie("I am sorry, but I was unable to delete the files in the all_gone subroutine. The value I have is $shopp_direc/$re_shop/Admin_files.");
rmdir "$shopp_direc/$re_shop/Admin_files";
&admin_delpage;
}

At least with the first snippet, I got the error, on the second one, it just proceeded to the admin_delpage without anything being done.

Thanks for your time






muinatit
Deleted

Feb 4, 2001, 5:27 AM

Post #4 of 15 (2600 views)
Re: remove/unlink [In reply to] Can't Post

Use the first snip if it atleast got you something. Also, the problem is you're using single quotes with strings that have variables in them. So put this in place of the things in your first snip:


Code
unlink ("$shopp_direc/$re_shop/Admin_files/*") || die $!; 
rmdir ("$shopp_direc/$re_shop/Admin_files");



six
stranger

Feb 4, 2001, 6:12 AM

Post #5 of 15 (2597 views)
Re: remove/unlink [In reply to] Can't Post

Thanks muinatit

I thought I had tried that and I did. Still get the error

I've been able to delete an empty folder from the server with

$dir = "$shopp_direc/$re_shop/test/";
rmdir $dir;

so I know I can delete from the server. I just can't get rid of the files and then delete the directory. At least, I know my paths and variables are correct.

If you have any other thoughts they would be appreciated

Brad




muinatit
Deleted

Feb 4, 2001, 8:45 AM

Post #6 of 15 (2593 views)
Re: remove/unlink [In reply to] Can't Post

Does the directory you're trying to delete have subdirectories? rmdir will only work if the folder is completely empty, and unlink with the * won't delete the directories under that one. I tried it with my shell.

muinatit
muinatit@hotpop.com


muinatit
Deleted

Feb 4, 2001, 8:47 AM

Post #7 of 15 (2593 views)
Re: remove/unlink [In reply to] Can't Post

Another thought, add a trailing slash to the directory in the rmdir command.

muinatit
muinatit@hotpop.com

(This post was edited by muinatit on Feb 4, 2001, 7:48 AM)


six
stranger

Feb 4, 2001, 9:53 AM

Post #8 of 15 (2590 views)
Re: remove/unlink [In reply to] Can't Post

Thanks again muinatit

The directory has no sub directories, just files

Adding the trailing slash didn't do it either, I'm still getting the error on unlink, getting rid of the files.

I've even manually removed all but one file from the directory and the script still won't blast that sucker.

Brad



muinatit
Deleted

Feb 4, 2001, 1:51 PM

Post #9 of 15 (2586 views)
Re: remove/unlink [In reply to] Can't Post

Wow... this thing really doesn't want to work for you, does it? I can't think of much else... :(

muinatit
muinatit@hotpop.com


six
stranger

Feb 4, 2001, 2:07 PM

Post #10 of 15 (2584 views)
Re: remove/unlink [In reply to] Can't Post

A bad trip to the dentist is about all that can compare...

Will keep after it and let you know when I get it working, and more importantly, why it wasn't.

Is there any special module I could (should) be calling to make it work?

Brad



Jasmine
Administrator

Feb 4, 2001, 2:40 PM

Post #11 of 15 (2583 views)
Re: remove/unlink [In reply to] Can't Post

In your cgidie calls, put $! in the error message. $! will contain more specific information than "sorry, I can't do this...".

After you put $! in your error messages, run the script and post the error here.



six
stranger

Feb 4, 2001, 7:13 PM

Post #12 of 15 (2578 views)
Re: remove/unlink [In reply to] Can't Post

Thanks Jasmine

I shortened the message to

&CgiDie("Can't delete. The value I have is $shopp_direc/$re_shop/Admin_files.$!");

This is what came back

Can't delete. The value I have is /cgi-bin/eGreyl/shops/jacob/Admin_files.No such file or directory

Hmmmm, even tho it prints out the path to the files with my variables

Again, thanks for the look

Brad





six
stranger

Feb 5, 2001, 6:01 AM

Post #13 of 15 (2571 views)
Re: remove/unlink [In reply to] Can't Post

Back at it again today

I tried this and it worked. I targeted one directory with one file in it, the script deleted the file and then the directory, both on my hardrive and a virtual server

unlink("$shopp_direc/$re_shop/test/test.txt") || &CgiDie("Can't delete. The value I have is $shopp_direc/$re_shop/test. $!");

rmdir("$shopp_direc/$re_shop/test/"); || &CgiDie("Can't delete. The value I have is $shopp_direc/$re_shop/test. $!");


But I still can't get this to work

unlink("$shopp_direc/$re_shop/test/*")
rmdir("$shopp_direc/$re_shop/test/");

Any thoughts? It comes back with the 'no file or directory error'.

Brad





japhy
Enthusiast

Feb 5, 2001, 6:25 AM

Post #14 of 15 (2570 views)
Re: remove/unlink [In reply to] Can't Post

Ok, if you read the documentation, you'll find out that the unlink() function takes a LIST of filenames to delete, NOT a file-glob. If you want to use a file-glob, use the glob() function.


Code
unlink glob "/path/to/files/*" or die "... $!";

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


six
stranger

Feb 5, 2001, 7:07 AM

Post #15 of 15 (2569 views)
Re: remove/unlink [In reply to] Can't Post

Hi japhy

I've seen your post(s) about glob, read the docs you pointed out in those posts and even have the coding in your above post commented out in my script.

I un-commented them and gave them another try

unlink glob "/$shopp_direc/$re_shop/Admin_files/*" or die "The value I have is $shopp_direc/$re_shop/Admin_files... $!";
rmdir("$shopp_direc/$re_shop/Admin_files/"); or die "Directory can't be deleted, the value I have is $shopp_direc/$re_shop/Admin_files... $!";


Tried it both on my computer and server. My computer error came back as

'The value I have is C:/Xitami/cgi-bin/eGreyl/shops/jacob/Admin_files... No such file or directory' and the same on my server. The paths are correct in both errors and I know the files are there.

I know it's tough helping someone from a distance. Is there another approach I can take?

Thanks for your time

Brad


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives