473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

copying files of a certain type to different folder

Hi,
I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory.
In other words i am sorting filetypes into subdirectories with same name.

so far i have been able to create the subdirectories, but i am not able to copy the files into those directories.
Expand|Select|Wrap|Line Numbers
  1. my code is : 
  2. use strict;
  3. use warnings;
  4. use File::Glob;
  5. use File::Copy;
  6.  
  7. my $dir_root; #dir to start in
  8. my $dir_dest_gel = 'C:\TS'; #destination directory for gel files
  9. my $dir_dest_res = 'C:\results'; # destination directory for results
  10. my $count = 0;
  11. sub dir_read 
  12. {
  13.       #parse directory for directories and files
  14.  
  15.       #local to this function
  16.       my @dir_list;
  17.       my @file_list;
  18.       my $dir_prefix = $_[0];  
  19.         my @gel_list;
  20.       my @res_list;
  21.  
  22.       print "reading dir: ${dir_prefix}\n";
  23.  
  24.       opendir(aDIR, $dir_prefix);
  25.  
  26.       #read, add to array
  27.       while($_ = readdir(aDIR))
  28.       {
  29.             #if a dir
  30.             if(-d "${dir_prefix}/${_}")
  31.                 {
  32.                   #dont allow . or ..
  33.                   if($_ ne "." && $_ ne "..")
  34.                       {
  35.                         #add to array
  36.                         push(@dir_list, "${dir_prefix}/${_}");
  37.  
  38.                         }
  39.                  }
  40.             #else a file
  41.             else
  42.             {
  43.               #filter for files 
  44.                 if($count ne 0) {
  45.  
  46.                 @gel_list = <$dir_list[$count]/*.gel>;
  47.         @res_list = <*.res>;
  48.                 foreach $_ (@gel_list){
  49.                 print"before file copy $_";
  50.                 copy( "$_", "$dir_dest_gel\\$dir_list[$count]\\$_"); }
  51.         foreach $_ (@res_list){
  52.                 print"before file copy $_";
  53.                 copy("$_", "$dir_dest_res\\$dir_list[$count]\\$_"); }
  54.         }
  55.               push(@file_list, "${dir_prefix}/${_}");
  56.  
  57.             }
  58.       }
  59.  
  60.       closedir(aDIR);
  61.  
  62.  
  63.  
  64.        #print dir and file list:
  65.         foreach $_ (@dir_list)
  66.             {
  67.  
  68.               print "\tdir: ${_}\n";
  69.             }  
  70.         foreach $_ (@file_list)
  71.             {
  72.               print "\tfile: ${_}\n";
  73.             }  
  74.  
  75. #print iteration
  76. print"$count\n";
  77. $count = $count + 1;
  78.  
  79.      #search lower dirs
  80.       foreach $_ (@dir_list)
  81.           {
  82.          # make subdirectories in TS and results folder                 
  83.                  mkdir "$dir_dest_gel\\${_}" or die $!;
  84.          mkdir "$dir_dest_res\\${_}" or die $!;                 
  85.             &dir_read($_);
  86.           }
  87. }
  88.  
  89.  
  90. if(! $ARGV[0])
  91.     {
  92.       #print usage
  93.       print "Usage: perl trial2.pl rootpath\n";  
  94.     }
  95. else 
  96.     {
  97.          # create main destination directories
  98.          mkdir $dir_dest_gel;
  99.          mkdir $dir_dest_res;
  100.  
  101.       #check if path exists
  102.       $dir_root = $ARGV[0];
  103.       if( -d $dir_root)
  104.           {
  105.             #dir is ok
  106.             print "root directory: ${dir_root}\n";
  107.           }
  108.       else
  109.           {
  110.             #end
  111.             die("root directory: ${dir_root} does not exist!, stopped\n");
  112.           }
  113.  
  114.       #read dir, and sub directories
  115.       &dir_read($dir_root);
  116.  
  117.       print "done.\n";
  118.     }
  119.  
  120.  

Can anyone please tell my how to use the glob? bcoz i am unable to copy the files.
Thanks in advance
Jul 3 '08 #1
4 2416
numberwhun
3,509 Expert Mod 2GB
Have you read the CPAN page for File::Glob ? All globbing really is, is the ability to use wild cards to match many things. The module....well.....modulizes that.

Regards,

Jeff
Jul 3 '08 #2
i am trying to group all files of a certain type into an array using glob.
but i guess that is not happening
Jul 3 '08 #3
Hi,
i modified my code, now i am able to print the path where i want the file to go, but still the file is not getting copied.
Please tell me what is wrong in the code below.

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

my $dir_root; #dir to start in
my $dir_dest_gel = 'C:\TS'; #destination directory for gel files
my $dir_dest_res = 'C:\results'; # destination directory for results
#my $count;

sub dir_read
{
#parse directory for directories and files

#local to this function
my @Dir_name;
my @dir_list;
my $dir_prefix = $_[0];

print "reading dir: ${dir_prefix}\n";

opendir(aDIR, $dir_prefix);

#read, add to array -----line no 25
while($_ = readdir(aDIR))
{
#if a dir
if(-d "${dir_prefix}/${_}")
{
#dont allow . or ..
if($_ ne "." && $_ ne "..")
{
#add to array36
push(@dir_list, "${dir_prefix}/${_}"); }
}
#else a file--------line no 37
else
{
@Dir_name = split m!/!,$dir_prefix;
if($_ =~ /\.gel/)
{
copy("$_", "$dir_dest_gel\\$Dir_name[-1]\\$_");
}
if($_ =~ /\.res/)
{
copy("$_", "$dir_dest_res\\$Dir_name[-1]\\$_");
}

}
}

closedir(aDIR);



#print dir and file list:74
foreach $_ (@dir_list)
{

print "\tdir: ${_}\n";
}


#search lower dirs
foreach $_ (@dir_list)
{
# make subdirectories in TS and results folder
mkdir "$dir_dest_gel\\${_}" or die $!;
mkdir "$dir_dest_res\\${_}" or die $!;
&dir_read($_);
}
}


if(! $ARGV[0])
{
#print usage
print "Usage: perl trial3.pl rootpath\n";
}
else
{
# create main destination directories
mkdir $dir_dest_gel;
mkdir $dir_dest_res;

#check if path exists
$dir_root = $ARGV[0];
if( -d $dir_root)
{
#dir is ok
print "root directory: ${dir_root}\n";
}
else
{
#end
die("root directory: ${dir_root} does not exist!, stopped\n");
}

#read dir, and sub directories
&dir_read($dir_root);

print "done.\n";
}
Jul 3 '08 #4
Hi thanks for the guidance.
i got to know the problem.
i had to specify the path for the source file.
as i was using read directory, i assumed the active directory would change but it was not so, so once i gave the full path for the src and dest files, the whole program worked.
thanks again
Jul 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Chad Crowder | last post by:
Hi all, I hope someone can give me a hand, because I'm out of ideas. I keep getting this message: Access to the path "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
4
by: tshad | last post by:
I have an old version of VS 2002 and want to make a copy of my Visual Studio project to another project with another name. In VS 2003, you can do it but there is no copy project command in VS 2002...
2
by: Jerad Rose | last post by:
I have a fairly simple C# console app which copies files from a network folder to a local folder. When the app resides on my local C: drive, it runs just fine. However, when the app resides on a...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
18
jhardman
by: jhardman | last post by:
Have you ever wanted to upload files through a form and thought, "I'd really like to use ASP, it surely has that capability, but the tutorial I used to learn ASP didn't mention how to do this."? ...
13
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
2
rhitam30111985
by: rhitam30111985 | last post by:
Hi all .. i came across the findstr batch command for searching for specific string in a file name . Can the same be used for reading folder names ? ie i have folders like these : 2008-001...
3
shoonya
by: shoonya | last post by:
Hi, I am having a Microsoft Visual Studio Solution file "abc" present in the folder "foo" having having all the files. Now accidently I did something to the solution file and it's giving...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.