×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Copy the latest file in multiple directory

Copy the latest file in multiple directory

Copy the latest file in multiple directory

(OP)
Hi,
Any help to a newbie will greatly appreciated.

I'm trying to write MS DOS batch file to check the 4 latest file in a directoty containing several subdirectories.
d:\subok
\10
\20
\30
\40
\50

New can come and stored in any of the sub-dir (10,20,...). Goal is to copy the 4 newest file.

THanks.

Kr,
Richard

RE: Copy the latest file in multiple directory

Since this is a UNIX Scripting forum, I'd recommend doing this by installing Cygwin and writing a bash script (that's how I'd do it).

Or, if you wanted to be Windows-y and are running a supported OS (which OS exactly, by the way?) you could use PowerShell, see forum1619: PowerShell (Microsoft).

Otherwise if it must be MS-DOS you may have some luck in one of the Windows forums.

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Copy the latest file in multiple directory

(OP)
Thanks. I reposted the thread in MS Powershell

RE: Copy the latest file in multiple directory

Quote (Annihilannic)

Since this is a UNIX Scripting forum...
THAT must be what I'm doing wrong. smile

RE: Copy the latest file in multiple directory

Sorry, was suffering from forum dyslexia. smile

Of course Perl would be a good option for this script too!

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Copy the latest file in multiple directory

As penance I've written a solution in Perl (tested using Strawberry Perl under Win XP):

CODE --> Perl

#!/usr/bin/perl -w
use strict;
use File::Copy;
my $srcdir="c:\\temp\\tektips\\subok2";
my $tgtdir="c:\\temp\\tektips\\subok2\\copies";
my @fileinfo;
my $i=0;

chdir $srcdir or die "unable to change to $srcdir";

# find dirs matching 10, 20, ...
opendir D,$srcdir or die "unable to open $srcdir";
my @subdirs = grep { /^[0-5]0$/ } readdir(D);
closedir D;

# find files in those dirs
foreach my $dir (@subdirs) {
	opendir D,$dir or die "unable to open $dir";
	my @files = grep { !/^[.]/} readdir(D);
	# store their names and modification times
	foreach my $file (@files) {
		$fileinfo[$i]{name} = $dir . "\\" . $file;
		$fileinfo[$i++]{mtime} = (stat($fileinfo[$i]{name}))[9];
	}
	closedir D;
}

# sort the files by modification time, process the last 4
foreach my $file ((sort { ${$a}{mtime} <=> ${$b}{mtime} } @fileinfo)[-4..-1]) {
	print "Copying file ${$file}{name} (modified " . localtime(${$file}{mtime}) . ") to $tgtdir\n";
	copy(${$file}{name},$tgtdir) or die "Copy failed: $!";
} 

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Copy the latest file in multiple directory

(OP)
Hi,

My server is using active PERL i'm trying to adapt the proposed script but i'm stucked at line 23
The directory structure was also changed :

T:\CFS\MP\Monday
\Hour0
\Hour1
\Hour10
\Hour11
\Hour12
\Hour13
\Hour14
\Hour15
\Hour16
\Hour17
\Hour18
\Hour19
\Hour2
\Hour20
\Hour21
\Hour22
\Hour23
\Hour3
\Hour4
\Hour5
\Hour6
\Hour7
\Hour8
\Hour9



/** compiling error
T:\script>test.pl
syntax error at T:\script\test.pl line 23, near "$dir ("
Global symbol "@srcdir" requires explicit package name at T:\script\test.pl line 23.
Global symbol "$dir" requires explicit package name at T:\script\test.pl line 24.
Global symbol "$dir" requires explicit package name at T:\script\test.pl line 24.
syntax error at T:\script\test.pl line 32, near "}"
Execution of T:\script\test.pl aborted due to compilation errors.
/**

//***** script ******
#!d:\bin\perl.exe -w
#use warnings;
use lib 'd:/perl/site/lib';
use strict;
use File::Copy;

my $srcdir="T:/cfs/mp/monday/";
my $tgtdir="T:/script/";
my @fileinfo;
my $i=0;
#my @subdirs;

chdir $srcdir;

# find dirs in scrdir
opendir (DIR,$srcdir) or die "unable to open $srcdir";
while (($srcdir = readdir(DIR))) {
printf ("$srcdir\n");
}
closedir(DIR)

# find files in those dirs
foreach my $dir (@srcdir) {
opendir (D,$dir) or die "unable to open $dir";
my @files = grep { !/^[.]/} readdir(D);
# store their names and modification times
foreach my $file (@files) {
$fileinfo[$i]{name} = $dir . "\\" . $file;
$fileinfo[$i++]{mtime} = (stat($fileinfo[$i]{name}))[9];
}
closedir D;
}

# sort the files by modification time, process the last 4
foreach my $file ((sort { ${$a}{mtime} <=> ${$b}{mtime} } @fileinfo)[-4..-1]) {
print "Copying file ${$file}{name} (modified " . localtime(${$file}{mtime}) . ") to $tgtdir\n";
copy(${$file}{name},$tgtdir) or die "Copy failed: $!";
}

RE: Copy the latest file in multiple directory

You are trying to use the same $srcdir variable for two different things... which won't work. Any reason why you stopped using @subdirs?

If you don't like the original grep usage, you could do something like this:

CODE

...
# find dirs in srcdir
my @subdirs;
opendir (DIR,$srcdir) or die "unable to open $srcdir";
while ((my $dir = readdir(DIR))) {
    push @subdirs,$dir;
}
closedir(DIR)

foreach my $dir (@subdirs) {
... 

But note that you may need to do special handling of the "." and ".." directories.

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Copy the latest file in multiple directory

(OP)
Thanks Annihilannic,

I modifed it a little a bit to adapt the according to my need and it is working perfectly now, below is code.

But I have one more favor to ask, these copied file are in CSV format and the filenames can be any, I need to search a particular string in line.
For example :
allAssociationUtilizationData;EWSDV16SG;2012/08/24;07:00:00;STP01.99;900;FALSE;SCANNER;10;8253;7611;16235;11998;2063216;1369640;0;1570236;972272;0

I need to search the line with "STPAM01.99" in column 5 and "10" in column 9 in one file and "STPAR02.99" in column 5 and "10" in column 9 in the other file.

Afterwards divide the value of column 14 with value of column 13 of the row for each file.
Depending on the result if it's > .50 script will call "low.bat" batch file.

Any suggestion on how to proceed.

Thanks a lot!

CODE --> Perl

#!d:\bin\perl.exe -w
#use warnings;
use lib 'd:/perl/site/lib';
use strict;
use File::Copy;

my $srcdir="T:/cfs/mp/monday/";
my $tgtdir="T:/script";
my @fileinfo;
my $i=0;

chdir $srcdir or die "unable to change to $srcdir";

# find dirs matching time
opendir D,$srcdir or die "unable to open $srcdir";
my @subdirs = grep { /^[Hour]/ } readdir(D);
closedir D;;

# find files in those dirs
foreach my $dir (@subdirs) {
	opendir D,$dir or die "unable to open $dir";
	my @files = grep { /PM_SCANREP_STP/} readdir(D);
	# store their names and modification times
	foreach my $file (@files) {
		$fileinfo[$i]{name} = $dir . "\\" . $file;
		$fileinfo[$i++]{mtime} = (stat($fileinfo[$i]{name}))[9];
	}
	closedir D;
}

# sort the files by modification time, process the last 2
foreach my $file ((sort { ${$a}{mtime} <=> ${$b}{mtime} } @fileinfo)[-2..-1]) {
	print "Copying file ${$file}{name} (modified " . localtime(${$file}{mtime}) . ") to $tgtdir\n";
	print ${$file}{name};
	copy(${$file}{name},$tgtdir) or die "Copy failed: $!";
} 

RE: Copy the latest file in multiple directory

One minor correction to your code: grep { /^[Hour]/ } should just be grep { /^Hour/ }. "^[Hour]" would match any filename beginning with the letters "H", "o", "u" or "r", whereas "^Hour" matches any filename beginning with the word "Hour".

I'm not sure I really understand your requirements, but this untested code should give you some ideas...

CODE --> Perl

# ...

# sort the files by modification time, process the last 2
my @copiedfiles;
foreach my $file ((sort { ${$a}{mtime} <=> ${$b}{mtime} } @fileinfo)[-2..-1]) {
	print "Copying file ${$file}{name} (modified " . localtime(${$file}{mtime}) . ") to $tgtdir\n";
	print ${$file}{name};
	copy(${$file}{name},$tgtdir) or die "Copy failed: $!";
        push @copiedfiles,"$tgtdir/${$file}{name}";
} 

foreach my $file (@copiedfiles) {
        open FILE,$file or die "unable to open $file";
        while (<FILE>) {
                 my @a=split(/,/);
                 if ($a[4] eq "STPAM01.99" && $a[9]==10 && $a[13]/$a[12] > 0.5) { system("c:/somepath/low.bat $file"); }
                 if ($a[4] eq "STPAR02.99" && $a[9]==10 && $a[13]/$a[12] > 0.5) { system("c:/somepath/low.bat $file"); }
        }
        close FILE;
} 

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Copy the latest file in multiple directory

(OP)
Thanks.

I have inserted your proposal accoring to my need but i'm getting to following error when running it.

/**
Use of uninitialized value in string eq at T:\script\test3.pl line 44, <FILE> line 98
Use of uninitialized value in string eq at T:\script\test3.pl line 45, <FILE> line 98
/**

The idea of this script is to look for the 2 latest file in a source directory that contains several sub-directory.
The copied files are CSV and i need to divide the value of column 13 and 12 if the condition column 4 and 9 are met.
depending on the result a DOS batch is called.

CODE --> Windows

#!d:\bin\perl.exe -w
#use warnings;
use lib 'd:/perl/site/lib';
use strict;
use File::Copy;

my $srcdir="T:/cfs/mp/monday/";
my $tgtdir="T:/script";
my @fileinfo;
my $i=0;
my @spf = glob "t:/script/*.spf";


chdir $srcdir or die "unable to change to $srcdir";

# find dirs matching time
opendir D,$srcdir or die "unable to open $srcdir";
my @subdirs = grep { /^Hour/ } readdir(D);
closedir D;;

# find files in those dirs
foreach my $dir (@subdirs) {
	opendir D,$dir or die "unable to open $dir";
	my @files = grep { /PM_SCANREP_STP/} readdir(D);
	# store their names and modification times
	foreach my $file (@files) {
		$fileinfo[$i]{name} = $dir . "\\" . $file;
		$fileinfo[$i++]{mtime} = (stat($fileinfo[$i]{name}))[9];
	}
	closedir D;
}

# sort the files by modification time, process the last 2
foreach my $file ((sort { ${$a}{mtime} <=> ${$b}{mtime} } @fileinfo)[-2..-1]) {
	print "Copying file ${$file}{name} (modified " . localtime(${$file}{mtime}) . ") to $tgtdir\n";
	print ${$file}{name};
	copy(${$file}{name},$tgtdir) or die "Copy failed: $!";
} 

foreach my $file (@spf) {
	open FILE, "< $file" or die "Cannot open $file for read\n$!";       
        while (<FILE>) {
                 my @a=split(/,/);
	         if ($a[4] eq "STPAM01.47" && $a[9]==10 && $a[13]/$a[12] > 0.5) { system("t:/script/low.bat"); }
	         if ($a[4] eq "STPAR02.47" && $a[9]==10 && $a[13]/$a[12] > 0.5) { system("t:/script/low2.bat"); }           

        }
        close FILE;
} 

RE: Copy the latest file in multiple directory

And what is on line 98 of the input file? My guess is that it doesn't have 14 fields...

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Copy the latest file in multiple directory

(OP)
Thanks Annihilannic,

the last feedback gave me hint, i forgot that my cvs is actually semi-colon separated.
after changing in the split function /,/ with /;/... it worked!!

Kr,
Richard

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close