Ejecutar llamada a sistema de manera recursiva en Windows
Publicado: 2018-09-11 09:02 @418
Hola a todos, estoy trabajando en un script para activar "case-sensitive" (disponible en últimas versiones de Windows 10) en árboles de directorios, para hacer esto es necesario usar el comando fsutils, el cual no trabaja de manera recursiva por directorios.
El código que poseo es el siguiente:
Mi duda es si es la forma correcta de hacerlo o existe otra manera más eficiente de lograr lo que deseo.
Saludos
El código que poseo es el siguiente:
Using perl Syntax Highlighting
- #!/usr/bin/perl
- use v5.20;
- use strict;
- use autodie;
- use File::Find;
- use Getopt::Long qw(:config bundling_values require_order no_ignore_case);
- use File::Spec::Functions qw(devnull);
- # vars
- my $enable = 1;
- my $disable = 0;
- my $status = 0;
- my $help = 0;
- my $verbose = 0;
- my $null = devnull(); # "null" device for windows
- # error
- sub errorUsage { die "@_ (try csdirtree --help for more information)\n"; }
- #----------------- script identification, options and help ------------#
- my $script = "csdirtree";
- my $nv='v1.0';
- my $copyright = <<END_COPYRIGHT ;
- [2018-09-11] (c) 2018 by Pablo Gonzalez L, pablgonz<at>yahoo.com
- END_COPYRIGHT
- my $licensetxt = <<END_LICENSE ;
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- END_LICENSE
- my $title = "$script $nv $copyright";
- # usage
- my $usage = <<"HOW_TO_USE";
- ${title}Syntax: csdirtree [--enable|--disable|--status] "X:\\full\\path\\dirtree"
- Function: Set case sensitive in directory tree for windows 10
- Examples:
- \$ csdirtree --enable "C:\\testdir"
- \$ csdirtree --disable "C:\\testdir"
- \$ csdirtree --status "C:\\testdir"
- Options:
- -h, --help - display this help and exit
- -l, --license - display license and exit
- -e, --enable - run fsutil.exe file SetCaseSensitiveInfo "X:\\dirtree" enable
- -d, --disable - run fsutil.exe file SetCaseSensitiveInfo "X:\\dirtree" disable
- -s, --status - run fsutil.exe file queryCaseSensitiveInfo "X:\\dirtree"
- -v, --verbose - show information when running fsutil.exe command in cmd
- HOW_TO_USE
- # Getopt::Long
- my %opts_cmd;
- GetOptions (
- 'h|help' => \$opts_cmd{help},
- 'e|enable' => \$opts_cmd{enable},
- 'd|disable' => \$opts_cmd{disable},
- 's|status' => \$opts_cmd{status},
- 'v|verbose' => \$opts_cmd{verbose},
- 'l|license' => \$opts_cmd{license},
- ) or die $usage;
- # help
- if (defined $opts_cmd{help}){
- print $usage;
- exit(0);
- }
- # license
- if (defined $opts_cmd{license}){
- print $licensetxt;
- exit(0);
- }
- # disable
- if (defined $opts_cmd{disable}){
- $enable = 0;
- $disable=1;
- }
- # status
- if (defined $opts_cmd{status}){
- $enable = $disable = 0;
- $status = $verbose = 1;
- }
- # verbose
- if (defined $opts_cmd{verbose}){
- $verbose = 1;
- }
- # fsutils file <options> <$dirtree> [mark]
- my $fsutil = $status ? 'fsutil file queryCaseSensitiveInfo'
- : 'fsutil file SetCaseSensitiveInfo'
- ;
- my $mark = $enable ? 'enable'
- : $disable? 'disable'
- : ''
- ;
- my $quiet = $verbose ? ''
- : "> $null"
- ;
- # check argument input
- @ARGV > 0 or errorUsage 'Input "X:\\full\\path\\dirtree" missing';
- @ARGV < 2 or errorUsage 'Unknown option or too many inputs';
- # dirtree to change case sensitive
- my $dirtree = $ARGV[0];
- # search in tree
- finddepth(\&read_tree, $dirtree);
- sub read_tree{
- my @subdirs;
- my $element = $_;
- if (-d $element){ # if a dir
- push @subdirs,$File::Find::name;
- s{/}{\\}mg foreach @subdirs; # changue / by \
- system("$fsutil @subdirs $mark $quiet");
- }
- }
- __END__
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4
Saludos