[gray]#!/bin/bash/perl -w[/gray]
[gray][i]# This is a rename and move script. The goal is to rename files then move them to their[/i][/gray]
[gray][i]# appropriate folders, and then e-mail (hopefully) the files to pre-defined people.[/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]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]diagnostics[/green][red];[/red]
[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Basename[/green] [red]qw([/red][purple]fileparse[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Copy[/green] [red]qw([/red][purple]move[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]File::Path[/green] [red]qw([/red][purple]mkpath[/purple][red])[/red][red];[/red]
[gray][i]# Today's Date[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@today[/blue] = [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red];[/red]
[gray][i]# Fiscal Year Folders: Reports 2006-7[/i][/gray]
[black][b]my[/b][/black] [blue]$yearFolder[/blue] = [url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]{[/red]
[black][b]my[/b][/black] [blue]$y[/blue] = strftime [red]"[/red][purple]%Y[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
[black][b]my[/b][/black] [red]([/red][blue]$y1[/blue], [blue]$y2[/blue][red])[/red] = [blue]$today[/blue][red][[/red][fuchsia]4[/fuchsia][red]][/red] < [fuchsia]6[/fuchsia] ? [red]([/red][blue]$y[/blue]-[fuchsia]1[/fuchsia], [blue]$y[/blue][red])[/red] : [red]([/red][blue]$y[/blue], [blue]$y[/blue]+[fuchsia]1[/fuchsia][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$i[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]while[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/substr.html][black][b]substr[/b][/black][/url][red]([/red][blue]$y1[/blue],[blue]$i[/blue],[fuchsia]1[/fuchsia][red])[/red] eq [black][b]substr[/b][/black][red]([/red][blue]$y2[/blue],[blue]$i[/blue],[fuchsia]1[/fuchsia][red])[/red][red])[/red] [red]{[/red] [blue]$i[/blue]++ [red]}[/red]
[blue]$y2[/blue] = [black][b]substr[/b][/black] [blue]$y2[/blue], [blue]$i[/blue][red];[/red]
[red]"[/red][purple]Reports [blue]$y1[/blue]-[blue]$y2[/blue][/purple][red]"[/red][red];[/red]
[red]}[/red][red];[/red]
[gray][i]# Fiscal Month: Jan=07 .. Jun=12, Jul=01 .. Dec=06[/i][/gray]
[black][b]my[/b][/black] [blue]$fm[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]"[/red][purple]%02d[/purple][red]"[/red], [red]([/red][fuchsia]1[/fuchsia] + [red]([/red][red]([/red][blue]$today[/blue][red][[/red][fuchsia]4[/fuchsia][red]][/red] + [fuchsia]6[/fuchsia][red])[/red] [blue]%[/blue] [fuchsia]12[/fuchsia][red])[/red][red])[/red][red];[/red]
[gray][i]# Fiscal Month Folders : FM Mon YY example: 11 May 07[/i][/gray]
[black][b]my[/b][/black] [blue]$fmFolder[/blue] = strftime [red]"[/red][purple][blue]$fm[/blue] %b %y[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
[gray][i]# Destination Folder Based off of Year and Fiscal Month[/i][/gray]
[black][b]my[/b][/black] [blue]$dest[/blue] = [red]"[/red][purple]/UCSC/Utilities/Utility Management/UtilityOps/Recharges/Faser Reports/[blue]$yearFolder[/blue]/[blue]$fmFolder[/blue][/purple][red]"[/red][red];[/red]
[maroon]mkpath[/maroon][red]([/red][blue]$dest[/blue][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$src[/blue] = [red]"[/red][purple]/UCSC/WinXP_C/Documents and Settings/mcdonaldj.FACILITIES/Local Settings/Temp[/purple][red]"[/red][red];[/red]
[gray][i]# Move files.[/i][/gray]
[maroon]moveFile[/maroon][red]([/red][red]"[/red][purple][blue]$src[/blue]/1a-fac housing reads.xls[/purple][red]"[/red], [red]"[/red][purple][blue]$dest[/blue]/[blue]$fm[/blue]-fac housing reads.xls[/purple][red]"[/red][red])[/red][red];[/red]
[maroon]moveFile[/maroon][red]([/red][red]"[/red][purple][blue]$src[/blue]/1b-fac housing usages.xls[/purple][red]"[/red], [red]"[/red][purple][blue]$dest[/blue]/[blue]$fm[/blue]-fac housing usages.xls[/purple][red]"[/red][red])[/red][red];[/red]
[gray][i]# Move file to Unique Destination[/i][/gray]
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]moveFile[/maroon] [red]{[/red]
[black][b]my[/b][/black] [red]([/red][blue]$from[/blue], [blue]$to[/blue][red])[/red] = [blue]@_[/blue][red];[/red]
[gray][i]# File doesn't exist.[/i][/gray]
[url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [olive][b]if[/b][/olive] ! [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [blue]$from[/blue][red];[/red]
[gray][i]# Create Unique Destination[/i][/gray]
[gray][i]# File names with REV#'s : 11-fac housing reads REV-1.xls[/i][/gray]
[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [red]([/red][blue]$name[/blue], [blue]$path[/blue], [blue]$suffix[/blue], [blue]$i[/blue][red])[/red] = [maroon]fileparse[/maroon][red]([/red][blue]$to[/blue], [red]qr/[/red][purple][purple][b]\.[/b][/purple][^.]*[/purple][red]/[/red][red])[/red][red];[/red] [black][b]-e[/b][/black] [blue]$to[/blue][red];[/red][red])[/red] [red]{[/red]
[blue]$to[/blue] = [blue]$path[/blue] . [blue]$name[/blue] . [red]'[/red][purple] REV-[/purple][red]'[/red] . ++[blue]$i[/blue] . [blue]$suffix[/blue][red];[/red]
[red]}[/red]
[maroon]move[/maroon][red]([/red][blue]$from[/blue], [blue]$to[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Move failed: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]
[fuchsia]1[/fuchsia][red];[/red]
[teal]__END__[/teal]
[teal]# Description Given. Not followed, but adapted to get the true meaning.[/teal]
[teal]Get month from system[/teal]
[teal]Convert to 2 digit Fiscal month ID[/teal]
[teal]Open Destination directory[/teal]
[teal] Based on Fiscal Month ID --> test for appropriate Fiscal Year Folder[/teal]
[teal] If folder exists[/teal]
[teal] drill down into folder [/teal]
[teal] test for Fiscal Month Folder[/teal]
[teal] If Fiscal Month Folder exists[/teal]
[teal] drill into folder[/teal]
[teal] test for file existence[/teal]
[teal] If File true[/teal]
[teal] check for multiple copies[/teal]
[teal] If Multiple copies[/teal]
[teal] Create “.REV-#” copy +1[/teal]
[teal] Else[/teal]
[teal] Create “.REV-1”[/teal]
[teal] End If[/teal]
[teal] Else[/teal]
[teal] copy files from source directory[/teal]
[teal] End If[/teal]
[teal] Else[/teal]
[teal] create folder[/teal]
[teal] copy files from source directory into folder[/teal]
[teal] End If[/teal]
[teal] Else[/teal]
[teal] create fiscal year folder and first Fiscal Month Folder[/teal]
[teal] End If[/teal]
[teal]Delete source files from source directory[/teal]