找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 19972|回复: 2

[原创代码] 分类记事工具rcder.pl

[复制链接]
发表于 2011-5-21 13:05:40 | 显示全部楼层 |阅读模式
本帖最后由 523066680 于 2011-5-21 13:17 编辑

很像像wiki那样有目录树,有分类的记事,找了类似的软件zim
还行,打开速度比较慢。

用命令行的话也有不一样的缺点优点,我这个嘛……
未完成,主要是基础没学够却又急需用到,写的机制不太好,在考虑是不是修改整个机制。
用法 ,代码备注中有提到一些,常用的:
add             添加一个分类
rd                删除一个分类
cd                进入一个分类(模仿目录形式)
note 内容 : 添加一项记事
del   编号1 编号2  : 删除指定编号的记事
list   列出当前分类下的所有内容
list all 列出整个分类下的内容。
$Path:/verycd #list all
Notes:
+---2011-Apr-5
|       http://www.verycd.com/entries/518679/ 放浪男孩 comic
|       http://www.verycd.com/topics/2876939/ game 东方幻想系列
|
|       http://www.verycd.com/topics/2814764/ 黏黏世界
|
+---CG
|       《CG动画不完全收集》(Computer Graphics Animation Demo)
|       http://www.verycd.com/topics/255259/
|
+---动漫高清壁纸
|       《高清ACG壁纸》(HD ACG Wallpaper 停止更新)[压缩包]
|       http://www.verycd.com/topics/2797187/
|       《MINA ACG手机壁纸》(MINA ACG Phone Wallpapers)(480X854,带原图)[压缩包]

|       http://www.verycd.com/topics/2838059/
|       《高清晰动漫壁纸》(ACG)[压缩包]
|       http://www.verycd.com/topics/2822666/
|       高清壁纸
|       http://www.verycd.com/topics/2834430/
|       《1920高清自然风光图片》
|       http://www.verycd.com/topics/2823063/
|
+---高清资源
|       《人人影视纪录片特辑》[YYeTs人人影视][中英双语字幕]
|            http://www.verycd.com/topics/2789412/
|       BBC 频道,高清纪录片
|       《BBC:太阳系的奇迹》
|            http://www.verycd.com/topics/2847930/
|       BBC:大脑:神秘的历史
|            http://www.verycd.com/topics/2877004/
|       BBC:数学的故事
|           http://www.verycd.com/topics/2878349/
|           700MB版本4集  http://www.verycd.com/topics/2854607/
|           中文字幕在评论中可以找到
|       《世界上最美丽的地方:奥林匹克热带雨林》
|       http://www.verycd.com/topics/2773370/
|       《世界上最美丽的地方》
|       2011-Feb-20
|       http://www.verycd.com/topics/2878041/
|       http://www.verycd.com/topics/2797166/
|       http://www.verycd.com/topics/2773370/
|       http://www.verycd.com/topics/264809/
|       http://www.verycd.com/topics/2770521/ 世界上最美丽的地方 :巴厘岛
|       《BBC 失落之地:扎瓜》
|       《奇异的生物 第三季》
|       《探索频道:动物的末日》
|       《BBC自然世界--非洲龙山》
|       最大影像:哈勃望远镜3D
|       http://www.verycd.com/topics/2887856/
|
+---计算机
|       《轻松玩转个人服务器》(刘晓辉 编著)插图版[PDF]
|       http://www.verycd.com/topics/2794899/
|
\---剧集
        快递高手:日本  http://www.verycd.com/entries/517814/




dir
dir all

+-----verycd
|     +-----2011-Apr-5
|     +-----CG
|     +-----动漫高清壁纸
|     +-----高清资源
|     +-----计算机
|     \-----剧集
|
+-----video
|     +-----sex
|     +-----搞笑
|     +-----历史
|     \-----社会
|
+-----web
|     +-----博客文摘
|     +-----查询应用
|     +-----翻墙
|     +-----论坛
|     +-----网盘、在线图册
|     |     \-----upload.ouliu.net
|     |
|     \-----资源网
|
\-----购书
 楼主| 发表于 2011-5-21 13:13:52 | 显示全部楼层

  1. #!/usr/bin/perl
  2. system("title rcd.pl");
  3. if ($#ARGV<0) {
  4.         $folder="./";
  5.         $file="rcd2.txt";
  6. } else {
  7.         print "$file\n";
  8.         $ARGV[0]=~s#\\#/#g;
  9.         @file=split "/",$ARGV[0];
  10.         $file=pop @file;
  11.         $folder=join "/",@file;
  12. }

  13. chdir "$folder" or die "Could not into Dest folder...";
  14. if (! -e "$file") {
  15.         open CREATE,">$file";
  16.         print CREATE "/main\n";
  17.         close CREATE;
  18. }

  19. =Something
  20. 增加了重新读入功能,主要是一些变更需要重新读入
  21. 2011-1-3  date日期出现问题,原因:分隔符符-空格,个数不一,切割改为\s+
  22. 2011-1-3  目录名支持附加信息,分离处理
  23.   笔记,像copymove copytree 几个函数之间,有一些判断应该由另一个函数操作
  24.   rename 修改中
  25. 2011-1-12 函数权衡:函数只负责操作,尽量不用判断调用参数的合理性
  26.            大部分输入信息的判断交给主循环
  27. 2011-1-29 printf 引号中直接插入变量,如果含有%d会转义,需谨慎。所以改为外部引入。
  28.            sub save 以及 listnote listx 的printf都需要改动
  29. 2011-2-08 发现错别字=。=
  30. =cut

  31. =Descript environment variable
  32.         变量描述
  33.         $file        全局变量 文件的完整路径,已辨别存在的
  34.         $path        当前路径,去除info的
  35.         $last        目录名的最后一个,去除$info的
  36.         $front        /$last前面的字符
  37.         $info        读取时路径后面的附加信息
  38.         $fold{$path}=$info        key记录一个完整路径的存在,且value=附加信息
  39.         $item{$front}{$last}        建立所有路径的父子关系,方便递归,索引,列出当前目录
  40.         $num{$path}        记录当前路径的下一个下标,也记录了note的个数
  41.         $rcder{$path}[$num{$path}++]=$_;        一个路径下的所有notes,下标即为记录的编号,然后+1
  42.         $year-$mon-$day 日期,全局
  43. =cut

  44. #Backup
  45. ($week,$mon,$day,$time,$year)=split /\s+/,localtime(time);
  46. $dest="./backup";
  47. use File::Copy;
  48. mkdir "$dest" if (! -e $dest);
  49. copy "$file","$dest/$year-$mon-$day.x" if (! -e "$dest/$year-$mon-$day.x");

  50. #Read data
  51. &read();

  52. print "TREE_\n";

  53. $path="";
  54. &dir($path,1);
  55. print "\n"x2;

  56. %command=(
  57.         "exit"=>"quit of script",
  58.         "save"=>"save all information to RCD text",
  59.         "list"=>"look every notebook of tree",
  60.         "dir"=>"list directory ,dir all to list all",
  61.         "dirx"=>"list directory by tree mode",
  62.         "rename"=>"rename for a directory",
  63.         "add"=>"add a new folder for this tree",
  64.         "del"=>"WRITING",
  65.         "note"=>"write a new note in path",
  66.         "date"=>"write a note,record time",
  67.         "notemode"=>"make notes",
  68.         "push"=>"","mark"=>"",
  69.         "left"=>"","right"=>"",
  70.         "front"=>"","next"=>"",
  71.         "change"=>"",
  72.         "help"=>"list command name and information."
  73. );



  74. =Descript variable in while loop //////////////
  75. $file    指定文件
  76. $path    当前路径
  77. @current    记录当前目录下的子目录名,使其可以通过编号方式操作。
  78.              脚本中的全局变量
  79. $inp     输入的信息,去除了\n的
  80. @arg     $inp分割后的各个部分
  81. $else    除了第一个信息的其余部分
  82. ///////////////////////////////////////////////
  83. =cut

  84. =Descript commands /////////////////////////////////////
  85. add  添加一个分类
  86.   add \d 添加一个以日期为名字的分类
  87. rd   删除一个分类
  88. move
  89. copy
  90. dir 列出当前子分类
  91.    dir [n] 列出N层
  92.    dir all 递归所有层
  93. tree  以树状形式列出当前分类
  94.    tree [n]  N层
  95.    tree all  所有层
  96. info information  给一个分类添加注释。
  97. note notes 将后面的参数添加到记录中。
  98. date notes 将后面的参数添加到记录中,前面插入当前日期。
  99. notemode 输入模式,允许输入多行,直到一行为 exit 时退出

  100. #left right push mark 中
  101. # \b=空格 \t=调格 \d=引入当前日期
  102. #(循环过程的常用变量 $else中'\d'将替换为当前日期)

  103. left n info : 在指定信息前面插入信息,除了编号+1个空格,后面若有空格也算入。
  104. right n info: 在指定信息后面追加信息,只要是"$n "后面的都会作为信息包括空格。
  105.    left,right: 默认没有自动添加空格分隔原来的信息
  106.    example: "left 2 haha" = "haha第二条原信息"
  107.             "left 2  haha " = " haha 第二条原信息"
  108.             "left 2    " = "   第二条原信息"
  109.       right 作用在后面,形式跟以上例子相同
  110. push n1 [n2] [n3] .. info 在N个信息前面插入信息   默认带一个空格做分割 可用于整段推进
  111. mark n1 [n2] [n3] .. info 在N个信息后面追加信息   默认带一个空格做分割
  112.    空格可能导致出错,若要插入空格,可以用\b \t (我这里修改'\b'为空格,不用担心退格)
  113.    如果类似 push/mark 1 2 3 而后面没有信息,则将会把3作为信息添入 1 和 2 条目
  114. list
  115.      list n
  116.      list all
  117. listx
  118. del n1 [n2] [n3] ... 删除指定记录
  119. front,next
  120. change
  121. ////////////////////////////////////////////////////////////////
  122. =cut


  123. $output="C:/Documents and Settings/Amateur/桌面";
  124. $export=0;
  125. my @current;

  126. while (1) {
  127.         if ($export==0) {
  128.                 if ($path eq "") {print "\$Path:/ #";} else {print "\$Path:$path #";}
  129.                 $inp=<STDIN>;
  130.                 chomp $inp;

  131.                 if ($inp=~m/(.*)>(.*)/) {
  132.                         $inp=$1;
  133.                         open EXPORT,">${output}/$2";
  134.                         select EXPORT;
  135.                         $export=1;
  136.                 }
  137.         } else {
  138.                 $export=0;
  139.                 close EXPORT;
  140.                 select STDOUT;
  141.                 next;
  142.         }
  143.         #deal
  144.         $inp=~s/\\d/${year}-${mon}-${day}/ig;  #这里\d代替时间
  145.         @arg=split /\s+/,$inp;
  146.         if ($#arg<0) {print "Empty\n"; next;}
  147.         $else=substr($inp,length($arg[0]));  #除了第一参数后面的所有字符。
  148.         #采用substr的原因,从$inp中提取至$else,而不是在$else中截取
  149.         $else=~s/^\s+//; #去前面N空格
  150.         if ($arg[0]=~/^exit/i) {
  151.                 &save;
  152.                 exit;
  153.         } elsif ($arg[0]=~/^save$/i) {
  154.                 &save;
  155.         } elsif ($arg[0]=~/^export$/i) {
  156.                 print "EXPORT MODE,TESTING\n";
  157.                 open WRITE,">$output/test.txt";
  158.                 select WRITE;
  159.         } elsif ($arg[0]=~/^dir/i) {
  160.                 if ($arg[1]=~/\d+/) {
  161.                         $max=$arg[1];
  162.                 } elsif ($arg[1]=~/all/i) {
  163.                         $max="all";
  164.                 } else {
  165.                         &dir($path,1);
  166.                         next;
  167.                 }
  168.                 print "Folders:\n\n";
  169.                 if ($arg[0]=~/dirx/i) {
  170.                         &treex($path,0,$max);
  171.                 } else {
  172.                         &tree($path,0,$max);
  173.                 }
  174.                 print "\n";
  175.         } elsif ($arg[0]=~/^cd$/i) {
  176.                 if ($#arg<1) {
  177.                         print "No arguments\n";
  178.                         next;
  179.                 } elsif ($arg[1]=~/^\d+$/) {
  180.                         if (exists $fold{$path."/".$current[$arg[1]]}) {
  181.                                 $path=$path."/".$current[$arg[1]];
  182.                         } else {print "ERR : Bad codes\n";}
  183.                 } else {
  184.                         if (exists $item{$path}{$arg[1]}) {
  185.                                 $path=$path . "/" . $arg[1];
  186.                         } elsif (exists $fold{$arg[1]}) {
  187.                                 $path=$arg[1];
  188.                         } elsif ($arg[1] eq "..") {
  189.                                 @tmp=split("/",$path);
  190.                                 pop @tmp;
  191.                                 $path=join "/",@tmp;        #20101224 改了方式,关于+号匹配有问题不解释
  192.                                 print "$path\n";
  193.                                 #$path=~s#/\w+$##;        20101217,中文不被匹配,修改为用split
  194.                         } elsif ($arg[1] eq "/") {
  195.                                 $path="";
  196.                         } else {print "Folder:#$arg[1]# not exist\n\n"; next;}
  197.                 }
  198.                 &dir($path,1);
  199.                 print "\n";
  200.                 &listnote($path,0,1);
  201.                 print "\n";
  202.         } elsif ($arg[0]=~/^add$/i) {
  203.                 if (&mkdir($else)==1) {
  204.                         &dir($path,1);
  205.                 }
  206.         } elsif ($arg[0]=~/^info$/i) {                #MARK可能需要优化
  207.                 if ($#arg<1) {
  208.                         print "ERR: To few arguments \n";
  209.                         #如果参数只有目录名,没有info,意思是将info清空
  210.                 } elsif ($arg[2]=~m#/#) {
  211.                         print "ERR: information could not include / \n";
  212.                 } elsif (exists $fold{$path."/".$arg[1]}) {
  213.                         $fold{$path."/".$arg[1]}=$arg[2];
  214.                         &dir($path,1);
  215.                 } elsif (exists $fold{$path."/".$current[$arg[1]]}) {
  216.                         $fold{$path."/".$current[$arg[1]]}=$arg[2];
  217.                         &dir($path,1);
  218.                 } else {print "ERR: $arg[1] not exists\n";}
  219.                 if ($#arg>2) {print "info如果有空格,空格后的内容会被当做多余参数而去除\n";}
  220.         } elsif ($arg[0]=~/^rd$/i) {
  221.                 if (exists $fold{$path."/".$else}) {
  222.                         &deltree($path."/".$else,$else);
  223.                 } elsif ($arg[1]=~/^\d+$/) {
  224.                         if (exists $fold{$path."/".$current[$arg[1]]}) {
  225.                                 $else=$current[$arg[1]];
  226.                                 &deltree($path."/".$else,$else);
  227.                         } else {print "ERR : Bad codes\n";}
  228.                 } else {
  229.                         print "Folder: $arg[1] dose not exists! \n";
  230.                 }
  231.                 &dir($path,1);
  232.         } elsif ($arg[0]=~/^rename$/i) {
  233.                 if ($#arg<2) {
  234.                         print "ERR: too few arguments\n";
  235.                         next;
  236.                 }
  237.                 if ($arg[1]=~m#/#) {print "Err: arg1 Could not include:/\n";next;}
  238.                 if ($arg[2]=~m#/#) {print "Err: arg2 Could not include:/\n";next;}
  239.                 if ($arg[2]=~m#^\d+$#) {print "Err: arg2 Could not be a number\n";next;}
  240.                 #这里不用判断是否含空格,因为参数就是以空格隔开
  241.                 if (exists $fold{$path."/".$arg[2]}) {
  242.                         print "ERR: Folder $arg[2] does exists!\n";
  243.                         next;
  244.                 }
  245.                 if (exists $fold{$path."/".$arg[1]}) {
  246.                         &rename($arg[1],$arg[2],$path);
  247.                 } elsif ($arg[1]=~/^\d+$/) {
  248.                         if (exists $fold{$path."/".$current[$arg[1]]}) {
  249.                                 &rename($current[$arg[1]],$arg[2],$path);
  250.                         } else {
  251.                                 print "ERR: Argument: $arg[1] out of range \n";
  252.                                 next;
  253.                         }
  254.                 } else {
  255.                         print "ERR: Folder $arg[1] does not exists!\n";
  256.                 }
  257.         } elsif ($arg[0]=~/^move$/i) {
  258.                 if ($#arg<2) {print "Too few arguments\n";next;}
  259.                 foreach (1..($#arg-1)) {
  260.                         if ($arg[$_] ne $arg[$#arg]) {
  261.                                 &copymove($arg[$_] , $arg[$#arg] , "move");
  262.                         } else {
  263.                                 print "ERR: $arg[$_] eq $arg[$#_]\n";
  264.                         }
  265.                 }
  266.         } elsif ($arg[0]=~/^movex$/i) {
  267.                 if ($#arg<2) {print "Too few arguments\n";next;}
  268.                 foreach (1..($#arg-1)) {
  269.                         if ($arg[$_]!=$arg[$#_]) {
  270.                                 &copymove($current[$arg[$_]] , $current[$arg[$#arg]] , "move");
  271.                         } else {
  272.                                 print "ERR: $arg[$_]==$arg[$#_]\n";
  273.                         }
  274.                 }
  275.         } elsif ($arg[0]=~/^copy$/i) {
  276.                 if ($#arg<2) {print "Too few arguments\n";next;}
  277.                 foreach (1..($#arg-1)) {
  278.                         &copymove($arg[$_] , $arg[$#arg] , "copy");
  279.                 }
  280.         } elsif ($arg[0]=~/^del$/i) {
  281.                 if ($else eq "") {print "ERR:No argument!\n\n";next;}
  282.                 if ($else=~/(\d+)/) {
  283.                         while ($else=~s/(\d+)//) {
  284.                                 $rcder{$path}[$1]="\n";
  285.                         }
  286.                         &updata();
  287.                         &listnote($path,0,1);
  288.                 } else {
  289.                         print "ERR: Arguments not a number!\n\n";
  290.                 }
  291.         } elsif ($arg[0]=~/^note$/i) {
  292.                 if ($path eq "") {&SayRootErr; next;}
  293.                 if ($#arg<1) {
  294.                         print "Information is empty!\n";
  295.                 } else {
  296.                         $rcder{$path}[$num{$path}++]=" ".$else."\n";
  297.                         &listnote($path,0,1);
  298.                 }
  299.         } elsif ($arg[0]=~/^notemode$/i){
  300.                 $end=0;
  301.                 while ($end==0) {
  302.                         $inp=<STDIN>;
  303.                         chomp $inp;
  304.                         if ($inp=~/^exit$/i) {
  305.                                 $end=1;
  306.                         } else {
  307.                                 $rcder{$path}[$num{$path}++]=" $inp\n";
  308.                         }
  309.                 }
  310.                 &listnote($path,0,1);
  311.         } elsif ($arg[0]=~/^date$/i) {
  312.                 if ($path eq "") {&SayRootErr; next;}
  313.                 if ($#arg<1) {
  314.                         print "Information is empty!\n";
  315.                 } else {
  316.                         $rcder{$path}[$num{$path}++]=" ".
  317.                                 "${year}-${mon}-${day} ".$else."\n";
  318.                         &listnote($path,0,1);
  319.                 }
  320.         } elsif ($arg[0]=~/^mark$/i) {
  321.                 if ($path eq "") {&SayRootErr; next;}
  322.                 $fo=0;
  323.                 if ($#arg<2) {
  324.                         print "ERR: Arguments < 2\n";
  325.                 } elsif ($arg[1]=~/^\d+$/) {
  326.                         $arg[$#arg]=~s/\\t/\t/ig;
  327.                         $arg[$#arg]=~s/\\b/ /ig;
  328.                         foreach (1..($#arg-1)) {
  329.                                 if ($arg[$_]=~/^\d+$/) {
  330.                                         if ((0<=$arg[$_]) and ($arg[$_]<$num{$path})) { #num记录的比实际下标>1
  331.                                         chomp $rcder{$path}[$arg[$_]];
  332.                                         $rcder{$path}[$arg[$_]].=" $arg[$#arg]\n";
  333.                                         $fo++;
  334.                                         } else {print "ERR: $arg[$_] out of array\n";}
  335.                                 } else {print "ERR: $arg[$_] not a number\n";}
  336.                         }
  337.                 } else {print "ERR: arg1 not a number\n";}
  338.                 &listnote($path,0,1) if ($fo>0);
  339.         } elsif ($arg[0]=~/^push$/i) {
  340.                 if ($path eq "") {&SayRootErr; next;}
  341.                 $fo=0;
  342.                 if ($#arg<2) {
  343.                         print "ERR: Arguments < 2\n";
  344.                 } elsif ($arg[1]=~/^\d+$/) {
  345.                         $arg[$#arg]=~s/\\t/\t/ig;
  346.                         $arg[$#arg]=~s/\\b/ /ig;
  347.                         $arg[$#arg]=~s/\$date/${year}-${mon}-${day}/ig;
  348.                         foreach (1..($#arg-1)) {
  349.                                 if ($arg[$_]=~/^\d+$/) {
  350.                                         if ((0<=$arg[$_]) and ($arg[$_]<$num{$path})) { #num记录的比实际下标>1
  351.                                         $rcder{$path}[$arg[$_]]=" "."$arg[$#arg]" . $rcder{$path}[$arg[$_]] ;
  352.                                         $fo++;
  353.                                         } else {print "ERR: $arg[$_] out of array\n";}
  354.                                 } else {print "ERR: $arg[$_] not a number\n";}
  355.                         }
  356.                 } else {print "ERR: arg1 not a number\n";}
  357.                 &listnote($path,0,1) if ($fo>0);
  358.         } elsif ($arg[0]=~/^left$/i) {
  359.                 if ($path eq "") {&SayRootErr; next;}
  360.                 if ($#arg<1) {
  361.                         print "ERR: Arguments < 2\n";
  362.                 } elsif ($arg[1]=~/^\d+$/) {
  363.                         if ( (0<=$arg[1]) and ($arg[1]<=$num{$path}) ) {
  364.                                 $else=~s/^$arg[1] //;        #去这个编号+一个空格,后面若有空格也保留
  365.                                 $else=~s/\\t/\t/ig;
  366.                                 $else=~s/\\b/ /ig;      #我这里\b是空格 blank
  367.                                 $rcder{$path}[$arg[1]]=~s/^ //; #因为机制中,原信息前留一个空格 MARK
  368.                                 $rcder{$path}[$arg[1]]=" ". $else . $rcder{$path}[$arg[1]];
  369.                                 &listnote($path,0,1);
  370.                         } else {print "ERR: $arg[$1] out of range\n";}
  371.                 } else {print "ERR: arg1 not a number\n";}
  372.         } elsif ($arg[0]=~/^right$/i) {
  373.                 if ($path eq "") {&SayRootErr; next;}
  374.                 if ($#arg<1) {
  375.                         print "ERR: Arguments < 2\n";
  376.                 } elsif ($arg[1]=~/^\d+$/) {
  377.                         if ( (0<=$arg[1]) and ($arg[1]<=$num{$path}) ) {
  378.                                 $else=~s/^$arg[1] //;        #去这个编号+一个空格,后面若有空格也保留
  379.                                 $else=~s/\\t/\t/ig;
  380.                                 $else=~s/\\b/ /;
  381.                                 chomp $rcder{$path}[$arg[1]];
  382.                                 $rcder{$path}[$arg[1]].="$else\n";
  383.                                 &listnote($path,0,1);
  384.                         } else {print "ERR: $arg[$1] out of range\n";}
  385.                 } else {print "ERR: arg1 not a number\n";}
  386.         } elsif ($arg[0]=~/^next$/i) {
  387.                 if ($path eq "") {
  388.                         print "提示:根目录不支持写入信息\n";
  389.                         next;
  390.                 }
  391.                 if ($#arg<1) {
  392.                         print "提示: 参数个数 小于 1\n";
  393.                 } elsif ($arg[1]=~/^\d+$/) {
  394.                         if ((0<=$arg[1]) and ($arg[1]<$num{$path})) {        #num记录的比实际下标>1
  395.                                 print "在第 $arg[1] 条目后面追加信息,exit退出:\n";
  396.                                 $end=0;
  397.                                 while ($end==0) {
  398.                                         $inp=<STDIN>;
  399.                                         if ($inp=~/^exit\n$/i) {
  400.                                                 $end=1;
  401.                                         } else {
  402.                                                 $rcder{$path}[$arg[1]].=" ".$inp;
  403.                                         }
  404.                                 }
  405.                                 &updata();
  406.                                 &listnote($path,0,1);
  407.                         } else {print "ERR: $arg[1] out of array\n";}
  408.                 } else {print "ERR: arg1 not a number\n";}
  409.         } elsif ($arg[0]=~/^front$/i) {
  410.                 if ($path eq "") {&SayRootErr; next;}
  411.                 if ($#arg<1) {
  412.                         print "提示: 参数个数 小于 1\n";
  413.                 } elsif ($arg[1]=~/^\d+$/) {
  414.                         if ((0<=$arg[1]) and ($arg[1]<$num{$path})) {        #num记录的比实际下标>1
  415.                                 print "在第 $arg[1] 条目前面追加信息,exit退出:\n";
  416.                                 $end=0;
  417.                                 $str="";
  418.                                 while ($end==0) {
  419.                                         $inp=<STDIN>;
  420.                                         if ($inp=~/^exit\n$/i) {
  421.                                                 $end=1;
  422.                                         } else {
  423.                                                 $str.=" ".$inp;
  424.                                         }
  425.                                 }
  426.                                 $rcder{$path}[$arg[1]]=$str.$rcder{$path}[$arg[1]];
  427.                                 &updata();
  428.                                 &listnote($path,0,1);
  429.                         } else {print "ERR: $arg[1] out of array\n";}
  430.                 } else {print "ERR: arg1 not a number\n";}
  431.         } elsif ($arg[0]=~/^change$/i) {
  432.                 if ($path eq "") {&SayRootErr; next;}
  433.                 if ($#arg<1) {
  434.                         print "ERR: Arguments < 2\n";
  435.                 } elsif ($arg[1]=~/^\d+$/) {
  436.                         if ((0<=$arg[1]) and ($arg[1]<$num{$path})) {        #num记录的比实际下标>1
  437.                                 print "EVER: $rcder{$path}[$arg[1]]\nINPUT:";
  438.                                 $inp=<STDIN>;  #包含换行符的
  439.                                 $rcder{$path}[$arg[1]]=" ".$inp;
  440.                                 &listnote($path,0,1);
  441.                         } else {print "ERR: $_ out of array\n";}
  442.                 } else {print "ERR: arg1 not a number\n";}
  443.         } elsif ($arg[0]=~/^list$/i) {
  444.                 $max=1;
  445.                 if ($else=~/^\s*$/i) {&listnote($path,0,1);next;}
  446.                 if ($arg[1]=~/\d+/) {$max=$arg[1];}
  447.                 if ($arg[1]=~/all/i) {$max="all";}
  448.                 if ( ($arg[1]=~/\w+/) and (exists $fold{$path."/".$arg[1]}) ) {
  449.                         &listx($path."/".$arg[1],0,$max);
  450.                 } else {
  451.                         &listx($path,0,$max);
  452.                 }
  453.                 print "\n";
  454.         } elsif ($arg[0]=~/^help$/i) {
  455.                 foreach (sort keys %command) {
  456.                         printf  "%-10s %s\n",$_,$command{$_};
  457.                 }
  458.         } elsif ($arg[0]=~/^clear$/i) {
  459.                 &clear();
  460.         } else {
  461.                 $tmp=$path."/".$arg[0];
  462.                 if (exists $fold{$tmp}) {
  463.                         if ($else ne "") {
  464.                                 $n=$num{$tmp}++;
  465.                                 $rcder{$tmp}[$n]=" ".$else ."\n";
  466.                                 &listx($tmp,0,4);
  467.                         } else {
  468.                                 print "Empty note!\n";
  469.                         }
  470.                 } else {
  471.                         print "Error command\n";
  472.                 }
  473.         }
  474. }

  475. <STDIN>;


  476. =Function Descript /////////////////////////////
  477.         read        从文本读入信息
  478.         save        保存信息
  479.         updata        重新读取信息,即调用save 再 read
  480.         tree        PathNow DepthLevel(0Begin) MaxDepth STR_space_n
  481.                 参数:当前路径   当前层次(初调用时统一为0)   最大层数  积累的缩进空格数
  482.         treex        PathNow DepthLevel(0Begin) MaxDepth STR_space_n Last?(0 or 1)
  483.                 比tree多一个$last 值为1或0,自身判断是否递归到最深一层用的,貌似没用上
  484.         dir        PathNow print/Unprint(1 or 0)
  485.                 作用,给当前目录编号,名单储存在@current,方便命令操作。
  486.                 参数1则显示,参数0则只是更新当前名单到@current
  487.         mkdir        在当前建立分类,单参数,包括名称和info,函数会帮你做一些判断
  488.         copymove        FolderA  FolderB  ("copy" or "move")字符参数
  489.                         如果是move就在完成后删除FolderA
  490.         copytree        PathA PathB (复制A到B)
  491.         deltree        FullPath LastName Sure?("y"/"n")  
  492.                 FullPath,又另给LastName,是为了方便函数操作,不用另外处理
  493.         listnote        Path DepthLevel MaxDepth
  494.         listx        PathNow DepthLevel(0Begin) MaxDepth STR_space_n Last?
  495.         clear
  496.         SayRootErr
  497. ///////////////////////////////////////////////
  498. =cut

  499. sub read {
  500.         open READ,"<$file";  # $file是全局变量
  501.         undef %rcder;
  502.         undef %item;
  503.         undef %num;
  504.         foreach (<READ>) {
  505.                 #chop;
  506.                 if (m#^/#) {
  507.                         chomp;
  508.                         @path=split "/",$_;
  509.                         $last=pop @path;
  510.                         $front=join "/",@path;
  511.                         $path=$_;                #path now
  512.                         $path=~s/ +(.*)//;        #包括空格在内的和空格后面的都replace
  513.                         if ($last=~s/ +(.*)//) {                #子目录名,也去除信息并提取到info
  514.                                 $fold{$path}=$1;        #文件夹存在,并且有信息
  515.                         } else {
  516.                                 $fold{$path}="";
  517.                         }
  518.                         $item{$front}{$last}="";        #for the tree , easy to list
  519.                         $num{$path}=0;                        #number for record of each path
  520.                 } else {
  521.                         $rcder{$path}[$num{$path}++]="$_";  #the Real array to record
  522.                 }
  523.         }
  524.         close READ;
  525. }

  526. # @_ [0] [1]
  527. sub export {

  528. }

  529. sub save {
  530.         open WRITE,">$file";        # $file是全局变量
  531.         foreach (sort keys %fold) {
  532.                 print WRITE "$_ $fold{$_}\n";
  533.                 foreach $f ( @{$rcder{$_}} ) {
  534.                         print WRITE "$f" if ($f ne "\n");
  535.                 }
  536.         }
  537.         close WRITE;
  538.         print "Information has been saved\n";
  539. }

  540. sub updata {
  541.         &save();
  542.         print "在重新读入\n";
  543.         $pathnow=$path;
  544.         &read();
  545.         $path=$pathnow;  #path会被刷新,让其恢复。
  546.         #不必 &dir($path,0); 更新@current,因为正是恢复目录
  547. }


  548. # @_ [0] [1] [2] [3] [4] - >  
  549. # PathNow DepthLevel(0Begin) MaxDepth STR_space_n Last?
  550. sub treex {
  551.         my ($path,$level,$max)=@_;
  552.         my $tmpstr;
  553.         my $arr_n;
  554.         my $test;
  555.         my $last;
  556.            @test=split "/",$path;


  557.         $arr_n=0;
  558.         foreach (keys %{$item{$_[0]}}) {$arr_n++;}

  559.         $str=substr($_[3],0,-6);
  560.         if ($_[4]==0) {$next="+-----";} else {$next="\\-----";}
  561.         if ($level>0) {
  562.                 print "$str$next$test[$#test]\n";
  563.         }        #第一参数第一层,是当前目录,不显示,只显示下面层的
  564.         if (($arr_n==0) and ($_[4]==1)) {print "$_[3]\n";}

  565.         if ($arr_n>0) {
  566.         if (($level<$max) or ($max eq "all")) {
  567.                 $test=0;
  568.                 $last=0;
  569.                 foreach (sort keys %{$item{$_[0]}}) {
  570.                         if ($test<($arr_n-1)) {
  571.                                 $tmpstr="|     ";
  572.                         } else {
  573.                                 $tmpstr="      ";
  574.                                 $last=1;
  575.                         }
  576.                         &treex( $_[0]."/".$_ ,
  577.                                 $level+1 ,
  578.                                 $max ,
  579.                                 $_[3].$tmpstr,
  580.                                 $last
  581.                         );
  582.                         $test++;
  583.                 }
  584.         }
  585.         }
  586. }

  587. # @_ [0] [1] - >  PathNow print/Unprint(1 or 0)
  588. sub dir {
  589.         my $num=0;
  590.         undef @current;
  591.         print "Folders:\n";
  592.         foreach (sort keys %{$item{$_[0]}}) {
  593.                 $current[$num]=$_;
  594.                 if ($_[1]==1) {
  595.                         printf "%2d -> %s\t%s\n",$num++,$_,$fold{$path."/".$_};
  596.                 }
  597.         }
  598.         print "\n";
  599. }

  600. # @_ [0] -> string -> include "name information"
  601. # 建立目录:名字不能带/ 不能是数字 不能为空 不能是已经存在的
  602. # info的名字不能带/ 不过在第一行判断里已经判断了整个字符串不能带/
  603. sub mkdir {
  604.         my $arg=$_[0];
  605.         my $create=0;
  606.         if ($arg=~m#/#) {print "Could not include \/ \n";next;}
  607.         if ($arg=~s/ +(.*)//) {$info=$1;} else {$info="";}
  608.         if ($arg eq "") {
  609.                 print "Arguments is emtpy!";
  610.         } elsif ($arg=~/^\d+$/) {
  611.                 print "Could not be a number\n";
  612.         } elsif (exists $fold{$path."/".$arg}) {
  613.                 print "ERR : ./$arg folder exists\n";
  614.         } else {
  615.                 $fold{$path."/".$arg}=$info;
  616.                 $item{$path}{$arg}="";
  617.                 $num{$path."/".$arg}=0;
  618.                 $create=1;
  619.         }
  620.         return $create;
  621. }

  622. # @_ [0] [1] [2] [3] - >  PathNow DepthLevel(0Begin) MaxDepth STR_space_n
  623. sub tree {
  624.         my ($path,$level,$max)=@_;
  625.         my $strlen;
  626.         @test=split "/",$path;
  627.         $path=$test[$#test];
  628.         $strlen=length $_[0];
  629.         if ($level!=0) {
  630.                 if ($_[0] eq "") {print "/\n";} else {print " "x$_[3] . "/$path\n";}
  631.         }
  632.         if (($level<$max) or ($max eq "all")) {
  633.                 foreach (sort keys %{$item{$_[0]}}) {
  634.                         &tree($_[0]."/".$_ , $level+1 , $max , $strlen);
  635.                 }
  636.         }
  637. }


  638. #$_ [0] [1] -> FolderA  FolderB  ["copy" or "move"]
  639. sub copymove {
  640.         $fullpatha=$path."/".$_[0];
  641.         $fullpathb=$path."/".$_[1];
  642.         $ok=0;
  643.         if (exists $fold{$fullpatha}) {$ok++;} else {
  644.                 print "ERR: $fullpatha does not exists\n";
  645.         }
  646.         if (exists $fold{$fullpathb}) {$ok++;} else {
  647.                 print "ERR: $fullpathb does not exists\n";
  648.         }
  649.         if ($ok==2) {
  650.                 print "\n";
  651.                 &copytree($fullpatha,$fullpathb);
  652.                 if ($_[2] eq "move") {
  653.                         &deltree($fullpatha,$_[0],"sure");
  654.                 }
  655.         }
  656. }

  657. # $_ [0] [1] [2] FolderName NewFolderName CurrentPath
  658. # 作用:将整个目录树搬到一个新目录名的树中,然后用于重命名。
  659. sub rename {
  660.         #临时机制:当前的目录独立处理,其所有子目录用foreach获取
  661.         #不与当前其他目录混淆
  662.         my $front;
  663.         my $last;
  664.         my $tmp;
  665. ########  Main Folder ######
  666.         $fold{$path."/".$_[1]}=$fold{$path."/".$_[0]};
  667.         $rcder{$path."/".$_[1]}=$rcder{$path."/".$_[0]};
  668.         $item{$path}{$_[1]}="";
  669.         $num{$path."/".$_[1]}=$rcder{$path."/".$_[0]};
  670. ########
  671.         foreach (keys %fold) {
  672.                 if (m#^$path/$_[0]/#) {  #筛选出其路径下所有子目录
  673.                         $tmp=$_;
  674.                         $tmp=~s#^$path/$_[0]/#$path/$_[1]/#;
  675.                         print ""$_" change to "$tmp"\n";
  676.                         $fold{$tmp}=$fold{$_};    #get info
  677.                         $rcder{$tmp}=$rcder{$_};  #get notes
  678.                         $num{$tmp}=$rcder{$_};    #num in array
  679.                         @tmp=split "/",$tmp;
  680.                         $last=pop @tmp;
  681.                         $front=join "/",@tmp;
  682.                         $item{$front}{$last}="";
  683.                 }
  684.         }
  685.         &deltree($path."/".$_[0],$_[0],"sure");
  686.         return 1;
  687. }

  688. # $_[0] [1] [2] FullPathA FullPathB (A to B)
  689. sub copytree {
  690.         my $path=$_[0];
  691.         my $arr_n;
  692.         my $last;
  693.         my @test;

  694.            @test=split "/",$path;
  695.         $last=pop @test;

  696.         print "Copy $_[0] -> $_[1]\n";
  697.         $item{$_[1]}{$last}="";                        #MARK1
  698.         $fold{$_[1]."/".$last}=$fold{$_[1]};        #MARK2
  699.         $num=0;
  700.         foreach ( @{$rcder{$_[0]}}) {
  701.                         $rcder{$_[1]."/".$last}[$num++]=$_;
  702.         }
  703.         $num{$_[1]."/".$last}=$num;        #MARK3
  704.         foreach (keys %{$item{$_[0]}}) {$arr_n++;}

  705.         if ($arr_n>0) {
  706.                 foreach (sort keys %{$item{$_[0]}}) {
  707.                         &copytree(
  708.                                 $_[0]."/".$_,
  709.                                 $_[1]."/".$last
  710.                         );
  711.                 }
  712.         }
  713. }


  714. # $_ [0] [1] [2]->  FullPath LastName Sure?
  715. sub deltree {
  716.         if (! $_[1]) {
  717.                 print "ERR: no arg2. notice for programer\n";
  718.                 return 0;
  719.         }
  720.         if ($_[2] eq "sure") {
  721.                 $sure="y";
  722.         } else {               
  723.                 print "Folder $_[0] dose exists\n";
  724.                 print "DelTree - Are you sure ? (Y/N): ";
  725.                 $sure=<STDIN>;
  726.         }
  727.         if ($sure=~/y/i) {
  728.                 delete $item{$path}{$_[1]};  #path是全局变量 $_[1]是lastname
  729.                 foreach (keys %fold) {
  730.                         if (/^$_[0](\/|$)/s) {                #20101219修改,还应该辨认结尾是否为/ 否则会误删 例如:
  731.                                                         # rd /a 却删除了 /a1 /a2  而一个目录也有可能是结尾所以没有/
  732.                                 print "Delete $_\n";
  733.                                 delete $fold{$_};
  734.                                 delete $rcder{$_};
  735.                                 delete $item{$_};
  736.                         }
  737.                 }
  738.         }
  739. }

  740. #list record note of path
  741. # @_ [0] [1] [2] -> Path DepthLevel MaxDepth
  742. sub listnote {
  743.         my ($path,$level,$max)=@_;
  744.         my $n=0;
  745.         $level++;
  746.         if ($path=~m#(/\w+)$#) {$path=$1;}


  747.         if ($_[1]>0) {
  748.                 if ($_[0] eq "") {
  749.                         print "/\n";
  750.                 } else {
  751.                         print "  "x$_[1] . "$path\n";
  752.                 }
  753.         } else {
  754.                 print "Notes:\n";
  755.         }

  756.         foreach ( @{$rcder{$_[0]}}) {
  757.                 if ($level==1) {
  758.                         printf "    "x$_[1] . "%2d %s",$n++,$_;
  759.                 } else {
  760.                         print "    "x$_[1] . "$_";
  761.                 }
  762.         }
  763.         if (($level<$max) or ($max eq "all")) {
  764.                 foreach (sort keys %{$item{$_[0]}}) {
  765.                         &listnote($_[0]."/".$_ , $level , $_[2]);
  766.                 }
  767.         }
  768. }

  769. # @_ [0] [1] [2] [3] [4] - >  
  770. # PathNow DepthLevel(0Begin) MaxDepth STR_space_n Last?
  771. sub listx {
  772.         my ($path,$level,$max)=@_;
  773.         my $tmpstr;
  774.         my $arr_n;
  775.         my $test;
  776.         my $last;
  777.            @test=split "/",$path;

  778.         $str=substr($_[3],0,-4);

  779.         if ($_[4]==0) {$next="+---";} else {$next="\\---";}
  780.         if ($level>0) {
  781.                 print "$str$next$test[$#test]\n";
  782.         } else {
  783.                 print "Notes:\n";
  784.         }
  785.         #第一层,是当前目录,不显示当前目录,只显示下面层的

  786.         foreach (keys %{$item{$_[0]}}) {$arr_n++;}
  787.         if (($level!=$max) and ($arr_n>0)) {
  788.                 $next2="|";
  789.         } else {
  790.                 $next2=" ";
  791.         }

  792.         foreach ( @{$rcder{$_[0]}}) {print "$_[3]$next2  $_";}
  793.         if (defined $rcder{$_[0]}[0]) {print "$_[3]$next2\n";}

  794.         if ($arr_n>0) {
  795.         if (($level<$max) or ($max eq "all")) {
  796.         $last=0;$test=0;
  797.                 foreach (sort keys %{$item{$_[0]}}) {
  798.                         if ($test<($arr_n-1)) {
  799.                                 $tmpstr="|   ";
  800.                         } else {
  801.                                 $tmpstr="    ";
  802.                                 $last=1;
  803.                         }
  804.                         &listx( $_[0]."/".$_ ,
  805.                                 $level+1 ,
  806.                                 $max ,
  807.                                 $_[3].$tmpstr,
  808.                                 $last
  809.                         );
  810.                         $test++;
  811.                 }
  812.         }
  813.         }
  814. }

  815. sub clear {
  816.         system("cls");
  817.         if ($path eq "") {
  818.                 print "Path:/ \n\n";
  819.         } else {
  820.                 print "Path:$path \n\n";
  821.         }
  822. }

  823. sub SayRootErr {
  824.         print "Sorry,in the Root Directory,";
  825.         print "dose not support writting\n\n";
  826. }

复制代码

评分

参与人数 1技术 +1 收起 理由
plp626 + 1 以后向你请教perl了。。。

查看全部评分

发表于 2011-5-21 19:12:15 | 显示全部楼层
exist你真能整,882行。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 03:10 , Processed in 0.022094 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表