unlink, rmdir problems on windoze !

Posted on 2006-05-07
Medium Priority
Last Modified: 2008-02-01
Ok the script im using,

$datadir = 'C:\\my-backups\\backups\\';
$x = '5';

opendir( DIR, $datadir );
@folders = readdir(DIR) ;
foreach $folder (@folders){
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($folder);
  if((time - $mtime) > (60*60*24*$x)){
  if ($folder != "." ||$folder != ".."||$folder !="del.pl"){
    ## unlink "$datadir$folder";   ## didnt work;(
    system("del","/q",$folder)or die("could not delete $folder"); ### dies could not delete folder !!!
    print "$datadir$folder  deleted\n";

Trouble shooting steps,
1) made sure that perl.exe was allowed in DEP  ( thought it was a good idea anyway)
2) user that ran the script is admin with full rights...
3) placed script in folder that had subs to be deleted, so msdos didnt have to cd...
windows 2003 server, activestate perl


Question by:ghboom
  • 4
  • 3
  • 2
  • +3
LVL 19

Expert Comment

by:Kim Ryan
ID: 16627322
The perl convention is to always use forward slashes to specify directories. It will convert internallly to back slashes for windows. So long as you run your program from the C: drive, you should be able to say:

$datadir = '/my-backups/backups/';
unlink "$datadir$folder" or die $!;
LVL 17

Expert Comment

ID: 16627456
Or I'm pretty sure $datadir = "C:\\my-backups\\backups\\"; will work...

Author Comment

ID: 16627491
Yea, on windows \\ is like / no problem there.
$datadir = "C:\\my-backups\\backups\\";
The $datadir var does hold the value of "C:\my-backups\backups\"  ...

Thats not the problem, unlink will just not work, thats why I tried the system call. now that
that doesnt work, I figured it was a permissions issue, but cant nail it. anyone ever have a simmilar problem ?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 19

Expert Comment

by:Kim Ryan
ID: 16627520
the C:\\ portion has no meaning in perl when you want to do unlink. That why I suggested getting back to the unix like format that perl needs of  $datadir = '/my-backups/backups/';  The unlink should then be able to function correctly.

You can still use back slashes if you want to run system commands, but this seems to be a work around for the original issue of unlink not working.

Author Comment

ID: 16627552
I was trying your suggestion (figuring that what I know about perl could fit in the next 3 lines LOL)
when a co worker logged into the box !
so, soon as they finish, I will report back :)

LVL 17

Expert Comment

ID: 16627817
I've used unlink on Windows computers using each method.  As a quick test, I just created a directory Unlink_test, put a file in it called New Text Document.txt, and ran this script:

#!/usr/bin/perl -w
use strict;

unlink "C:\\Documents and Settings\\All Users\\Desktop\\Unlink_test\\New Text Document.txt";

It worked perfectly.

LVL 17

Accepted Solution

mjcoyne earned 1000 total points
ID: 16628080
Just had a thought -- my test example above deleted a *file*.

I notice, however, that your variables are called $datadir and $folder.  Does the use of "$folder" imply that you're trying to delete a directory?  If so, unlink won't do it -- you need rmdir.  Something like:

#!/usr/bin/perl -w
use strict;

my $folder = "backups";
my $datadir = 'C:\\Documents and Settings\\All Users\\Desktop\\Unlink_test\\';

chdir ($datadir);
system ("rmdir /s/q $folder");

will change to the directory specified by $datadir and delete the directory specified by $folder (and all files in it).

Please be careful when testing or adapting this script -- if you make a typo, a whole section of your directory tree could be wiped out...

Assisted Solution

GnarOlak earned 600 total points
ID: 16631568
First, I think your if statement is incorrect:

if ($folder != "." ||$folder != ".."||$folder !="del.pl"){

will always be true since anything in $folder can't match all three.  For example, "." will not match ".." and since "or" logic only has to match one of the clauses and so the second clause will be true and the entire statement will be true.

You might have meant:

if ($folder != "." && $folder != ".." && $folder !="del.pl"){

meaning, in english, only delete things other than ., .., and del.pl.

Second, output the contents of $! upon error so you can see what the OS is complaining about:

    system("del","/q",$folder)or die("could not delete $folder: $!"); ### dies could not delete folder !!!

That might answer your question for you.

LVL 39

Expert Comment

ID: 16631653
In case you have any directories, you'll probably want to skip them...

foreach $folder (@folders){
    next if -d "$datadir$folder";  #skips all directories, including "." and ".."
    next if $folder =~ /del\.pl/i;    #skips "del.pl":  lets you get rid of "if ($folder...." statement
LVL 85

Expert Comment

ID: 16636540
 $folder != "."
  $folder != ".."
mean the same as
  $folder != 0
You may have meant to say
  $folder ne "."

use warnings;
would have warned you that Argument "." isn't numeric in numeric ne (!=)

Author Comment

ID: 16695645
I have not abandoned this, the server this was being developed on went down due to a bad power supply !

Hopefully I will be able to resume this within a few days...

Author Comment

ID: 16825298
First I have to say im sorry for taking so long to get to this,
second I have not had time to fully test as quite a few other issues poped up but
mjcoyne hit the nail on the head and GnarOlak may have helped with future logic.

I hope that when I re-visit this you folks wouldnt mind a little help if I need it ;)


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question