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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy works for one file but not the second. 1

Status
Not open for further replies.

MkIIISupra

Programmer
Apr 17, 2002
108
US
Code:
#!/bin/bash/perl -w
# This is a rename and move script. The goal is to rename files then move them to their
# appropriate folders, and then e-mail (hopefully) the files to pre-defined people.
use warnings;
use strict;
use diagnostics;
use Time::Local;
use File::Copy;

my $date = `date`; # Load $date with system date.
chomp $date; # Clear any trailing spaces
my $mMonth = substr ($date, 4, 3); # Move month into mMonth to be used to perform renames and moves.

# This If statement converts mMonth to the Fiscal month
# Jan = 07 / Feb = 08...
if ($mMonth eq "Jan")
	{
		$mMonth = "07";
	}
elsif ($mMonth eq "Feb")
	{
		$mMonth = "08";
	}
elsif ($mMonth eq "Mar")
	{
		$mMonth = "09";
	}
elsif ($mMonth eq "Apr")
	{
		$mMonth = "10";
	}
elsif ($mMonth eq "May")
	{
		$mMonth = "11";
	}
elsif ($mMonth eq "Jun")
	{
		$mMonth = "12";
	}
elsif ($mMonth eq "Jul")
	{
		$mMonth = "01";
	}
elsif ($mMonth eq "Aug")
	{
		$mMonth = "02";
	}
elsif ($mMonth eq "Sep")
	{
		$mMonth = "03";
	}
elsif ($mMonth eq "Oct")
	{
		$mMonth = "04";
	}
elsif ($mMonth eq "Nov")
	{
		$mMonth = "05";
	}
elsif ($mMonth eq "Dec")
	{
		$mMonth = "06";
	}
else
	{
		print "Oh HELP ME! The date function failed! \n";
	}

my $source = "";
my $destination = "";

opendir DH,"/UCSC/WinXP_C/Documents and Settings/mcdonaldj.FACILITIES/Local Settings/Temp" or die "Couldn't open /UCSC/WinXP_C/Documents and Settings/mcdonaldj.FACILITIES/Local Settings/Temp : $!";

while($_ = readdir(DH)){
	if ($_ eq "1a-fac housing reads.xls"){

		$source = "/UCSC/WinXP_C/Documents and Settings/mcdonaldj.FACILITIES/Local Settings/Temp/1a-fac housing reads.xls";
		$destination = "/UCSC/MONTHLY DROP/$mMonth-fac housing reads.xls";

		open IN, $source or die "Can't get the file $source, did you run it? : $!\n";
		open OUT, "> $destination" or die "Can't write to $destination: $!\n";

		while(<IN>){
			print OUT $_;
		}
	}
	elsif ($_ eq "1b-fac housing usages.xls"){

		$source = "/UCSC/WinXP_C/Documents and Settings/mcdonaldj.FACILITIES/Local Settings/Temp/1b-fac housing usages.xls";
		$destination = "/UCSC/MONTHLY DROP/$mMonth-fac housing reads.xls";

		open IN, $source or die "Can't get the file $source, did you run it? : $!\n";
		open OUT, "> $destination" or die "Can't write to $destination: $!\n";

		while(<IN>){
			print OUT $_;
		}
	}
}

Okay I got this to copy the first file and rename it in the process. But the second file doesn't get touched. I thought the while statement would reload and retest each file instance but it doesn't appear that way... or I am doing it incorrectly. What am I missing?

One by one the penguins return my sanity, as day by day Microsoft steals my sanity!

OpenSuSE 10.0 kicks fanny!
 
Hi Dude,

I took a moment to compress your script a little. The problem is that you're moving those two files to the same destination. Therefore, they probably are both being moved, but one is overwriting the other.

Your script could be simplified even more than I've already done, but this should at least help you fix your bug.

Code:
[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]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]diagnostics[/green][red];[/red]

[black][b]use[/b][/black] [green]File::Copy[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$date[/blue] = [red]`[/red][purple]date[/purple][red]`[/red][red];[/red] [gray][i]# Load $date with system date.[/i][/gray]
[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [blue]$date[/blue][red];[/red] [gray][i]# Clear any trailing spaces[/i][/gray]
[black][b]my[/b][/black] [blue]$mMonth[/blue] = [url=http://perldoc.perl.org/functions/substr.html][black][b]substr[/b][/black][/url] [red]([/red][blue]$date[/blue], [fuchsia]4[/fuchsia], [fuchsia]3[/fuchsia][red])[/red][red];[/red] [gray][i]# Move month into mMonth to be used to perform renames and moves.[/i][/gray]

[gray][i]# This If statement converts mMonth to the Fiscal month[/i][/gray]
[gray][i]# Jan = 07 / Feb = 08...[/i][/gray]
[black][b]my[/b][/black] [blue]%months[/blue] = [red]([/red]
	[purple]Jan[/purple]	=> [red]'[/red][purple]07[/purple][red]'[/red],
	[purple]Feb[/purple]	=> [red]'[/red][purple]08[/purple][red]'[/red],
	[purple]Mar[/purple]	=> [red]'[/red][purple]09[/purple][red]'[/red],
	[purple]Apr[/purple]	=> [red]'[/red][purple]10[/purple][red]'[/red],
	[purple]May[/purple]	=> [red]'[/red][purple]11[/purple][red]'[/red],
	[purple]Jun[/purple]	=> [red]'[/red][purple]12[/purple][red]'[/red],
	[purple]Jul[/purple]	=> [red]'[/red][purple]01[/purple][red]'[/red],
	[purple]Aug[/purple]	=> [red]'[/red][purple]02[/purple][red]'[/red],
	[purple]Sep[/purple]	=> [red]'[/red][purple]03[/purple][red]'[/red],
	[purple]Oct[/purple]	=> [red]'[/red][purple]04[/purple][red]'[/red],
	[purple]Nov[/purple]	=> [red]'[/red][purple]05[/purple][red]'[/red],
	[purple]Dec[/purple]	=> [red]'[/red][purple]06[/purple][red]'[/red],
[red])[/red][red];[/red]

[blue]$mMonth[/blue] = [blue]$months[/blue][red]{[/red][blue]$mMonth[/blue][red]}[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Oh HELP ME! The date function failed: [blue]$mMonth[/blue]![/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$dir[/blue] = [red]"[/red][purple]/UCSC/WinXP_C/Documents and Settings/mcdonaldj.FACILITIES/Local Settings/Temp[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DH, [blue]$dir[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$dir[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$file[/blue] [red]([/red][url=http://perldoc.perl.org/functions/readdir.html][black][b]readdir[/b][/black][/url][red]([/red]DH[red])[/red][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$file[/blue] eq [red]"[/red][purple]1a-fac housing reads.xls[/purple][red]"[/red][red])[/red][red]{[/red]
		[maroon]copy[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue]/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple]/UCSC/MONTHLY DROP/[blue]$mMonth[/blue]-fac housing reads.xls[/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Copy failed: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][blue]$file[/blue] eq [red]"[/red][purple]1b-fac housing usages.xls[/purple][red]"[/red][red])[/red][red]{[/red]
		[maroon]copy[/maroon][red]([/red][red]"[/red][purple][blue]$dir[/blue]/[blue]$file[/blue][/purple][red]"[/red], [red]"[/red][purple]/UCSC/MONTHLY DROP/[blue]$mMonth[/blue]-fac housing reads.xls[/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Copy failed: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red]DH[red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]diagnostics - Produce verbose warning diagnostics[/li]
[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]
[/ul]
[/tt]

Go slugs!
- Miller
 
Man that cleaned a lot up! Thanks! I am still learning Perl and have only been seriously working with it for about a week. Unfortunately I have been working with VBA for the last couple year, but now I have this killer job at UCSC, I am allowed a lot of freedom. As in I can choose my language and build my own tools.

Something I have wanted to do for so long! I also am allowed to use a 100% Open Source solution, including my desktop of choice, Linux. SO it's a learning period for the next 6 months. Learning to program clean again! Oy, thank you very much for the help! Of course I will be back with another issue I am sure!

One by one the penguins return my sanity, as day by day Microsoft steals my sanity!

OpenSuSE 10.0 kicks fanny!
 
Hehehe, I found the problem! I was naming both destination files the same! DOH!!! One is supposed to be reads the other usages! Caught it and it works fine....

One by one the penguins return my sanity, as day by day Microsoft steals my sanity!

OpenSuSE 10.0 kicks fanny!
 
MkIIISupra said:
Hehehe, I found the problem! I was naming both destination files the same!

That's what I said.

MillerH said:
The problem is that you're moving those two files to the same destination. Therefore, they probably are both being moved, but one is overwriting the other.

Good luck with your studies. Make sure to take some time to hit the boardwalk. It's beautiful this time of year.

- Miller
 
Code:
[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]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]diagnostics - Produce verbose warning diagnostics[/li]
[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::Basename - Parse file paths into directory, filename and suffix.[/li]
[li]File::Copy - Copy files or filehandles[/li]
[li]File::path - create or remove directory trees[/li]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

- Miller
 
That deserves a star [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I think it mostly deserves a "you are too easily absorbed into other people's problems" dope slap. But I'll gladly take a star instead :)

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top