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

update a folder script

Status
Not open for further replies.

atamel

Programmer
Feb 24, 2005
3
US
Hi,

I'm new to Perl but I feel like this problem can easily be solved in Perl. Here's what I want to do.

I have 2 almost identical folders, say Folder1 and Folder2. Both folders have files and subfolders. You can think of Folder2 as a folder that contains an updated version of the program in Folder1. What I want to do is write an update.pl script that will look in Folder2 and update Folder1 (files and subfolders) accordingly. In other words, if there's a more recent file in Folder2, then update.pl replace that old file in Folder1 with the updated file. Similarly, if there's a new subfolder in Folder2, the script will create that folder under Folder1 along with the files in it.

Does anyone know a simple way of doing this in Perl?

Thanks,
Mete
 
While this isn't a full solution, try these subroutines as a starting point for your request.

Code:
sub verifydirs {

    foreach (glob "$_[0]/*")
	{
		if ((-d) && !(-l))
		{
            # check to see if directory exists
			# on backup server
#			print "Checking directory: $_\n";
			my $directory = "n:/";
			my @dirtree = split /\//, $_;
			shift @dirtree;
			$directory .= join("/", @dirtree);
			if(!(-e "$directory"))
			{
				print "Making directory: $_\n";
				mkdir("$directory", 0777) or keelover("Cannot create output directory $directory: $!", "4");
			}
		}
        # recurse into subdirectories;
		-d && verifydirs($_);
    }
}

sub copyfiles
{
    foreach (glob "$_[0]/*")
	{
        # recurse into subdirectories;
		-d && copyfiles($_) unless -l;
        # if it's a file
		if (-f)
		{
			#and if it ends in the following extensions
			if (($_ =~/\.bak$/i) || ($_ =~/~$/i))
			{
				#skip to the next file
				next;
			}
			#otherwise check file for age.  If less than 1 day old,
			# copy file to the backup server
			if ((-M) < 1.1)
			{
				my @filename = (split /\//, $_);
				shift @filename;
				my $newfile = "n:/";
				$newfile .= join("/", @filename);
				print "Copying file: $_\n";
				copy("$_", "$newfile") or keelover("Could not copy file $_ : $! .", "3");
			}
		}
    }
}

&verifydirs("/path/to/dir");
&copyfiles("/path/to/dir");

- Rieekan
 
Oops, you'll also need to use the File::Copy module to run the copyfiles subroutine.

That'll teach me not to read my own code,

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top