QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1002|回复: 5

怎样在控制台中批量改文件名?

[复制链接]
发表于 2003-7-17 16:58:24 | 显示全部楼层 |阅读模式
比如某个文件夹中有 一些文件,现在需要在所有的文件名后面加上扩展名htm...

我试过了mv , rename 好像都不行啊?
而且在KDE文件管理器中也做不到...8-(

请问有没有类似win中TotalCommander那样的可以批量改名的东东?
发表于 2003-7-18 17:20:57 | 显示全部楼层
写script吧,perl里面有很多文件操作的函数的
回复

使用道具 举报

发表于 2003-7-21 09:43:59 | 显示全部楼层
Oh~not that hard..Try this script
[code:1]
#!/bin/sh
for i in `echo *`
do
    mv $i ${i}.htm   #change your file name here!
done
[/code:1]
回复

使用道具 举报

发表于 2003-8-6 22:10:59 | 显示全部楼层
如果文件名有空格,怎么办?
回复

使用道具 举报

发表于 2003-8-12 11:26:28 | 显示全部楼层
[quote:09aafeaa76="wanghixxyy"]如果文件名有空格,怎么办?[/quote]
Try this: it can change all file names in  current directory.

[code:1]
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define SUFFIX ".html"
int main( int argc, char **argv )
{
    DIR *dir;
    struct dirent *dp;
    char s[255];
   
   if ( ( dir = opendir(".") ) == NULL )
       perror("opendir");
   while ( ( dp = readdir(dir) ) != NULL )
   {
       if ( strcmp( dp->d_name , "." ) == 0 ||
               strcmp( dp->d_name, ".." ) == 0 )
           continue;
      
       strcpy( s, dp->d_name );
       strcat( s, SUFFIX );
       rename( dp->d_name, s );
       printf( "%s ==> %s\n", dp->d_name, s );
   }
   closedir(dir);
   return 0;
}
[/code:1]
回复

使用道具 举报

发表于 2003-8-13 01:11:34 | 显示全部楼层
[quote:7321ccf70c="Dasn"][quote:7321ccf70c="wanghixxyy"]如果文件名有空格,怎么办?[/quote]
Try this: it can change all file names in  current directory.

[code:1]
#include <dirent.h>
#include <string.h>
#include <stdio.h>
……
……
……
[/code:1][/quote]

多谢多谢!
我打算把它段代码用python来重写一下,然后再使它支持命令行参数,这样便于控制后缀名。

还有,我想知道能不能就利用linux现有的基本程序来做到这点
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-9-20 16:47 , Processed in 0.053866 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

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