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 - Continued

Status
Not open for further replies.

KevinADC

Technical User
Jan 21, 2005
5,070
US
OK I went ahead and fixed the file renamimg scheme for repeated files:

file.txt
file_1.txt
file_2.txt

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]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]
[black][b]use[/b][/black] [green]CGI::Carp[/green] [red]qw/[/red][purple]fatalsToBrowser[/purple][red]/[/red][red];[/red]
[blue]$|[/blue]=[fuchsia]1[/fuchsia][red];[/red]
[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]
[black][b]print[/b][/black] [red]'[/red][purple]<plaintext>[/purple][red]'[/red][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]/home/e-pixsco/public_html/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 non-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]
[maroon]MAINLOOP[/maroon][maroon]:[/maroon] [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]
         [olive][b]next[/b][/olive] MAINLOOP [olive][b]if[/b][/olive] [red]([/red][blue]$type[/blue] eq [red]'[/red][purple]NOEXT[/purple][red]'[/red][red])[/red][red];[/red]
         [black][b]my[/b][/black] [blue]$n[/blue] = [fuchsia]1[/fuchsia][red];[/red]
         [black][b]my[/b][/black] [blue]$newfile[/blue][red];[/red]
         [maroon]LOOP[/maroon][maroon]:[/maroon] [red]{[/red]
            [blue]$newfile[/blue] = [maroon]filename_new[/maroon][red]([/red][blue]$file[/blue],[blue]$n[/blue][red])[/red][red];[/red]
            [olive][b]if[/b][/olive] [red]([/red] [black][b]-e[/b][/black] [red]"[/red][purple][blue]$dir[/blue]/[blue]$type[/blue]/[blue]$newfile[/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]$newfile[/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]$newfile[/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]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]filename_new[/maroon] [red]{[/red]
   [url=http://perldoc.perl.org/functions/no.html][black][b]no[/b][/black][/url] [green]warnings[/green][red];[/red]
   [black][b]my[/b][/black] [blue]$t[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url] or [url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [fuchsia]0[/fuchsia][red];[/red]
   [black][b]my[/b][/black] [blue]$n[/blue] = [black][b]shift[/b][/black] or [black][b]return[/b][/black] [fuchsia]0[/fuchsia][red];[/red]
   [black][b]my[/b][/black] [red]([/red][blue]$name[/blue], [blue]$ext[/blue][red])[/red] = [blue]$t[/blue] =~ [red]/[/red][purple](.+)[purple][b]\.[/b][/purple]([^.]+)$[/purple][red]/[/red][red];[/red]
   [black][b]return[/b][/black][red]([/red][red]"[/red][purple][blue]$[/blue]{name}_[blue]$n[/blue].[blue]$ext[/blue][/purple][red]"[/red][red])[/red][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]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI::Carp - CGI routines for writing to the HTTPD (or other) error log[/li]
[li]File::Copy - Copy files or filehandles[/li]
[li]File::path - create or remove directory trees[/li]
[/ul]
[/tt]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Good idea. The tiny scroll bar was a pain :).

Thats your script sorted then! I'm going to check again about renaming files i.e. .File
As I said before I thought it overwrote, but I probably didn't test properly, because your script is efficient whilst I pretty much separated processing odd file types with processing normal file types with extensions, therefore I have to repeat tasks for both file types :s. Its shameful to see how well structured and coded your script is compared to mine, and it seems like with little effort you produce an efficient system with minimal coding to produce logical results.

Did you figure out why my script was processing files in chunks?

Chris
 
Your latest script also seems to working fine now. Good job [smile].

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Did you figure out why my script was processing files in chunks?

Not really. I suspect it was one of the "unless" loops but I didn't check.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
actually this should be removed in my scrpt:

Code:
         next MAINLOOP if ($type eq 'NOEXT');

not sure what I was thinking, but it's pretty late and this has been a marathon session. [yawn]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you,

You were right, it isn't overighting a file named ".file". However, I ran the script 3 times with the file ".file" to see how it would rename.
The 1st time a folder was created called file (which is expected), and .file moved into there.
The 2nd time ".file" was renamed to "_1." This isn't a problem (atleast its being renamed) but its not been identified as being a renamed file from ".file"
The 3rd time, "_2.".

I then decided to see what would happen if I tried to process the file "_1.". Nothing happened, so I assumed it overwrote "_1." instead of renaming it to "_1_1.". Not entirely sure.

Ill take a look at my loops to see what might be going wrong in my script. It would be nice if every file could be processed in one run :).

Chris

 
Haha, yeah, I do love exchanging suggestions, ideas and criticisms :p. Don't ever get me wrong though, I will never insult your expertise, I love to test things fully! Its late there huh? Well its early here now (08:49 am), i've stayed up all night.
 
Well, it is hard to insult me, I don't take this stuff too seriously and I know I'm far from being a perl guru. I also enjoy exchanging ideas and suggestions.

It's 1:00 AM here (California USA). I'll catch ya later Chris. Good-night.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Argh, I just wrote a long message, posted it and recieved an ERROR, so I lost it. But basically you were right it was an unless loop causing the chunks of files to be processed because I changed the $_'s value in the first if statement.

Thank you for all your help! And goodnight!

Code:
[gray][i]#############################################################################################[/i][/gray]
[gray][i]## Tek-tips.com Topic Response                                                             ##[/i][/gray]
[gray][i]## [URL unfurl="true"]http://tek-tips.com/viewthread.cfm?qid=1416792&page=1[/URL]                                   ##[/i][/gray]
[gray][i]##                                                                                         ##[/i][/gray]
[gray][i]## - Sort files with different extensions into corresponding directories.                  ##[/i][/gray]
[gray][i]## - The corresponding directories are children of the files folder.                       ##[/i][/gray]
[gray][i]## - If a file has no extension (i.e. file OR file.) then place into the No_Ext directory. ##[/i][/gray]
[gray][i]## - Create the extension directories automatically, unless one already exists.            ##[/i][/gray]
[gray][i]## - If a file name already exists, then rename it with a unique new name.                 ##[/i][/gray]
[gray][i]#############################################################################################[/i][/gray]

[gray][i]#############################################################################################[/i][/gray]
[gray][i]## Please Note:                                                                            ##[/i][/gray]
[gray][i]##                                                                                         ##[/i][/gray]
[gray][i]## If your file has no extension but contains .'s (i.e. "My.File") then the string after   ##[/i][/gray]
[gray][i]## the first . will be counted as the extention (i.e. the file will be called "My" and its ##[/i][/gray]
[gray][i]## extension will be "File" (or ".File").                                                  ##[/i][/gray]
[gray][i]#############################################################################################[/i][/gray]

[gray][i]#############################################################################################[/i][/gray]
[gray][i]## Problems:                                                                               ##[/i][/gray]
[gray][i]##                                                                                         ##[/i][/gray]
[gray][i]## - Not all files are processed together, instead one or a few at a time. Need to find    ##[/i][/gray]
[gray][i]##   out why this is happening. For now, you must keep refreshing until message "0 files   ##[/i][/gray]
[gray][i]##   have been sorted" appears.                                                            ##[/i][/gray]
[gray][i]## - Haven't accounted for files i.e. ".File", therefore its moved into an extension       ##[/i][/gray]
[gray][i]##   folder named "File". If the file exists then it is renamed i.e. _001122001122.File.   ##[/i][/gray]
[gray][i]## - I could use a number to rename files i.e. "File_n.txt" It takes the _n from the file  ##[/i][/gray]
[gray][i]##   that already exists and adds 1 to it, to create the next number.                      ##[/i][/gray]
[gray][i]## - If I processed 2 files with the same name during one session, then it is likely the   ##[/i][/gray]
[gray][i]##   script will take less than a second to process the files, therefore they will be      ##[/i][/gray]
[gray][i]##   given the same time/date stamp.                                                       ##[/i][/gray]
[gray][i]#############################################################################################[/i][/gray]

[gray][i]###############[/i][/gray]
[gray][i]#! /usr/bin/perl[/i][/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]
[gray][i]###############[/i][/gray]

[gray][i]###############[/i][/gray]
[gray][i]##### DECLARE VARIABLES[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]$time_date_stamp[/blue], [blue]$path[/blue], [blue]$no_ext_dump[/blue], [blue]$fullstop[/blue], [blue]$underslash[/blue], [blue]@directory_contents[/blue], [blue]$counter[/blue], [blue]$file_name[/blue], [blue]@split_string[/blue], [blue]$last_fullstop[/blue], [blue]$new_name[/blue], [blue]@split_name_ext[/blue], [blue]$name[/blue], [blue]$ext[/blue][red])[/red][red];[/red]
[gray][i]##### GET TIME/DATE STAMP[/i][/gray]
[black][b]my[/b][/black] [red]([/red][blue]$sec[/blue], [blue]$min[/blue], [blue]$hour[/blue], [blue]$mday[/blue], [blue]$mon[/blue], [blue]$year[/blue][red])[/red]=[url=http://perldoc.perl.org/functions/gmtime.html][black][b]gmtime[/b][/black][/url][red];[/red]
[blue]$time_date_stamp[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]([/red][red]'[/red][purple]%02d%02d%02d%02d%02d%02d[/purple][red]'[/red], [blue]$hour[/blue], [blue]$min[/blue], [blue]$sec[/blue], [blue]$mday[/blue], [blue]$mon[/blue], [blue]$year[/blue][red])[/red][red];[/red]
[gray][i]##### DECLARE CONTENT TYPE[/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]##### INITIAL VALUES[/i][/gray]
[blue]$path[/blue] = [red]"[/red][purple]/kunden/homepages/4/d160423788/htdocs/ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/[/purple][red]"[/red][red];[/red]
[blue]$no_ext_dump[/blue] = [red]"[/red][purple]No_Ext[/purple][red]"[/red][red];[/red]
[blue]$fullstop[/blue] = [red]"[/red][purple].[/purple][red]"[/red][red];[/red]
[blue]$underslash[/blue] = [red]"[/red][purple]_[/purple][red]"[/red][red];[/red]
[gray][i]##### START PROCESS COUNTER ON 0[/i][/gray]
[blue]$counter[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[gray][i]##### OPEN PATH DIRECTORY AND GET CONTENTS[/i][/gray]
[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url] [red]([/red]FILE, [red]"[/red][purple][blue]$path[/blue][/purple][red]"[/red][red])[/red] || [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Cannot Open: [blue]$path[/blue][/purple][red]"[/red][red];[/red]
[blue]@directory_contents[/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] [red]([/red]FILE[red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url] [red]([/red]FILE[red])[/red][red];[/red]
[gray][i]###############[/i][/gray]

[gray][i]###############[/i][/gray]
[gray][i]##### MAIN LOOP[/i][/gray]
[olive][b]foreach[/b][/olive] [red]([/red][blue]@directory_contents[/blue][red])[/red] [red]{[/red]
     [olive][b]if[/b][/olive] [red]([/red][red]([/red][blue]$_[/blue] !~ [red]/[/red][purple][purple][b]\.[/b][/purple][/purple][red]/[/red][red])[/red] || [red]([/red][blue]$_[/blue] =~ [red]/[/red][purple][purple][b]\.[/b][/purple]$[/purple][red]/[/red][red])[/red] || [red]([/red][red]([/red][blue]$_[/blue] =~ [red]/[/red][purple][purple][b]\.[/b][/purple]$[/purple][red]/[/red][red])[/red] && [red]([/red][blue]$_[/blue] =~ [red]/[/red][purple][purple][b]\.[/b][/purple]+[/purple][red]/[/red][red])[/red][red])[/red][red])[/red] [red]{[/red]
          [olive][b]unless[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/-X.html][black][b]-d[/b][/black][/url] [red]"[/red][purple][blue]$path[/blue][blue]$_[/blue][/purple][red]"[/red][red])[/red] [red]{[/red]
               [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]$path[/blue][blue]$no_ext_dump[/blue][/purple][red]"[/red][red])[/red] [red]{[/red]
                    [url=http://perldoc.perl.org/functions/mkdir.html][black][b]mkdir[/b][/black][/url] [red]([/red][red]"[/red][purple][blue]$path[/blue][blue]$no_ext_dump[/blue][/purple][red]"[/red], [fuchsia]0777[/fuchsia][red])[/red] || [black][b]die[/b][/black] [red]"[/red][purple]Cannot Open: [blue]$path[/blue][blue]$no_ext_dump[/blue][/purple][red]"[/red][red];[/red]
               [red]}[/red]
               [blue]$file_name[/blue] = [blue]$_[/blue][red];[/red]
               [olive][b]if[/b][/olive] [red]([/red][black][b]-e[/b][/black] [red]"[/red][purple][blue]$path[/blue][blue]$no_ext_dump[/blue]/[blue]$_[/blue][/purple][red]"[/red][red])[/red] [red]{[/red]
                    [blue]@split_string[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][/purple][red]/[/red], [blue]$_[/blue][red])[/red][red];[/red]
                    [olive][b]if[/b][/olive] [red]([/red][blue]$split_string[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red] eq [red]"[/red][purple].[/purple][red]"[/red][red])[/red] [red]{[/red]
                         [url=http://perldoc.perl.org/functions/chop.html][black][b]chop[/b][/black][/url] [red]([/red][blue]@split_string[/blue][red])[/red][red];[/red]
                         [blue]$_[/blue] = [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]([/red][red]"[/red][purple][/purple][red]"[/red], [blue]@split_string[/blue][red])[/red][red];[/red]
                         [blue]$last_fullstop[/blue] = [red]"[/red][purple].[/purple][red]"[/red][red];[/red]
                    [red]}[/red]
                    [olive][b]else[/b][/olive] [red]{[/red]
                         [blue]$last_fullstop[/blue] = [red]"[/red][purple][/purple][red]"[/red][red];[/red]
                    [red]}[/red]
                    [blue]$new_name[/blue] = [blue]$_[/blue] . [blue]$underslash[/blue] . [blue]$time_date_stamp[/blue] . [blue]$last_fullstop[/blue][red];[/red]
               [red]}[/red]
               [url=http://perldoc.perl.org/functions/rename.html][black][b]rename[/b][/black][/url] [red]([/red][red]"[/red][purple][blue]$path[/blue][blue]$file_name[/blue][/purple][red]"[/red], [red]"[/red][purple][blue]$path[/blue][blue]$no_ext_dump[/blue]/[blue]$new_name[/blue][/purple][red]"[/red][red])[/red] || [black][b]die[/b][/black] [red]"[/red][purple]Cannot Open: [blue]$path[/blue][blue]$_[/blue] - [blue]$path[/blue][blue]$no_ext_dump[/blue]/[blue]$new_name[/blue][/purple][red]"[/red][red];[/red]
               [black][b]print[/b][/black] [red]"[/red][purple]<p>[blue]$_[/blue] has been moved to [blue]$path[/blue][blue]$no_ext_dump[/blue]/[blue]$_[/blue][/purple][red]"[/red][red];[/red]
               [blue]$counter[/blue]++
          [red]}[/red]
     [red]}[/red]
     [olive][b]else[/b][/olive] [red]{[/red]
          [blue]@split_name_ext[/blue] = [black][b]split[/b][/black] [red]([/red][red]/[/red][purple][purple][b]\.[/b][/purple][/purple][red]/[/red], [blue]$_[/blue][red])[/red][red];[/red]
          [blue]$ext[/blue] = [blue]$split_name_ext[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red][red];[/red]
          [url=http://perldoc.perl.org/functions/pop.html][black][b]pop[/b][/black][/url] [red]([/red][blue]@split_name_ext[/blue][red])[/red][red];[/red]
          [blue]$name[/blue] = [black][b]join[/b][/black] [red]([/red][red]"[/red][purple].[/purple][red]"[/red], [blue]@split_name_ext[/blue][red])[/red][red];[/red]
          [olive][b]unless[/b][/olive] [red]([/red][black][b]-e[/b][/black] [red]"[/red][purple][blue]$path[/blue][blue]$ext[/blue][/purple][red]"[/red][red])[/red] [red]{[/red]
               [black][b]mkdir[/b][/black] [red]([/red][red]"[/red][purple][blue]$path[/blue][blue]$ext[/blue][/purple][red]"[/red], [fuchsia]0777[/fuchsia][red])[/red] || [black][b]die[/b][/black] [red]"[/red][purple]Cannot Open: [blue]$path[/blue][blue]$ext[/blue][/purple][red]"[/red][red];[/red]
          [red]}[/red]
     [red]}[/red]
     [olive][b]unless[/b][/olive] [red]([/red][black][b]-d[/b][/black] [red]"[/red][purple][blue]$path[/blue][blue]$_[/blue][/purple][red]"[/red][red])[/red] [red]{[/red]
          [blue]$file_name[/blue] = [blue]$_[/blue][red];[/red]
          [olive][b]if[/b][/olive] [red]([/red][black][b]-e[/b][/black] [red]"[/red][purple][blue]$path[/blue][blue]$ext[/blue]/[blue]$_[/blue][/purple][red]"[/red][red])[/red] [red]{[/red]
               [blue]$_[/blue] = [blue]$name[/blue] . [blue]$underslash[/blue] . [blue]$time_date_stamp[/blue] . [blue]$fullstop[/blue] . [blue]$ext[/blue][red];[/red]
          [red]}[/red]
          [black][b]rename[/b][/black] [red]([/red][red]"[/red][purple][blue]$path[/blue][blue]$file_name[/blue][/purple][red]"[/red], [red]"[/red][purple][blue]$path[/blue][blue]$ext[/blue]/[blue]$_[/blue][/purple][red]"[/red][red])[/red] || [black][b]die[/b][/black] [red]"[/red][purple]Cannot Open: [blue]$path[/blue][blue]$_[/blue] - [blue]$path[/blue][blue]$ext[/blue]/[blue]$_[/blue][/purple][red]"[/red][red];[/red]
          [black][b]print[/b][/black] [red]"[/red][purple]<p>[blue]$_[/blue] has been moved to [blue]$path[/blue][blue]$ext[/blue]/[blue]$_[/blue][/purple][red]"[/red][red];[/red]
          [blue]$counter[/blue]++
     [red]}[/red]
[red]}[/red]
[gray][i]##### MAIN LOOP ENDED[/i][/gray]
[gray][i]###############[/i][/gray]

[gray][i]###############[/i][/gray]
[gray][i]##### FINAL PRINT STATEMENT[/i][/gray]
[black][b]print[/b][/black] [red]"[/red][purple]<p>[blue]$counter[/blue] files have been sorted[/purple][red]"[/red][red];[/red]
[gray][i]###############[/i][/gray]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[/ul]
[/tt]
 
When I was talking about ".file" files being renamed to _1 in your script Kevin.

Was wondering if you would just change the code in the bottom subroutine to something like...

Code:
sub filename_new {
   no warnings;
   my $t = shift or return 0;
   my $n = shift or return 0;
   if ($file =~ (/\.\b/) {
        my ($name, $ext) = $t =~ /FORMAT PATTERN/;
   }
   else {
        my ($name, $ext) = $t =~ /(.+)\.([^.]+)$/;
   }
   return("${name}_$n.$ext");
}

Chris
 
Sorry,

Possibly this, formats are probably wrong (i'm only just starting to use them for the first time):

Code:
sub filename_new {
   no warnings;
   my $t = shift or return 0;
   my $n = shift or return 0;
   if ($file =~ (/(^\.[^.]+)$/) {
        my ($name, $ext) = $t =~ /(\.)([.*])$/;
   }
   else {
        my ($name, $ext) = $t =~ /(.+)\.([^.]+)$/;
   }
   return("${name}_$n.$ext");
}

Chris
 
feel free to change whatever you like Chris. Your code above will fail because $name and $ext are scoped wrong.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top