bldsystemtools/commonbldutils/FileOps.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Script to do file operations within a directory with the option to exclude sub directories or files
       
    15 # 
       
    16 #
       
    17 
       
    18 use strict;
       
    19 use FindBin;		# for FindBin::Bin
       
    20 use Getopt::Long;
       
    21 
       
    22 use lib $FindBin::Bin;
       
    23 
       
    24 use FileOps;
       
    25 
       
    26 # Process the commandline
       
    27 my ($iSourceDir, $iTargetDir, $iAction, @iExcludes) = &ProcessCommandLine();
       
    28 
       
    29 &FileOps::ProcessDir($iSourceDir, $iTargetDir, $iAction, @iExcludes);
       
    30 
       
    31 # ProcessCommandLine
       
    32 #
       
    33 # Inputs
       
    34 #
       
    35 # Outputs
       
    36 #
       
    37 # Description
       
    38 # This function processes the commandline
       
    39 
       
    40 sub ProcessCommandLine {
       
    41   my ($iHelp, $iSourceDir, $iTargetDir, $iAction, @iExclude, @iExcludes);
       
    42   GetOptions('h' => \$iHelp, 's=s' =>\$iSourceDir,  't:s' =>\$iTargetDir, 'a=s' => \$iAction, 'x:s@' => \@iExclude);
       
    43 
       
    44   if (($iHelp) || (!defined $iSourceDir) || (!defined $iAction))
       
    45   {
       
    46     Usage();
       
    47   } elsif (! -d $iSourceDir) {
       
    48     die "$iSourceDir is not a Directory";
       
    49   } elsif ((lc($iAction) ne 'copy') && (lc($iAction) ne 'move') && (lc($iAction) ne 'delete') && (lc($iAction) ne 'zip')) {
       
    50     die "$iAction is not a supported Action";
       
    51   } elsif ((lc($iAction) eq 'copy') || (lc($iAction) eq 'move') && (lc($iAction) ne 'delete')) {
       
    52     if (!defined $iTargetDir)
       
    53     {
       
    54       die "$iAction Requires a Target Directory";
       
    55     }
       
    56     if (! -d $iTargetDir)
       
    57     {
       
    58       die "$iTargetDir is not a Directory";
       
    59     }
       
    60   } 
       
    61   
       
    62   # Check all the exclude sub directories or files
       
    63   foreach my $iSubItem (@iExclude)
       
    64   {
       
    65     if ((! -d "$iSourceDir\\$iSubItem") && (! -f "$iSourceDir\\$iSubItem"))
       
    66     {
       
    67       print "$iSubItem is not a Directory or File in $iSourceDir - not Excluding\n";
       
    68     } else {
       
    69       push  @iExcludes,$iSubItem;
       
    70     }
       
    71   }
       
    72   
       
    73   return($iSourceDir, $iTargetDir, $iAction, @iExcludes);
       
    74 }
       
    75 
       
    76 # Usage
       
    77 #
       
    78 # Output Usage Information.
       
    79 #
       
    80 
       
    81 sub Usage {
       
    82   print <<USAGE_EOF;
       
    83 
       
    84 	Usage: FileOps.pl [options]
       
    85 
       
    86 	options:
       
    87 
       
    88 	-h  help
       
    89 	-a  Action (Move or Copy or Delete or Zip the directories)
       
    90 	-s  Source Directory
       
    91 	-t  Target Directory (Not required for Delete Action)
       
    92 	-x  Exclude a File or Directory from the Action
       
    93 USAGE_EOF
       
    94 	exit 1;
       
    95 }