免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6950 | 回复: 18
打印 上一主题 下一主题

怎么样为一个目录下的文件全部重命名 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-30 13:55 |只看该作者 |倒序浏览
本帖最后由 mmh 于 2010-03-30 14:49 编辑

windows系统,目录下有一些文件。
文件名为:
CODEOU22(Error)20100330073553.txt
CODEOU45(Error)20100330072350.txt
CODEOU80(Error)20100330070253.txt
CODEOU81(Error)20100330071453.txt
想取到(Error)之前和之后的字母和数字为文件重新命名。

论坛徽章:
0
2 [报告]
发表于 2010-03-30 14:17 |只看该作者
比如重命名为www.
s/(\w+)\(/www\(/g;
不知道理解对不对

你可以看看这个。  
?: 表示非捕获符号。就是什么也不匹配。
(?!pattern) 如果正则表达式在后面不匹配 pattern ,才会开始匹配。如/foo(?!bar)/,只有当出现 foo,并且后面不出现 bar 时才开始匹配.




Type                       Regex            Successful if the enclosed subexpression ...

Positive Lookbehind       (?<=......)        successful if can match to the left                        #例子:"abcdefg" =~ m/(?<=c)/;
                                                                                                                print $`,$&,$';

Negative Lookbehind       (?<!......)        successful if can not match to the left

Positive Lookahead        (?=......)         successful if can match to the right                       #例子: "abcdefg" =~ m/(?=c)/;
                                                                                                                print $`,$&,$';

Negative Lookahead        (?!......)         successful if can not match to the right



                                      Example Showing After-Match Special Variables After the match of

                                   12               2 3   4     4 31
    "Pi is 3.14159, roughly" =~ m/\b((tasty|fattening)|(\d+(\.\d*)?))\b/;

the following special variables are given the values shown.

Variable                 Meaning                                                 Value

$'                  Text before match                                            Pi?is?

$&                  Text matched                                                 3.14159

$'                  Text after match                                             , ?roughly

$1                  Text matched within 1st set of parentheses                   3.14159

$2                  Text matched within 2nd set of parentheses                   undef

$3                  Text matched within 3rd set of parentheses                   3.14159

$4                  Text matched within 4th set of parentheses                   .14159

$+                  Text from highest-numbered $1, $2, etc.                      .14159

$^N                 Text from most recently closed $1, $2, etc.                  3.14159

@-                  Array of match-start indices into target text                (6, 6, undef, 6, 7)

@+                  Array of match-end indices into target text                  (13, 13, undef, 13, 13)

论坛徽章:
0
3 [报告]
发表于 2010-03-30 14:48 |只看该作者
就是想取到(Error)之前和之后的字母和数字

论坛徽章:
0
4 [报告]
发表于 2010-03-30 15:06 |只看该作者
回复 3# mmh


   
CODEOU22(Error)20100330073553.txt  -> CODEOU2220100330073553.txt ??

论坛徽章:
0
5 [报告]
发表于 2010-03-30 15:10 |只看该作者
是的

论坛徽章:
0
6 [报告]
发表于 2010-03-30 15:41 |只看该作者
本帖最后由 yigenjin 于 2010-03-30 15:43 编辑

for f in /path/to/*; do
  newname=`echo $f | sed -e 's/(Error)//g'`
  mv $f $newname
done

sed的语法忘了,大致就这么个东西

论坛徽章:
0
7 [报告]
发表于 2010-03-30 15:43 |只看该作者
我是windows没有sed 只能用perl

论坛徽章:
0
8 [报告]
发表于 2010-03-30 15:46 |只看该作者
本帖最后由 小小稻草人 于 2010-03-30 15:48 编辑
  1. use 5.010;
  2. use strict;
  3. use warnings;
  4. use diagnostics;

  5. use File::Spec;

  6. use File::Copy;

  7. opendir(DIR,"e:\\perl_test") or die "Can't open the current directory:$!\n";
  8. my @file=readdir(DIR);
  9. close DIR;

  10. foreach my $i (@file){
  11.         if ($i =~/\)/){
  12.                 print $i."\n";
  13.                 print $'."\n";
  14.                 copy("$i","CODEOU$'") or die "Rename is failed:$!\n";
  15.                
  16.         }
  17. }
复制代码

论坛徽章:
0
9 [报告]
发表于 2010-03-30 16:27 |只看该作者
回复 8# 小小稻草人


出错了:
    Uncaught exception from user code:
        Rename is failed:No such file or directory
at D:/Documents and Settings/Administrator/workspace/perl/222.pl line 21

有的文件是后面的数据是一样的像这两个:
CODEOU80(Error)20100330141419.txt
CODEOU81(Error)20100330141419.txt

所以只有全取到才可以,如果把(Error)改了也可以!!

论坛徽章:
0
10 [报告]
发表于 2010-03-30 16:35 |只看该作者
回复 9# mmh

你也没有给列出相关文件名!
改成这样:
  1. foreach my $i (@file){
  2.         if ($i =~/(\w+)\(Error\)/){
  3.                 copy ("$i","$1$'") or die "Rename is failed:$!\n";
  4.                
  5.         }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP