Problem faced while deleting the required files from directory !!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijayarl
    New Member
    • Sep 2008
    • 65

    #1

    Problem faced while deleting the required files from directory !!!

    Hi Everyone,

    i have written small code where it fetches the required files from required dir path
    & deleting those files before proceeding further.

    script goes like this:
    Code:
    my $@file_names;
    my $full_path;
    my $dir  = "c:\\svap\\input\\";
    my $file_name ="*.xls";
    &check_file($dir,$file_name);
    
    sub check_file($dir,$file_name){
    $full_path = $dir.$file_name;
    @file_names = glob($full_path);
                      if(@file_names eq 0){
                        print " no *.xls file found,can proceed further!!!";
                       }
                     else{
                          foreach my $i(@file_names){
                               unlink "$i";
                                  }
                            }
                    }
    The above code works fine if there is only *.xls file but my problem is:
    sometimes i get *.xls + filename.xls.xl s files in the dir

    how to delete these *.xls.xls files with out altering the $file_name value(ie,$file_ name = "*.xls.xls" )

    Can anyone help me on this ???

    Regards,
    Vijayal
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    The code you posted will not even run, so post some good code. There is little sense in helping you with code that is bad to begin with.

    Bareword found where operator expected at script line 2, near "$@file_nam es"
    (Missing operator before file_names?)
    Illegal character in prototype for main::check_fil e : $dir,$file_name at script line 8 .
    Bareword found where operator expected at script line 2, near "$@file_nam es"
    (Missing operator before file_names?)
    Illegal character in prototype for main::check_fil e : $dir,$file_name at script line 8 .
    Can't use global $@ in "my" at script line 2, near "my $@"
    syntax error at script line 2, near "$@file_nam es"

    Comment

    • vijayarl
      New Member
      • Sep 2008
      • 65

      #3
      Hi Kevin,

      As i am newbiee to perl, it was my mistake(type error)

      2nd line has to be:
      Code:
      my @file_names;
      and then in line no 8, am just trying to concatenate the 2 values to get this value
      Code:
      c:\svap\input\*.xls
      please can you help me to delete *.xls.xls file ??

      Thanks,
      Vijayarl

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        There are other problems with your code. See how this works:

        Code:
        my $dir  = 'c:\svap\input\\';
        my $file_name = '*.xls';
        &check_file($dir,$file_name);
        
        sub check_file {
           my ($dir, $path) = @_;
           my @file_names = glob("$dir$path");
           if(@file_names == 0){
              print " no *.xls file found,can proceed further!!!";
           }
           else{
              foreach my $i (@file_names){
                 unlink "$i" or print "Can't delete $i : $!\n";
              }
           }
        }

        Comment

        • vijayarl
          New Member
          • Sep 2008
          • 65

          #5
          hi Kevin,

          Thank you very much for your support... it really worked ..
          able to rectify my mistakes from you..
          thanks once again...
          pls keep doing good work, it helps all newbiee's in perl.

          bye
          vijayarl
          Last edited by vijayarl; Sep 21 '08, 08:15 AM. Reason: mis spell the name..

          Comment

          Working...