Posts Tagged ‘PERL’

Removing Empty Directory

Did you know that having empty folders on you system can cause slowdown on you system?  Yes it is true specially if you have program that traverse all the files and folders on your systems.  You know, by looking to the files at our hard disk, it is a jungle out there.

Here is a simple approach to this problem:

dir <Target Dirctory> /b /ad /s | Sort /r > <output file>

EX: dir c:\Windows /b /as /s | sort /r >DirList.TXT

and append RD for each line on of the output file

The concept here is get the directory list and sort it reverse order. Why in reverse order since the directory display the top directory then it goes to it contents.  By reversing the order you will remove the inner folder first, then the outer folder.

Since RD (Remove Directory) removes empty folder only, therefore it will just remove empty folders only.

Here is a PERL function I made:

sub EraseDir()
{

if (!-d $_[0])
{
return 0; #Function Failed
}
else
{
#Get the directory list and sort it reverse order.
system (“dir $_[0] /b /ad /s | Sort /r > _junk.file”);
open LIST ,”_junk.file” || return 0; #Function Failed
open FH,”>_junk.Bat”;
foreach my $line (<LIST>)
{
chop $line;
print FH “RD \”$line\”\n”;
}

close (FH);
close (LIST);
unlink “_junk.file”;
system (“_junk.BAT”);
unlink “_junk.Bat”;
return 1; #Successful
}
}

Advertisements

How to get free space of Hard Disk using script?

The fastest way to get the free disk space is by looking at Windows Explorer, however most of us want to do this in command line or integrate this to a program for any unknown reasons.

The next fastest and easy this is to use dir command. Why? Since it uses Windows native  command.  I don’t know if find first find next function on Assembly or C is faster than this one.

image

On dir result we see the summary at the last line.  All we need to do is place the result of dir command at a text file and parse the last line from the text file.

Here is a sample PERL script so you can understand what I meant.

sub getFreeDiskSpace ()
{
my $dirname = “$_[0]”;
#Execution of the DIR command
system(“dir $dirname /s > junk”);
open FILE,”junk” || die (“Cannot open temporary file – \”junk\””);

#Parsing of the last lines of the DIR results
@_ = <FILE>;
close(FILE);
my $lastline = @_;
my $dir_size = @_[$lastline-2];
my $disk_remain = @_[$lastline-1];
my @fields = split(/ +/,$dir_size);
$_ = $fields[3];
s/,//g; #get rid of commas in the number
my $folder_size=$_;
my $file_count=$fields[1];
@fields = split(/ +/,$disk_remain);
$_ = $fields[3];
s/,//g; #get rid of commas in the number
my $free_space=$_;
my $folder_count=$fields[1];
unlink “junk”;
return ($free_space,$folder_size,$file_count,$folder_count);
}

I go this concept from a script I found from the internet.  I modified it and to a function for portability on the future use.  What struck me the most is I am trying to look for a PERL function and this is what I’ve got.  The lesson I got from this function is “Don’t reinvent the wheel, since it is already invented.” So use what’s available on the market.