Advertisement
Guest User

win_tree.pl

a guest
Feb 27th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.84 KB | None | 0 0
  1.  
  2. $no_of_args = @ARGV;
  3. $expand_size = 5;
  4. $max_width = 10;
  5.  
  6. if(($no_of_args == 0) || ($no_of_args == 2))
  7. {
  8.   print "ERROR: Insufficient argument\n";
  9.   goto help;
  10. }
  11. elsif($no_of_args == 3)
  12. {
  13.   if($ARGV[1] !~ "-width")
  14.   {
  15.     print "ERROR: Invalid argument\n";
  16.     goto help;
  17.   }
  18.   elsif(($ARGV[2] > $max_width) || ($ARGV[2] < 2))
  19.   {
  20.     print "ERROR: Width should not lie outside 2 to $max_width.\n";
  21.     goto help;
  22.   }
  23.   else
  24.   {
  25.     $expand_size = $ARGV[2];
  26.   }
  27. }
  28. $dir_path = $ARGV[0];
  29. if(!(-d $dir_path))
  30. {
  31.   print "ERROR: $dir_path doesn't exist\n";
  32.   goto help;
  33. }
  34.  
  35. if(($dir_path =~ /^\.$/) || ($dir_path =~ /^\.\/$/) || ($dir_path =~ /^\.\\$/))
  36. {
  37.   $string = `cd`;
  38.   $dir_name = $string;
  39.   $dir_path = $string;
  40. }
  41. else
  42. {
  43.   $dir_name = "$dir_path\n";
  44. }
  45.  
  46. @array = `dir /B /S $dir_path`;
  47. $array_size = @array;
  48. $line_count = 0;
  49.  
  50. while($line_count < $array_size)
  51. {
  52.   $array[$line_count] =~ s/\\/\//g;
  53.   $line_count  ;
  54. }
  55.  
  56. $line_count = 0;
  57.  
  58. print " ----$dir_name";
  59. $temp = $dir_path;
  60. chop($temp);
  61. $temp =~ s/\\/\//g;
  62. while($line_count < $array_size)
  63. {
  64.   if($array[$line_count] =~ /$temp\/(.*)/)
  65.   {
  66.     $array[$line_count] = "--$1\n";
  67.   }
  68.  
  69.   $array[$line_count] =~ s/.[^\/]*\//--/g;
  70.   $line_count  ;
  71. }
  72.  
  73. $line_count = 0;
  74. while($line_count < $array_size)
  75. {
  76.   $array[$line_count] = "\|\-".$array[$line_count];
  77.   $string = " " x ($expand_size - 1);
  78.   while($array[$line_count] =~ /--/)
  79.   {
  80.     $array[$line_count] =~ s/--//;
  81.     $array[$line_count] =~ s/\|\-/|$string|-/;
  82.   }
  83.   $string = "-" x ($expand_size - 1);
  84.   $array[$line_count] =~ s/\-/$string/;
  85.   $array[$line_count] =~ s/\|$string/\ $string/;
  86.   $line_count  ;
  87. }
  88.  
  89. @cross = 0;
  90. $line_count = 0;
  91. while($line_count < $array_size)
  92. {
  93.   $line = $array[$array_size - $line_count - 1];
  94.   @line_array = split(//, $line);
  95.   $no_of_char = @line_array;
  96.   $char_count = 0;
  97.   while($char_count < $no_of_char)
  98.   {
  99.     if($line_array[$char_count] =~ /\ /)
  100.     {
  101.       $cross[$char_count] = 1;
  102.     }
  103.     elsif($line_array[$char_count] =~ /\|/)
  104.     {
  105.       if($cross[$char_count] == 0)
  106.       {
  107.         $line_array[$char_count] = " ";
  108.       }
  109.     }
  110.     else
  111.     {
  112.       $cross[$char_count] = 0;
  113.     }
  114.     $char_count = $char_count   $expand_size;
  115.   }
  116.  
  117.   $array[$array_size - $line_count - 1] = join("", @line_array);
  118.   $line_count  ;
  119. }
  120.  
  121. print @array;
  122. exit;
  123.  
  124. help:
  125. printf("#####################################################################\n");
  126. printf("# To run the script,\n");
  127. printf("# perl $0 <PATH> -width <width_no>\n");
  128. printf("#    e.g. \"perl $0 . \" displays tree view of current dir\n");
  129. printf("# \"-width\" expand or compress the tree. Max = $max_width, Min = 2\n");
  130. printf("# Don't give relative path.....\n");
  131. printf("# Default width = 5\n");
  132. printf("#####################################################################\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement