Thread Idee für Dateienlöschen gesucht (11 answers)
Opened by jan99 at 2014-10-24 12:32

GwenDragon
 2014-10-24 14:44
#177948 #177948
User since
2005-01-17
14748 Artikel
Admin1
[Homepage]
user image
Ohne Gewähr und Haftung für Schäden!

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Cwd;
use File::Spec;
use Getopt::Long;
use File::Find::Rule;

$| = 1;

my $PROGNAME = ( split m|[\\/]|, $0 )[-1];

help() if !@ARGV;

my ( @filemask, @dir );
my ( $verbose, $notest, $progressbar, $debug, $force, $help );
GetOptions(
    "f|file=s" =>
      \@filemask,    # filemasks to select several files (DEFAULT: all files)
    "d|dir=s"  => \@dir,            # dirs for scan (DEFAULT: current dir)
    "progress" => \$progressbar,    # show progress (DEFAULT: on)
    "notest"   => \$notest,         # force to run in real mode (DEFAULT: off)
    "force" => \$force,    # force confirm with Yes all actions (DEFAULT: off)
    "v|verbose" =>
      \$verbose,    # show information about current action (DEFAULT: off)
    "debug"  => \$debug,    # show debug info
    "h|help" => \$help      # show help
) or die "Error in command line arguments\n";

### set defaults of parameters if not already set
push @dir, Cwd::cwd()
  if not @dir;              # use current dir, if no parameter --dir defined
$verbose     //= 0;
$notest      //= 0;
$force       //= 0;
$progressbar //= 1;
$progressbar = 0 if $verbose;
###

#
my $testmode = not $notest;

if ($debug) {
    say '--------------------';
    say "Testmode:    $testmode";
    say "Force:       $force";
    say "Progressbar: $progressbar";
    say "Verbose:     $verbose";
    say "Dirs:        @dir";
    say "Masks:       @filemask";
    say '--------------------';
    exit 4711;
}

help() if $help;

if (not @filemask) {
    say "!!!! ##ERROR## !!!! filemask must not be empty!\n";
    help();
}

say "Testmode #####################" if $testmode;

#### search files
my $rule           = File::Find::Rule->new();
my $progress_count = 0;
$rule->name(@filemask);   # set filemasks for search
$rule->exec(              # this sub is called for each matched file of search
    sub {
        my ( $shortfn, $dir, $filename ) = @_;

        $filename = File::Spec->rel2abs($filename);

        $progress_count++;
        if ($progressbar) {
            print '.'
              if $progress_count % 100 == 0;    # show progress all 100 files
        }
        elsif ( $verbose and $testmode ) {
            print '#TESTMODE# found ';
            say $filename;  # print filename for testing, no changing of files
        }

        if ( not $testmode ) {  # is no test, let the program run in real life
            my $char = 'Y';
            if ( not $force ) {    # interactive confirm
                $char = 'N';
                print "Confirm action for $filename. Really? (y/N) ";
                $char = getc();
                getc();            # fetch Enter after y or n
            }
            if ( uc $char eq 'Y' ) {    # Y means let run action for this file
                action_for_file($filename);
            }
        }
    }
);
$rule->in(@dir);                        #start search in dirs now

exit;

# -----------------------------------------------------------------------------

sub action_for_file {
    my $filename = shift;
    my $result;

    say "Processing $filename" if $verbose;

    $result =
########################################################################
###### !!!!! DO THE VERY BAD ACTION FOR THE FILE HERE!!!!!
########################################################################
      unlink($filename)    # !!!!!!!! BEWARE !!!!!!!!
########################################################################
########################################################################
      ;

    return $result;
}

# ------------------------------------

sub help {
    print <<"HELP";
Usage:
   $PROGNAME --file=FILEMASK --dir=DIR1
   
Parameters:
   --dir           Selected directory
   --file          Filemask for selecting several files
   --progress      Action with progressbar
   --verbose       Show some information about current actions
   --force         Force confirm for all actions with Yes
   --notest        Force action for files into real mode (test mode is off!)
   --help          This help
   --debug         Some debugging information for program
   
Example:

   $PROGNAME --file=a.[12]* -d=X:\\TEST --verbose
     Processes all files a.1* .. a.2* from dir X:\\TEST in testmode

   $PROGNAME --file=a.[12]* -d=/home/useer998/TEST --verbose
     Processes all files a.1* .. a.2* from dir /home/useer998/TEST in testmode
     
   $PROGNAME --file=a.[2]* -d=. --notest
     Processes all files a.2* from current dir in real mode (ACTIONS ARE PROCESSED!) with confirmation
     
   $PROGNAME --file=a.[2]* --notest --force
     Processes all files a.2* with no confirmation in real mode (ACTIONS ARE PROCESSED!)
        
HELP
    exit 4711;
}

__END__


ACHTUNG: Zeile 117.
Dort hinein muss die Perlfunktion,. die irgendwas mit der Datei macht.
Das ist auf eigene Gefahr!

Sollte laufen.
Last edited: 2014-10-25 11:37:22 +0200 (CEST)

View full thread Idee für Dateienlöschen gesucht