• Publicidad

Ejecutar llamada a sistema de manera recursiva en Windows

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

Ejecutar llamada a sistema de manera recursiva en Windows

Notapor pablgonz » 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:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2. use v5.20;
  3. use strict;
  4. use autodie;
  5. use File::Find;
  6. use Getopt::Long qw(:config bundling_values require_order no_ignore_case);
  7. use File::Spec::Functions qw(devnull);
  8.  
  9. # vars
  10. my $enable  = 1;
  11. my $disable = 0;
  12. my $status  = 0;
  13. my $help    = 0;
  14. my $verbose = 0;
  15. my $null    = devnull(); # "null" device for windows
  16.  
  17. # error
  18. sub errorUsage { die "@_ (try csdirtree --help for more information)\n"; }
  19.  
  20. #----------------- script identification, options and help ------------#
  21. my $script = "csdirtree";
  22. my $nv='v1.0';
  23. my $copyright = <<END_COPYRIGHT ;
  24. [2018-09-11] (c) 2018 by Pablo Gonzalez L, pablgonz<at>yahoo.com
  25. END_COPYRIGHT
  26.  
  27. my $licensetxt = <<END_LICENSE ;
  28.     This program is free software; you can redistribute it and/or modify
  29.     it under the terms of the GNU General Public License as published by
  30.     the Free Software Foundation; either version 3 of the License, or
  31.     (at your option) any later version.
  32.  
  33.     This program is distributed in the hope that it will be useful, but
  34.     WITHOUT ANY WARRANTY; without even the implied warranty of
  35.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  36.     General Public License for more details.
  37. END_LICENSE
  38.  
  39. my $title = "$script $nv $copyright";
  40.  
  41. # usage
  42. my $usage = <<"HOW_TO_USE";
  43. ${title}Syntax: csdirtree [--enable|--disable|--status] "X:\\full\\path\\dirtree"
  44. Function: Set case sensitive in directory tree for windows 10
  45. Examples:
  46. \$ csdirtree --enable  "C:\\testdir"
  47. \$ csdirtree --disable "C:\\testdir"
  48. \$ csdirtree --status  "C:\\testdir"
  49.  
  50. Options:
  51.  -h, --help     - display this help and exit
  52.  -l, --license  - display license and exit
  53.  -e, --enable   - run fsutil.exe file SetCaseSensitiveInfo "X:\\dirtree" enable
  54.  -d, --disable  - run fsutil.exe file SetCaseSensitiveInfo "X:\\dirtree" disable
  55.  -s, --status   - run fsutil.exe file queryCaseSensitiveInfo "X:\\dirtree"
  56.  -v, --verbose  - show information when running fsutil.exe command in cmd
  57. HOW_TO_USE
  58.  
  59. # Getopt::Long
  60. my %opts_cmd;
  61. GetOptions (
  62.     'h|help'       => \$opts_cmd{help},
  63.     'e|enable'     => \$opts_cmd{enable},
  64.     'd|disable'    => \$opts_cmd{disable},
  65.     's|status'     => \$opts_cmd{status},
  66.     'v|verbose'    => \$opts_cmd{verbose},
  67.     'l|license'    => \$opts_cmd{license},
  68. ) or die $usage;
  69.  
  70. # help
  71. if (defined $opts_cmd{help}){
  72.     print $usage;
  73.     exit(0);
  74. }
  75.  
  76. # license
  77. if (defined $opts_cmd{license}){
  78.     print $licensetxt;
  79.     exit(0);
  80. }
  81.  
  82. # disable
  83. if (defined $opts_cmd{disable}){
  84.     $enable = 0;
  85.     $disable=1;
  86. }
  87.  
  88. # status
  89. if (defined $opts_cmd{status}){
  90.     $enable = $disable = 0;
  91.     $status = $verbose = 1;
  92. }
  93.  
  94. # verbose
  95. if (defined $opts_cmd{verbose}){
  96.     $verbose = 1;
  97. }
  98.  
  99. # fsutils file <options> <$dirtree> [mark]
  100. my $fsutil = $status ? 'fsutil file queryCaseSensitiveInfo'
  101.             :          'fsutil file SetCaseSensitiveInfo'
  102.             ;
  103.  
  104. my $mark    = $enable ? 'enable'
  105.             : $disable? 'disable'
  106.             :           ''
  107.             ;
  108.  
  109. my $quiet   = $verbose ? ''
  110.             :            "> $null"
  111.             ;
  112.  
  113. # check argument input
  114. @ARGV > 0 or errorUsage 'Input "X:\\full\\path\\dirtree" missing';
  115. @ARGV < 2 or errorUsage 'Unknown option or too many inputs';
  116.  
  117. # dirtree to change case sensitive
  118. my $dirtree  = $ARGV[0];
  119.  
  120. # search in tree
  121. finddepth(\&read_tree, $dirtree);
  122.  
  123. sub read_tree{
  124. my @subdirs;
  125. my $element = $_;
  126. if (-d $element){ # if a dir
  127.     push @subdirs,$File::Find::name;
  128.     s{/}{\\}mg foreach @subdirs; # changue / by \
  129.     system("$fsutil @subdirs $mark $quiet");
  130.     }
  131. }
  132. __END__
  133.  
Coloreado en 0.006 segundos, usando GeSHi 1.0.8.4
Mi duda es si es la forma correcta de hacerlo o existe otra manera más eficiente de lograr lo que deseo.

Saludos
pablgonz
Perlero nuevo
Perlero nuevo
 
Mensajes: 236
Registrado: 2010-09-08 21:03 @919
Ubicación: Concepción (Chile)

Publicidad

Re: Ejecutar llamada a sistema de manera recursiva en Window

Notapor explorer » 2018-09-13 14:55 @663

No lo creo. O usar otra herramienta que sí admita recorrer un árbol de forma recursiva.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14476
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Ejecutar llamada a sistema de manera recursiva en Window

Notapor charlygarcia » 2020-07-21 22:08 @964

La recursividad desborda la pila, pero es la mejor opción.
[text]¿Cómo lograr que no se seque una gota de agua? Arrojándola al mar. [/text] - Nota: Este tag no se puede utilizar en firmas.Samsara
Avatar de Usuario
charlygarcia
Perlero nuevo
Perlero nuevo
 
Mensajes: 54
Registrado: 2009-03-06 23:16 @011

Re: Ejecutar llamada a sistema de manera recursiva en Window

Notapor explorer » 2020-07-22 05:19 @263

Perl suele tener un aviso de exceso de recursividad cuando llega al nivel 100.

Se puede cambiar ese nivel con

use warnings::recursion 1000;

Incluso se puede desactivar completamente, y depender exclusivamente del tamaño de la memoria RAM:

no warnings 'recursion';
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14476
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 3 invitados

cron