cut a file to deplace him in an other directory...

From: B.julien (expertspy_at_caramail.com)
Date: 04/09/04


Date: 8 Apr 2004 16:10:27 -0700

Hi !
this is the little script
#! /usr/bin/perl -W
# this program copy all files in a directory
# and deplace them in a new directory named "multimedia" if he doesn't exist
# the program create him...

use strict ;

my($targetDirFull,$targetDir,
   $origDirFull,$origDir,$bit,
   @allThings,$i,$file,@fileIn);
   
$targetDir = 'multimedia';
$targetDirFull = 'C:\multimedia' ;
$origDirFull = 'C:\Documents and Settings\julien\XXX\' ;
#-------------------------------------
#verification that "multimedia" exist-
#-------------------------------------
unless( chdir('C:\\') ) { print "Error 1..\n" ; exit(0);}
else { chdir('C:\\'); }
unless (-e $targetDir) {
       print "creating directory \"multimedia\"...\n";
       mkdir('multimedia',0777);
}
############################################################################
## openning the Original Dir' ;take each file and write them in "multimedia#
############################################################################
opendir(ORIG,$origDirFull) or die("the directory is unavaible !\n") ;
unless(readdir(ORIG)) {
   print "Error 2.." ; exit(0);
}
else {
     print "open $origDirFull ... [OK]\n";
     #to list all files in the directory
     @allThings = readdir(ORIG);
     if ($allThings[0] eq "") {
             print "the directory is empty ..normal ?\n";
     }
     else {
               print "there's some file in this directory..\nI deplace them\n";
               while($allThings[0] ne "") {
                     $file = pop(@allThings) ;
                  open(INPUT,$file) or die ("Error 3..\n");
                  @fileIn = <INPUT>;
                  close(INPUT);
                  
                  opendir(TARGET,$targetDir."\"") or die ("computer crazy !..\n");
                  open(OUTPUT,">".$file) or die ("Error 4..\n");
                  foreach $bit (@fileIn) {
                  print OUTPUT $bit ;
                      }
                close(OUTPUT);
                closedir(TARGET);
          }
      }
}
#######END OF SCRIPT######
thanks for all !