myback.pl

なんてのを作った。

#!/usr/bin/perl -w
# myback.pl
# created:2005-03-19 22:57:18+09:00

#
# todo:もちっときれいに書く
# todo:ログファイルの出力
#

use strict;
use Cwd;
use File::Copy;

my $dir = cwd;
my $backdir = "backs";
my $timestamp = &get_timestamp(0);
my $backpath = "$dir/$backdir/$timestamp";

MAIN: {
	opendir(DH,"$dir");
	my @dir_list = readdir(DH);
	closedir(DH);

	unless (-e $backdir) {
		mkdir($backdir);
	}

	unless (-e $backpath) {
		mkdir($backpath);
	}

	©_dir($dir, $backpath);
	exit(0);
}

########################################
# subs
########################################
sub copy_dir {
	my $from = shift;
	my $to = shift;

	opendir(DH, $from);
	my @dir_list = readdir(DH);
	closedir(DH);

	foreach my $dir_and_file (@dir_list) {
		next if ($dir_and_file eq '.');
		next if($dir_and_file eq '..');
		next if($dir_and_file eq "$backdir");

		if (-d $dir_and_file) {	# ディレクトリだったら
			mkdir("$to/$dir_and_file");
			chdir("$from/$dir_and_file");
			©_dir("$from/$dir_and_file", "$to/$dir_and_file");
			chdir("$from");
		} else {
			copy("$from/$dir_and_file", "$to/$dir_and_file");
			print "COPY $from/$dir_and_file => $to/$dir_and_file done.\n";
		}
	}
}

sub print_dir {
	# どこに、どんなファイル名で、どのディレクトリを
	# ($path, $logfilename, $dir_path)
	# まだ呼び出しなし

	my $output_path = shift;
	my $logfile = shift;
	my $dir_path = shift;		# 一応リストで渡しておくか?
	my @dir_list;

	if ($dir_path == undef) {
		$dir_path = &cwd;
	}

	opendir(DH, "$dir_path");
	@dir_list = readdir(DH);
	closedir(DH);

	open(LOG, ">$output_path.$logfile");

	foreach my $atom (@dir_list) {
		if ($atom =~ /^\.{1,2}$/) {
			next;
		}
		my $format;
		print $format;
	}

	close(LOG);
}

# 時間を返す
sub get_timestamp {
	my($arg) = @_;
	if ($arg) {
		return "";
	}
	else {
		my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
		return sprintf("%04d_%02d_%02d_%02d_%02d_%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
	}
}
# myback.pl ends here.

使い方は、上のやつをファイルに保存して、コマンドプロンプト

perl myback.pl

と入力する。するとmyback.plのあるディレクトリ内*1のファイルとディレクトリをbacks/タイムスタンプというディレクトリの中にコピーする。

*1:pl2batで変更してPATHを追加すればどこでも呼び出せるが。