Answer» Experts
I am new to batch programming and NEED some help here. I am looking for a batch script for following task.
a) Say in folder A, i have 10 files which are updated daily. i need to check these files and if the files are updated with latest timestamp then copy to destination folder. The no of files in folder A may vary. b) if among 10 files, any file is not updated then display error, saying this file is not updated. b) On destination, before copying i need to archive existing files.
Thanks in advance.what is "latest timestamp"? Is it today's date? What is your LOCAL date format?
How will you "archive"?
Dias de verano,
Yes it's today's date and date format is mm/dd/yyyy format. IF you know Perl and can use it, here's an alternative SOLUTION. Code: [Select]use strict; use warnings; use File::Find; use File::Copy; use Archive::Tar; use Cwd;
my $dir="c:/test"; my $destination="c:/destination"; my $archive_destination="c:/archive_destination"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $year+=1900; $mon+=1; my $archive="$year-$mon-$mday-$hour-$min-$sec.tar"; #set archive filename
sub archive { my $cwd = getcwd(); chdir($destination); my @filelist; while(<*>){ -f && push(@filelist,$_); } #get files to archive my $tar = Archive::Tar->new; $tar->add_files(@filelist); $tar->write($archive); move($archive,$archive_destination); #move archived file to destination foreach(@filelist){ unlink $_ ; # remove files after archive. } chdir($cwd); }
sub wanted { if ( lstat($_) && -f _ && (int(-M _) == 0) ) { my $filename = $File::Find::name; print "moving $filename to $destination\n"; move("$filename","$destination") or warn "Cannot move $filename to $destination; $!\n"; } elsif ( -f _ ) { print "File not updated: $filename\n"; } }
archive; find({wanted => \&wanted}, "$dir"); If I may say so, ghostdog, an excellent solution! Thanks Ghostdog, but i need script which needs to be scheduled and hence looking for some batch script Quote from: satya85 on April 21, 2009, 07:52:59 AM Thanks Ghostdog, but i need script which needs to be scheduled and hence looking for some batch script
open your editor, type in
perl myscript.pl
save as mybatch.bat. Use the task scheduler to run it periodically.
Otherwise, wait for other solutions to come by.i tried to save the perl script to mybatch.bat and ran that script, didnt work. No- he means you save the perl file as a perl file (.pl) and then create a small batch file that invokes the perl interpreter with t hat perl code. you can then set up an automated task using the batch just as you would have given a pure batch solution.ok got it , i installed active perl to run this. Anyways i need a batch script that does the same. Thanks ghostdog for the perl script.
can i have text file that has list of file names and then pass each file name as parameter to check for timestamp and move to destination folder.Quote from: satya85 on April 21, 2009, 10:08:20 AMok got it , i installed active perl to run this. Anyways i need a batch script that does the same. Thanks ghostdog for the perl script.
can i have text file that has list of file names and then pass each file name as parameter to check for timestamp and move to destination folder.
sure Code: [Select]use File::stat; use Time::localtime; use File::Copy; my $destination = "some path"; open(FH,"<", "file") or die "Cannot open file:$!"; while (<FH>){ CHOMP; #get rid of newline my $st = stat($_) or die "No $_: $!"; my $timestamp = $st->mtime; #get file modified time in SECS since epoch # depending on how you want to check time stamp ..code here move($_,$destination);
} close(FH);
please read up the documentation to get started. perldoc perl.
|