Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Sorting Script

Status
Not open for further replies.

SegFaultAX

Programmer
Oct 12, 2007
2
US
Problem: I have a bunch of files with assorted extensions in a "dump" folder. I want to sort the files into directories according to their extensions. Assume that the extension will ALWAYS be followed by the last period. If there is no period present, then it can either be ignored or moved into some other directory. The directory will be a direct child of the dump dir. If a file is found that has an extension with no corresponding directory, make it, then move it. Under some circumstances, there may be duplicate file names, in which case something like _1 or _2 can be appended to the end.

Question: How difficult would a script like this be to write? Are there any limitations in perl that would make some aspect of this impossible? Can anyone provide examples that would at least give a skeleton for creating a script like this? Any other suggestions or comments?

Thanks in advance!
Cheers, MK
 
Ignore syntax error...

$_ = join ("", (chop (@split_string));

Changed to...

chop (@split_string);
$_ = join ("", @split_string);

Why doesn't that work! Would help shorten code

 
My code = complete failure :/

The above code only processes one file at a time, so I have to keep refreshing for it to process every file. And all files are placed into the No_Ext folder. I think only the first if part of the code runs :s.

I thought it was perfect hehe
 
This is hopefully my final question:

I have this:
if (($_ !~ /\./) || ($_ =~ /\.$/) || (($_ !~ /\.$/) && ($_ =~ /\.+/)) || (($_ =~ /\.$/) && ($_ =~ /\.+/)))

The third condition:
(($_ !~ /\.$/) && ($_ =~ /\.+/))

Is meant to check for files that look like this...
Hello.World

However, it will also find files with real extentions i.e.
File.txt
Therefore placing every single file under this condition (or one of the other 3) therefore every file is dumped into the No_Ext folder...

Q) How can I change this so that I am checking only if the file doesn't have an extension. I think it is impossible because files that do have an extension will look the same as this. I can't even split off the extension, because i.e. World will be counted as the extension etc.

I will have to work out how you did it Kevin.

Cheers
 
in my code, this line checks if the file has an extension:

Code:
   if ( $t =~ /\.([^.]+)$/ ) {

here it is in context:

Code:
   [b][red]   # get the file extension
   if ( $t =~ /\.([^.]+)$/ ) {[/red][/b]
      my $f = $1;
      # check it for suspicious characters
      if ($f =~ /^[a-zA-Z0-9_-]+$/) {
         # its OK
         push @{$types{$f}}, $t;
      }
      else {
         # it's suspicious
         push @{$types{SUSPECT}}, $t;
      }
   }
   else {
      [b][red]# no file extension[/red][/b]
      push @{$types{NOEXT}}, $t;
   }

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
My code = complete failure :/

The above code only processes one file at a time, so I have to keep refreshing for it to process every file. And all files are placed into the No_Ext folder. I think only the first if part of the code runs :s.

I thought it was perfect hehe

Chris,

This is called learning. Trial and error is a great way to learn, it can be frustrating, but you learn 100 ways how not to do something and (hopefully) a few ways how to do it.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Well, I had nothing else to do so here is what looks like a complete working script although there is no warranty implied. If anyone uses it they use it at their own risk.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Path[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Copy[/green][red];[/red]
[gray][i]# uncomment next line for debugging and sanity checks[/i][/gray]
[gray][i]#use Data::Dumper;[/i][/gray]

[gray][i]# the start directory[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [red]'[/red][purple]c:/dump[/purple][red]'[/red][red];[/red]

[gray][i]# a hash to store the filesnames[/i][/gray]
[gray][i]# the keys are the MIME type or file extenstion.[/i][/gray]
[gray][i]# NOEXT is for file with no extension[/i][/gray]
[gray][i]# SUSPECT is for file extensions with none word characters in them. Could be[/i][/gray]
[gray][i]# potentitally dangerous so just move these files into the SUSPECT folder.[/i][/gray]
[black][b]my[/b][/black] [blue]%types[/blue] = [red]([/red] [purple]NOEXT[/purple] => [red][[/red][red]][/red], [purple]SUSPECT[/purple] => [red][[/red][red]][/red] [red])[/red][red];[/red]    

[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [blue]$dir[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$t[/blue] = [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url] DIR[red])[/red] [red]{[/red]
   [gray][i]# we only want files[/i][/gray]
   [olive][b]next[/b][/olive] [olive][b]unless[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/-X.html][black][b]-f[/b][/black][/url] [red]"[/red][purple][blue]$dir[/blue]/[blue]$t[/blue][/purple][red]"[/red][red])[/red][red];[/red]
   [gray][i]# get the file extension[/i][/gray]
   [olive][b]if[/b][/olive] [red]([/red] [blue]$t[/blue] =~ [red]/[/red][purple][purple][b]\.[/b][/purple]([^.]+)$[/purple][red]/[/red] [red])[/red] [red]{[/red]
      [black][b]my[/b][/black] [blue]$f[/blue] = [blue]$1[/blue][red];[/red]
      [gray][i]# check it for suspicious characters[/i][/gray]
      [olive][b]if[/b][/olive] [red]([/red][blue]$f[/blue] =~ [red]/[/red][purple]^[a-zA-Z0-9_-]+$[/purple][red]/[/red][red])[/red] [red]{[/red]
         [gray][i]# its OK[/i][/gray]
         [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][blue]$f[/blue][red]}[/red][red]}[/red], [blue]$t[/blue][red];[/red]
      [red]}[/red]
      [olive][b]else[/b][/olive] [red]{[/red]
         [gray][i]# it's suspicious[/i][/gray]
         [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][purple]SUSPECT[/purple][red]}[/red][red]}[/red], [blue]$t[/blue][red];[/red]
      [red]}[/red]
   [red]}[/red]
   [olive][b]else[/b][/olive] [red]{[/red]
      [gray][i]# no file extension[/i][/gray]
      [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][purple]NOEXT[/purple][red]}[/red][red]}[/red], [blue]$t[/blue][red];[/red]
   [red]}[/red]
[red]}[/red]
[gray][i]# uncomment next line for sanity checks[/i][/gray]
[gray][i]#print Dumper \%types;[/i][/gray]

[gray][i]# make the folders and fill them with files.[/i][/gray]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$type[/blue] [red]([/red] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%types[/blue] [red])[/red] [red]{[/red]
   [gray][i]# make a folder[/i][/gray]
   [maroon]mkpath[/maroon][red]([/red] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue][/purple][red]"[/red], [red]{[/red][purple]verbose[/purple] => [fuchsia]1[/fuchsia][red]}[/red] [red])[/red] [b]or[/b]
     [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Could not create '[blue]$type[/blue]'. Might already exist.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [gray][i]# loop through hash[/i][/gray]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][blue]$type[/blue][red]}[/red][red]}[/red] [red])[/red] [red]{[/red]
      [gray][i]# check that file does not already exists[/i][/gray]
      [olive][b]unless[/b][/olive] [red]([/red] [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$file[/blue][/purple][red]"[/red] [red])[/red] [red]{[/red]      
         [maroon]move[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue]/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$file[/blue][/purple][red]"[/red][red])[/red] [b]or[/b]
           [black][b]print[/b][/black] [red]"[/red][purple]move failed [[blue]$dir[/blue]/[blue]$file[/blue], [blue]$dir[/blue]/[blue]$type[/blue]/[blue]$file[/blue]] : [blue]$![/blue][/purple][red]"[/red][red];[/red]
      [red]}[/red]
      [gray][i]# the file does exist so now we need to add the '_n'[/i][/gray]
      [gray][i]# to the end of the filename. So that means we have to check[/i][/gray]
      [gray][i]# for files already with '_n' appended to them.[/i][/gray]
      [olive][b]else[/b][/olive] [red]{[/red]
         [black][b]my[/b][/black] [blue]$n[/blue] = [fuchsia]1[/fuchsia][red];[/red]
         [maroon]LOOP[/maroon][maroon]:[/maroon] [red]{[/red]
            [olive][b]if[/b][/olive] [red]([/red] [black][b]-e[/b][/black] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$[/blue]{file}_[blue]$n[/blue][/purple][red]"[/red] [red])[/red] [red]{[/red]
               [blue]$n[/blue]++[red];[/red]
               [olive][b]redo[/b][/olive] LOOP[red];[/red]
            [red]}[/red]        
            [olive][b]else[/b][/olive] [red]{[/red]
               [maroon]move[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue]/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$[/blue]{file}_[blue]$n[/blue][/purple][red]"[/red][red])[/red] [b]or[/b]
                 [black][b]print[/b][/black] [red]"[/red][purple]move failed [[blue]$dir[/blue]/[blue]$file[/blue], [blue]$dir[/blue]/[blue]$type[/blue]/[blue]$[/blue]{file}_[blue]$n[/blue]] : [blue]$![/blue][/purple][red]"[/red][red];[/red]
            [red]}[/red]
         [red]}[/red]
      [red]}[/red]
   [red]}[/red]
[red]}[/red]

[black][b]print[/b][/black] [red]"[/red][purple]Finished processing files[/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Copy - Copy files or filehandles[/li]
[li]File::path - create or remove directory trees[/li]
[/ul]
[/tt]



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

I tested your code after changing a few bits to suit using it on a linux web server, however it didn't work properly for me...

2 folders were created...
NOEXT and
SUSPECT

And every file remained where it was, none of them were moved, nor were there unique directories created...

any idea why...

Here is my edited version of your code to suit the server

Code:
[gray]#! /usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]CGI[/green] [red]'[/red][purple]:standard[/purple][red]'[/red][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Path[/green][red];[/red]
[black][b]use[/b][/black] [green]File::Copy[/green][red];[/red]
[gray][i]# uncomment next line for debugging and sanity checks[/i][/gray]
[gray][i]#use Data::Dumper;[/i][/gray]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Content-type: text/html[purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]# the start directory[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$dir[/blue] = [red]'[/red][purple]ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/[/purple][red]'[/red][red];[/red]

[gray][i]# a hash to store the filesnames[/i][/gray]
[gray][i]# the keys are the MIME type or file extenstion.[/i][/gray]
[gray][i]# NOEXT is for file with no extension[/i][/gray]
[gray][i]# SUSPECT is for file extensions with none word characters in them. Could be[/i][/gray]
[gray][i]# potentitally dangerous so just move these files into the SUSPECT folder.[/i][/gray]
[black][b]my[/b][/black] [blue]%types[/blue] = [red]([/red] [purple]NOEXT[/purple] => [red][[/red][red]][/red], [purple]SUSPECT[/purple] => [red][[/red][red]][/red] [red])[/red][red];[/red]    

[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [blue]$dir[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$t[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red]![red]/[/red][purple]^[purple][b]\.[/b][/purple]{1,2}$[/purple][red]/[/red][red]}[/red] [url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url] DIR[red])[/red] [red]{[/red]
   [gray][i]# we only want files[/i][/gray]
   [olive][b]next[/b][/olive] [olive][b]unless[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/-X.html][black][b]-f[/b][/black][/url] [red]"[/red][purple][blue]$dir[/blue]/[blue]$t[/blue][/purple][red]"[/red][red])[/red][red];[/red]
   [gray][i]# get the file extension[/i][/gray]
   [olive][b]if[/b][/olive] [red]([/red] [blue]$t[/blue] =~ [red]/[/red][purple][purple][b]\.[/b][/purple]([^.]+)$[/purple][red]/[/red] [red])[/red] [red]{[/red]
      [black][b]my[/b][/black] [blue]$f[/blue] = [blue]$1[/blue][red];[/red]
      [gray][i]# check it for suspicious characters[/i][/gray]
      [olive][b]if[/b][/olive] [red]([/red][blue]$f[/blue] =~ [red]/[/red][purple]^[a-zA-Z0-9_-]+$[/purple][red]/[/red][red])[/red] [red]{[/red]
         [gray][i]# its OK[/i][/gray]
         [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][blue]$f[/blue][red]}[/red][red]}[/red], [blue]$t[/blue][red];[/red]
      [red]}[/red]
      [olive][b]else[/b][/olive] [red]{[/red]
         [gray][i]# it's suspicious[/i][/gray]
         [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][purple]SUSPECT[/purple][red]}[/red][red]}[/red], [blue]$t[/blue][red];[/red]
      [red]}[/red]
   [red]}[/red]
   [olive][b]else[/b][/olive] [red]{[/red]
      [gray][i]# no file extension[/i][/gray]
      [black][b]push[/b][/black] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][purple]NOEXT[/purple][red]}[/red][red]}[/red], [blue]$t[/blue][red];[/red]
   [red]}[/red]
[red]}[/red]
[gray][i]# uncomment next line for sanity checks[/i][/gray]
[gray][i]#print Dumper \%types;[/i][/gray]

[gray][i]# make the folders and fill them with files.[/i][/gray]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$type[/blue] [red]([/red] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%types[/blue] [red])[/red] [red]{[/red]
   [gray][i]# make a folder[/i][/gray]
   [maroon]mkpath[/maroon][red]([/red] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue][/purple][red]"[/red], [red]{[/red][purple]verbose[/purple] => [fuchsia]1[/fuchsia][red]}[/red] [red])[/red] or
     [black][b]print[/b][/black] [red]"[/red][purple]Could not create '[blue]$type[/blue]'. Might already exist.[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [gray][i]# loop through hash[/i][/gray]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red] [blue]@[/blue][red]{[/red][blue]$types[/blue][red]{[/red][blue]$type[/blue][red]}[/red][red]}[/red] [red])[/red] [red]{[/red]
      [gray][i]# check that file does not already exists[/i][/gray]
      [olive][b]unless[/b][/olive] [red]([/red] [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$file[/blue][/purple][red]"[/red] [red])[/red] [red]{[/red]      
         [maroon]move[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue]/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$file[/blue][/purple][red]"[/red][red])[/red] or
           [black][b]print[/b][/black] [red]"[/red][purple]move failed [[blue]$dir[/blue]/[blue]$file[/blue], [blue]$dir[/blue]/[blue]$type[/blue]/[blue]$file[/blue]] : [blue]$![/blue][/purple][red]"[/red][red];[/red]
      [red]}[/red]
      [gray][i]# the file does exist so now we need to add the '_n'[/i][/gray]
      [gray][i]# to the end of the filename. So that means we have to check[/i][/gray]
      [gray][i]# for files already with '_n' appended to them.[/i][/gray]
      [olive][b]else[/b][/olive] [red]{[/red]
         [black][b]my[/b][/black] [blue]$n[/blue] = [fuchsia]1[/fuchsia][red];[/red]
         [maroon]LOOP[/maroon][maroon]:[/maroon] [red]{[/red]
            [olive][b]if[/b][/olive] [red]([/red] [black][b]-e[/b][/black] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$[/blue]{file}_[blue]$n[/blue][/purple][red]"[/red] [red])[/red] [red]{[/red]
               [blue]$n[/blue]++[red];[/red]
               [olive][b]redo[/b][/olive] LOOP[red];[/red]
            [red]}[/red]        
            [olive][b]else[/b][/olive] [red]{[/red]
               [maroon]move[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue]/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$[/blue]{file}_[blue]$n[/blue][/purple][red]"[/red][red])[/red] or
                 [black][b]print[/b][/black] [red]"[/red][purple]move failed [[blue]$dir[/blue]/[blue]$file[/blue], [blue]$dir[/blue]/[blue]$type[/blue]/[blue]$[/blue]{file}_[blue]$n[/blue]] : [blue]$![/blue][/purple][red]"[/red][red];[/red]
            [red]}[/red]
         [red]}[/red]
      [red]}[/red]
   [red]}[/red]
[red]}[/red]

[black][b]print[/b][/black] [red]"[/red][purple]Finished processing files[/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[li]File::Copy - Copy files or filehandles[/li]
[li]File::path - create or remove directory trees[/li]
[/ul]
[/tt]

Chris
 
Remove the grep from the while loop condition:

while (my $t = grep {!/^\.{1,2}$/} readdir DIR) {

should be:

while (my $t = readdir DIR) {

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I understand now what you have done differently to me.

If a file contains "odd" characters (i.e. a .) then you move it to the SUSPECT file. As you said before, these can sometimes be dodgy files.

What I was trying to do was even if the file contained odd characters then I still wanted to move it to its correct directory.

Still checking through your code, not sure if you compensated for files that have just a dot at the end i.e. "file." and files that have nothing at the end i.e. "file".

Its good to see a well structured example
 
The code I posted will work on windows or linux/unix. There was no need to change anything except the start directory.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Aha, nice, I removed the grep and it worked fine...

Your script works exactly as mine does at the moment. The only issue I have with it is that (which I said is probably an impossible task) is to place files with . characters in but no extension nor a . at the end i.e. "Hello.World" into the no extensions folder. But it reads World as being the extension.

Why do you print this message...
Could not create 'NOEXT'. Might already exist. Could not create 'SUSPECT'. Might already exist. Finished processing files

Not that its a problem, just wondering why you decided to do that, and not warn if any of the extension directories are created.

Cheers, Chris

 
files that have just a dot at the end will be treated the same as having no extension. Files with nothing at the end will be put in the NOEXT folder. The funny files will be stuff like '.htaccess' (files that start with a dot and have no extension). I did not test for that but I am sure it will be treated as a file with an extension.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Why do you print this message...
Could not create 'NOEXT'. Might already exist. Could not create 'SUSPECT'. Might already exist. Finished processing files

Just so I could see something while the script was running. It does not have to be there. It can be removed or changed to something you prefer.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
When I ran your script, I tested using approximately 30 files all named in different ways. The files that had nothing at the end but contained a . i.e. "My.File" were put into the extension folder "File" because it reads that as its extension. I'm just being really picky hehe, trying to process every possible file name/extension. I'm just being stupid.

I also didn't test for files i.e. .htaccess. But I have a feeling my script will place them into the NO_EXT (no extensions folder) because they are counted as files that have no extension but contain a ".". I'm going to see.

Chris
 
I forgot to include: closedir DIR; at the end of the "while" loop.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm just being really picky hehe, trying to process every possible file name/extension.

File extensions are really just a convenience. There is no way to test for every possible name/extension. You can literally invent them on the fly. "Hello.World" is as valid as "frog.gif". Here is a list I use for common internet file types (for a file manager script):

Code:
%FT = (
   '.html'  => 'html',    '.htm'  => 'html',    '.shtm' => 'html',
   '.shtml' => 'html',    '.xml'  => 'html',

   '.gif'   => 'image',   '.jpg'  => 'image',   '.jpeg' => 'image',
   '.bmp'   => 'image',   '.art'  => 'image',   '.png'  => 'image',
	
   '.mpeg'  => 'movie',   '.mpg'  => 'movie',   '.mov'  => 'movie',
   '.avi'   => 'movie',   '.wmv'  => 'movie',   '.asf'  => 'movie',

   '.mid'   => 'sound',   '.midi' => 'sound',   '.wav'  => 'sound',
   '.ra'    => 'sound',   '.ram'  => 'sound',   '.mp3'  => 'sound',
   '.mpu'   => 'sound',

   '.tar'   => 'archive', '.zip'  => 'archive', '.gz'   => 'archive',
   '.ace'   => 'archive', '.rar'  => 'archive', '.cab'  => 'archive',
   '.bin'   => 'archive',

   '.txt'   => 'text',    '.dat'  => 'text',    '.css'  => 'text',
   '.js'    => 'text',    '.cgi'  => 'text',    '.pl'   => 'text',
   '.log'   => 'text',    '.php'  => 'text'
);

it's used to know which icon to place next to a file in the file list, like an apache tree directory.





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Just tested running files i.e. ".file"

Both our scripts create the folder named "file" because it reads that as being the extension. One thing to note though in your script if the file already exists (I checked this) your script overights the file. Mine checks if the file exists first. I haven't looked, and I know you aren't trying to achieve the most efficient file moving system in the world, but I assume you have missed renaming the file with "odd" file types. I haven't tested this for normal file types i.e. "file.txt"...

One thing I have noted for me is that in the occurence of a ".file" because of the way i rename files. it renames the file to i.e. "_064407141007.file" because of it thinking "file" is the extension.

Chris
 
Thats a good idea to create a list of common file types. That way you can also make sure no suspect file types are sorted.

Chris
 
My script should not overwrite any files, but I have not really tested it very much. It checks if a file exists first. If the file exists it then checks for 'file_n' files. I know that can be improved because it names files like:

frog.gif
frog.gif_1
frog.gif_2

instead of:

frog.gif
frog_1.gif
frog_2.gif

but I'm not interested in fixing that. If anyone wants to use the code they can make that change if they think it need to be changed.





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Well, every script has a purpose, and because we are creating this script with no real purpose the possiblities are endless and it could consistantly be changed. If there was a purpose, then there would be some sort of specification...

Before I go to sleep, I really want to fix my final problem. The fact that I have to keep refreshing the page until every file has been processed (it processes files in chunks). Its only been doing this the last couple of times I updated the code. This is an example output (the large gaps between statements are where I have refreshed the page).

Code:
TXT Hello.Big.World.txt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/txt/Hello.Big.World.txt

TXT Hello.World.txt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/txt/Hello.World.txt

NO_EXT Hello.WorldDotExt. has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/No_Ext/Hello.WorldDotExt.




WOLRDNOEXT Hello.WorldNoExt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/WorldNoExt/Hello.WorldNoExt

TXT Hello.txt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/txt/Hello.txt

NO_EXT HelloDotExt. has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/No_Ext/HelloDotExt.




NO_EXT HelloNoExt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/No_Ext/HelloNoExt




TXT javascript form issue.txt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/txt/javascript form issue.txt

TXT Test.txt has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/txt/Test.txt

DOC lit sur fin.doc has been moved to /ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/doc/lit sur fin.doc

3 files have been sorted

It was doing this in the last version of my code I posted, so if you need to just have a look.

Then thats it, i've finished with this task hehe. Move onto something else :)

Thanks for all your help though, I have been learning alot.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top