I want to remove a particular substring from all the file names in a directory:
我想要从目录中的所有文件名中删除一个特定的子字符串:
-- like 'XYZ.com' from 'Futurama s1e20 - [XYZ.com].avi' --
——比如来自Futurama s1e20 - [XYZ.com].avi的“XYZ.com”。
So basically I need to provide the method with a desired substring, and it has to loop through all file names and compare.
因此,基本上我需要为方法提供所需的子字符串,并且它必须遍历所有文件名并进行比较。
I cant figure out how to loop through all files in a folder using C.
我不知道如何使用C来遍历文件夹中的所有文件。
7 个解决方案
#1
5
You may use man 3 fts to loop through all files in a folder using C:
您可以使用man 3 fts对文件夹中的所有文件进行循环,使用C:
http://keramida.wordpress.com/2009/07/05/fts3-or-avoiding-to-reinvent-the-wheel/
http://keramida.wordpress.com/2009/07/05/fts3-or-avoiding-to-reinvent-the-wheel/
#2
8
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char** argv)
{
struct dirent *dp;
DIR *dfd;
char *dir ;
dir = argv[1] ;
if ( argc == 1 )
{
printf("Usage: %s dirname\n",argv[0]);
return 0;
}
if ((dfd = opendir(dir)) == NULL)
{
fprintf(stderr, "Can't open %s\n", dir);
return 0;
}
char filename_qfd[100] ;
char new_name_qfd[100] ;
while ((dp = readdir(dfd)) != NULL)
{
struct stat stbuf ;
sprintf( filename_qfd , "%s/%s",dir,dp->d_name) ;
if( stat(filename_qfd,&stbuf ) == -1 )
{
printf("Unable to stat file: %s\n",filename_qfd) ;
continue ;
}
if ( ( stbuf.st_mode & S_IFMT ) == S_IFDIR )
{
continue;
// Skip directories
}
else
{
char* new_name = get_new_name( dp->d_name ) ;// returns the new string
// after removing reqd part
sprintf(new_name_qfd,"%s/%s",dir,new_name) ;
rename( filename_qfd , new_name_qfd ) ;
}
}
}
Although I would personally prefer a script to do this job like
虽然我个人更喜欢一个脚本来完成这项工作。
#!/bin/bash -f
dir=$1
for file in `ls $dir`
do
if [ -f $dir/$file ];then
new_name=`echo "$file" | sed s:to_change::g`
mv $dir/$file $dir/$new_name
fi
done
#3
4
I know this answer will get me down-voted, but your problem is perfect for a shell script, (or .cmd script), a PHP script, or PERL script. Doing it in C is more work than the problem is worth.
我知道这个答案会让我失望,但是您的问题对于shell脚本(或者.cmd脚本)、PHP脚本或PERL脚本来说是非常完美的。用C来做比问题本身更重要。
#5
1
The key functions are _findfirst, _findnext and _findclose
关键函数是_findfirst、_findnext和_findclose。
struct _finddata_t file_info;
char discard[] = "XYZ.com";
char dir[256] = "c:\\folder\\";
char old_path[256];
char new_path[256];
intptr_t handle = 0;
memset(&file_info,0,sizeof(file_info));
strcpy(old_path,dir);
strcat(old_path,"*.avi");
handle = _findfirst(old_path,&file_info);
if (handle != -1)
{
do
{
char *new_name = NULL;
char *found = NULL;
new_name = strdup(file_info.name);
while ((found = strstr(new_name,discard)) != 0)
{
int pos = found - new_name;
char* temp = (char*)malloc(strlen(new_name));
char* remain = found+strlen(discard);
temp[pos] = '\0';
memcpy(temp,new_name,pos);
strcat(temp+pos,remain);
memcpy(new_name,temp,strlen(new_name));
free(temp);
}
strcpy(old_path,dir);
strcat(old_path,file_info.name);
strcpy(new_path,dir);
strcat(new_path,new_name);
rename(old_path,new_path);
free(new_name);
}while(_findnext(handle,&file_info) != -1);
}
_findclose(handle);
#6
0
fts
has a nice interface, but it's 4.4BSD and is not portable. (I recently got bitten in the rear by some software with an inherent dependency on fts.) opendir
and readdir
are less fun but are POSIX standards and are portable.
fts有一个很好的接口,但是它是4.4BSD,而且是不可移植的。(我最近被一些带有对fts的固有依赖的软件咬了。)opendir和readdir不那么有趣,但它们是POSIX标准,并且是可移植的。
#7
0
fts(3) is 4.4BSD, Linux, Mac OS X, ... Just FYI!
fts(3)是4.4BSD, Linux, Mac OS X,…仅供参考!
#1
5
You may use man 3 fts to loop through all files in a folder using C:
您可以使用man 3 fts对文件夹中的所有文件进行循环,使用C:
http://keramida.wordpress.com/2009/07/05/fts3-or-avoiding-to-reinvent-the-wheel/
http://keramida.wordpress.com/2009/07/05/fts3-or-avoiding-to-reinvent-the-wheel/
#2
8
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char** argv)
{
struct dirent *dp;
DIR *dfd;
char *dir ;
dir = argv[1] ;
if ( argc == 1 )
{
printf("Usage: %s dirname\n",argv[0]);
return 0;
}
if ((dfd = opendir(dir)) == NULL)
{
fprintf(stderr, "Can't open %s\n", dir);
return 0;
}
char filename_qfd[100] ;
char new_name_qfd[100] ;
while ((dp = readdir(dfd)) != NULL)
{
struct stat stbuf ;
sprintf( filename_qfd , "%s/%s",dir,dp->d_name) ;
if( stat(filename_qfd,&stbuf ) == -1 )
{
printf("Unable to stat file: %s\n",filename_qfd) ;
continue ;
}
if ( ( stbuf.st_mode & S_IFMT ) == S_IFDIR )
{
continue;
// Skip directories
}
else
{
char* new_name = get_new_name( dp->d_name ) ;// returns the new string
// after removing reqd part
sprintf(new_name_qfd,"%s/%s",dir,new_name) ;
rename( filename_qfd , new_name_qfd ) ;
}
}
}
Although I would personally prefer a script to do this job like
虽然我个人更喜欢一个脚本来完成这项工作。
#!/bin/bash -f
dir=$1
for file in `ls $dir`
do
if [ -f $dir/$file ];then
new_name=`echo "$file" | sed s:to_change::g`
mv $dir/$file $dir/$new_name
fi
done
#3
4
I know this answer will get me down-voted, but your problem is perfect for a shell script, (or .cmd script), a PHP script, or PERL script. Doing it in C is more work than the problem is worth.
我知道这个答案会让我失望,但是您的问题对于shell脚本(或者.cmd脚本)、PHP脚本或PERL脚本来说是非常完美的。用C来做比问题本身更重要。
#4
#5
1
The key functions are _findfirst, _findnext and _findclose
关键函数是_findfirst、_findnext和_findclose。
struct _finddata_t file_info;
char discard[] = "XYZ.com";
char dir[256] = "c:\\folder\\";
char old_path[256];
char new_path[256];
intptr_t handle = 0;
memset(&file_info,0,sizeof(file_info));
strcpy(old_path,dir);
strcat(old_path,"*.avi");
handle = _findfirst(old_path,&file_info);
if (handle != -1)
{
do
{
char *new_name = NULL;
char *found = NULL;
new_name = strdup(file_info.name);
while ((found = strstr(new_name,discard)) != 0)
{
int pos = found - new_name;
char* temp = (char*)malloc(strlen(new_name));
char* remain = found+strlen(discard);
temp[pos] = '\0';
memcpy(temp,new_name,pos);
strcat(temp+pos,remain);
memcpy(new_name,temp,strlen(new_name));
free(temp);
}
strcpy(old_path,dir);
strcat(old_path,file_info.name);
strcpy(new_path,dir);
strcat(new_path,new_name);
rename(old_path,new_path);
free(new_name);
}while(_findnext(handle,&file_info) != -1);
}
_findclose(handle);
#6
0
fts
has a nice interface, but it's 4.4BSD and is not portable. (I recently got bitten in the rear by some software with an inherent dependency on fts.) opendir
and readdir
are less fun but are POSIX standards and are portable.
fts有一个很好的接口,但是它是4.4BSD,而且是不可移植的。(我最近被一些带有对fts的固有依赖的软件咬了。)opendir和readdir不那么有趣,但它们是POSIX标准,并且是可移植的。
#7
0
fts(3) is 4.4BSD, Linux, Mac OS X, ... Just FYI!
fts(3)是4.4BSD, Linux, Mac OS X,…仅供参考!