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

Copying files from Subdirectories

Status
Not open for further replies.

CJP12711

MIS
Jul 23, 2001
116
0
0
US
Help Please!

I have run into a problem that seems like it should be easy to solve, but has completely stumped me. I am fairly new to DOS script writing. I couldn't get it to work with DOS, so I'd thought I would try Perl, which I have a little more experience with, but I am still bery much a beginner.

We have a program that runs and inserts files into multipl subdirectories on the C drive. I need to find a way to search for these *.jpg files in each subdirectory and move them to 1 single folder named c:\images. I am having trouble figuring out how to search subdirectories for these files and then move them all. The number of directories will vary, as well as the names of the folders...
For Example:
C:\folder1\image1.jpg
C:\folder2\image2.jpg
Both of these jpg files should be moved to c:\images.

Any help is greatly appreciated!

Thanks!
CJ
 
If its only one directory deep you can do this :

=======
$base_directory = "something";
$destination_directory = "something else" ;

opendir(DIR, $base_directory) ;

my @dirs = readdir(DIR) ; # gets rid of dotfiles
foreach $dir(@dirs){
opendir(SUBDIR,"$base_directry/$dir");
my @files = readdir(SUBDIR) ;
foreach my $file(@files){
if($file =~ /.jpg$/ ) {
`YOUR_DOS_MOVE_COMMAND $base_directory/$dir/$file $destination_directory/$file` ;
}
}
closedir(SUBDIR);
}
closedir(DIR);

=====
Note that this will probably clobber duplicate filenames. You would be better off writing your program to put the files where you really want them in the first place...

There are a million ways to do this, this is just quick and dirty.
 
Thank you for your help! I would love to have the program put the files where I want them to go, but unfortunately, this is not a high priority for the programmers at our company, so I am stuck trying to make this process easier. There can be up to 600-800 sub directories and it's pretty lame having to cut and paste them all each day.

I am running into an issue with this script when I try and run it, I get a parse error for the 5th line(parse error in file c:\batches\images.pl at line 5, next 2 tokens "my @dirs") and another error on line 6(missing $ loop variable at c:\batches\images.pl line 6).

Again, thanks so much for your help!
 
W9rks for me on my unix box, you may have some windows related issues.
 
you might want to try \\ instead of / in any path names - differs from UNIX

Cheers
Duncan
 
Thanks Duncan... That did help. I still am getting a parse error at my copy statement... (parse error in file c:\batches\images.pl at line 10, next 2 tokens "copy '$base_directory\\$dir\\$file'")

$base_directory = 'C:\\batches';
$destination_directory = 'C:\\batches\\001';
opendir(DIR,"$base_directory");
@dirs = readdir(DIR);
foreach $dir(@dirs){
opendir(SUBDIR,"$base_directry/$dir");
@files = readdir(SUBDIR);
foreach $file(@files){
if($file =~ /.jpg$/ ) {
COPY '$base_directory\\$dir\\$file' '$destination_directory\\$file';
}
}
closedir(SUBDIR);
}
closedir(DIR);



Thanks again!!!
 
Ahh here you go

You are using single quotes

COPY '$base_directory\\$dir\\$file' '$destination_directory\\$file';

Perl does not interprete variables in single quotes. Do:

COPY "$base_directory\\$dir\\$file" "$destination_directory\\$file";

and it may work
 
Actually, I had the double quotes first and it didn't work. That is when I got the error above. I tried the single quotes as a last ditch effort... Any other ideas???
 
To execute a command, you need to put the command in backticks (`), like:
Code:
`COPY file1 file2`
the way siberian has it in the first reply.

//Daniel
 
Check out a few modules on cpan.org. Definitely File::Find, and probably File::Copy or maybe even File::NCopy. With either copy module, you don't have to worry about the type of slash or backticks or anything else (I'm a fan of / because you don't have to escape it like \\). File::Find does all the subdirectory work for you to an arbitrary depth.
Code:
use File::Find;
use File::Copy;

my $outdir = 'c:/images';
my @paths = qw(c:/folder1
               c:/folder2);

find(sub { /\.jpg$/i && copy($File::Find::name, $outdir) }, @paths);
Both are standard modules, so you don't even have to install them. Check each module's POD on cpan for details on how they work. Yell if problems pop up.

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top