If you feel like using Windows C APi, which is very nice...
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
int isDirGood(WIN32_FIND_DATA dir){
return (dir.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& (strcmp(dir.cFileName, "."))
&& (strcmp(dir.cFileName, ".."));
}
int isFileGood(WIN32_FIND_DATA file){
return !(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
void renameFiles(char *startingDir){
HANDLE hTheFiles;
WIN32_FIND_DATA theFiles;
int len;
len = strlen(startingDir);
startingDir[len]='*';
startingDir[len+1]=0;
hTheFiles = FindFirstFile(startingDir, &theFiles);
startingDir[len]=0;
if(hTheFiles==INVALID_HANDLE_VALUE) return;
do{
if(isDirGood(theFiles)){
sprintf(startingDir+len, "%s\\", theFiles.cFileName);
//puts(startingDir);
renameFiles(startingDir);
startingDir[len]=0;
}else if(isFileGood(theFiles)){
//RENAME THE FILE
printf("FILE: %s%s renamed\n", startingDir, theFiles.cFileName);
}
}while(FindNextFile(hTheFiles, &theFiles));
FindClose(hTheFiles);
}
char *dirToFullPath(char *s){
char curDir[MAX_PATH];
if(!s) s = (char*)calloc(1, MAX_PATH);
if(!*s){//no dir specified
GetCurrentDirectory(MAX_PATH, s);
if(s[strlen(s)-1]!='\\')
strcat(s,"\\");
}
else if(s[1]==':'){//full path dir
if(s[strlen(s)-1]!='\\')
strcat(s,"\\");
}
else{//relative dir
GetCurrentDirectory(MAX_PATH, curDir);
if(curDir[strlen(curDir)-1]!='\\')
strcat(curDir,"\\");
if(*s=='\\') s++;
if(s[0] && s[strlen(s)-1]!='\\')
strcat(s,"\\");
strcat(curDir,s);
strcpy(s, curDir);
}
return s;
}
main(){
char s[1000];
strcpy(s, YOUR_DIR_HERE);
dirToFullPath(s);
renameFiles(s);
}
It's all done... just add your renaming criteria to the renameFiles function, and add more arguments to it if you want.
Maybe you want to receive a directory by command line... If so the dirToFullPath is very good because it allows you to write any directory name, and changes it to its fullpath.
e.g: c:\dir\dir2> rename mydir3\mydir2
that input will make the startingDir string look like
"c:\dir\dir2\mydir3\mydir2".
The only problem is to allocate enough memory to the dir name buffer, but that's not really a problem...
Plus... "if(s[1]==':')" this condition works only for windows. If you want to make your app work in other OS you should change that condition.