Is there a way to do even do this? I want to store the names in an array and then rename them.
Is there a way to do even do this? I want to store the names in an array and then rename them.
http://www.adrianxw.dk/SoftwareSite/...irstFile1.html
This would be a good place to start, if you're using Windws.
To rename your files check out the documentation for your compiler.
In Dev++ it's as simple as using the keyword 'Rename'.
Thanks very much for that link. It helped quite a bit. Now to go through the file names and remove spaces.
Can anyone tell me why this is not working:
Code:#include <windows.h> #include <iostream> using namespace std; void fixname(CHAR*,int); void makepath(char*,char*); int main(int argc, char *argv[]) { HANDLE hFind; WIN32_FIND_DATA FindData; char path[100]=""; char ext[4]=""; char quit; cout<<"Please enter the path you would like to rename in the form:\n"; cout<<"C:\Pictures\UglyDog\Sam\n"; cin>>path; cout<<"What extentions would you like to rename?\n"; cin>>ext; makepath(path,ext); hFind=FindFirstFile(path, &FindData); cout<<FindData.cFileName<<endl; fixname(FindData.cFileName,strlen(FindData.cFileName)); while(FindNextFile(hFind, &FindData)) { cout<<FindData.cFileName<<endl; fixname(FindData.cFileName,strlen(FindData.cFileName)); } FindClose(hFind); //cout<<"Done renaming files, please press Ctrl+C to quit"; //cin>>path; return 0; } /*this function seems to fail and I don't understand why*/ void fixname(CHAR* cpt, int length) { for(int x=0;x<length;x++) { if(cpt[x]= ' ') { for(int y=x;y<length;y++) { cpt[y]=cpt[y+1]; } } } } void makepath(char* main,char* end) { int flag=0; for(int x=strlen(main)-1;x!=0;x--) { if(!isspace(main[x])) { while(flag==0) { main[x+1]='/'; main[x+2]='*'; main[x+3]='.'; main[x+4]=end[0]; main[x+5]=end[1]; main[x+6]=end[2]; return; } } } }
The second parameter is an integer, but you are passing a char array. That means length will be some ungodly huge number, probably considerably larger than 100. Probably the length of the string should be passed.Code:void fixname(CHAR* cpt, int length)
secondly, cin>> path will not accept any spaces. you have to use getline() if you want to get a path from the keyboard with spacesCode:makepath(path,strlen(path));
Finally, this is a c++ program -- why are you using C style character arrays (unless of course your instructor told you to do that). std::string class will simplify your program.Code:cin.getline(path,sizeof(path));
>> The second parameter is an integer, but you are passing a char array.
I think you are confusing makepath and fixname. AFAICT makepath takes two character pointers, and is getting two character pointers, fixname takes a character pointer and an int and is getting a character pointer and an int.
I can't see anything wrong with the function (I didn't run it). Maybe some debugging would help, or at least indicate how it is failing and on what input.
Yes, I did get then confused.Originally Posted by Daved
This is wrong -- should use '==' boolean operator, not the assignment operator.Code:if(cpt[x]= ' ')
Think you have to work on the function fixname.
and you should consider the case when there is more then one space in the string.Code:void fixname(CHAR* cpt, int length) { for(int x=0;x<length;x++) { if(cpt[x]= ' ') { for(int y=x;y<length;y++) { cpt[y]=cpt[y+1]; // cpt[y+1] is not a valid index if y == length -1 } } } }
Kurt
>> // cpt[y+1] is not a valid index if y == length -1
Yes it is valid. It will be the terminating null character.
I think Ancient Dragon got it with = vs ==.
> cout<<"C:\Pictures\UglyDog\Sam\n";
Don't forget your \\
> void fixname(CHAR*,int);
> void makepath(char*,char*);
What's the difference between CHAR and char?
> void makepath(char* main,char* end)
Whilst legal, having variable names the same as function names is poor form.
Wow, thanks for all the help, especially the = vs == thing, no wonder things didn't work. I've been doing more RPG coding lately than C++. The reason I'm using C-Strings as opposed to Strings is because of the CHAR vs char thing. I don't know if there is a difference, but the compiler seems to think so and I wasn't going to argue w/ the compiler. And this isn't actually a class program, just something I'm doing on my own. I have 100's of files with spaces in the name and I need to get rid of those spaces to get them to work with an RPG program I'm writing.
And I thought I did take care of string with more than one space in them. I'm usiing a kind of bubble sort thing where I step through each character, starting from the beginning, and if I hit a space I slide down all characters. Then I continue to the next character in the name and repeat the process, there is no returning after only finding one space..
-----------------------------------------------------------------------------------
edit: Okay, I've finished it. If anyone would like to use it for anything, have fun:
Code:/* Program sk.cpp Description: Given the path, this program will rename all files of the given extension to a name without spaces. There may be spaces in the path */ #include <windows.h> #include <iostream> using namespace std; int fixname(int,char*,char*); void makepath(char*,char*); void clean(char*); WIN32_FIND_DATA FindData; int main(int argc, char *argv[]) { HANDLE hFind; char path[128]=""; char path2[128]=""; char newname[128]=""; char ext[4]=""; char fin[]="\\"; char quit; int err; cout<<"Please enter the path you would like to rename in the form:\n"; cout<<"C:\\Pictures\\UglyDog\\Sam\n"; cin.getline(path,sizeof(path)); strcpy(path2,path); strcat(path2,fin); cout<<"What extentions would you like to rename?\n"; cin>>ext; makepath(path,ext); hFind=FindFirstFile(path, &FindData); err=fixname(strlen(FindData.cFileName),newname,path2); clean(newname); if(err!=0) { cout<<"error renaming file"<<endl; } while(FindNextFile(hFind, &FindData)) { err=fixname(strlen(FindData.cFileName),newname,path2); clean(newname); } FindClose(hFind); cout<<"Done renaming files, please press Ctrl+C to quit"; cin>>path; return 0; } int fixname(int length,char* renam, char* pat) { char command[200]=""; char newpath[128]; char oldpath[128]; char blank[]=" "; char qot[]="\""; char comm[]="ren"; int result; strcpy(renam, FindData.cFileName); for(int x=0;x<length;x++) { if(renam[x]== ' ') { for(int y=x;y<length;y++) { renam[y]=renam[y+1]; } } } strcat(command,comm); strcat(command,blank); strcat(command,qot); strcat(command,pat); strcat(command,FindData.cFileName); strcat(command,qot); strcat(command,blank); //beginning of second name strcat(command,qot); strcat(command,renam); strcat(command,qot); cout<<command<<endl; system(command); for(int q=0;q<200;q++) { command[q]=*blank; } } void makepath(char* foo,char* end) { int flag=0; for(int x=strlen(foo)-1;x!=0;x--) { if(!isspace(foo[x])) { while(flag==0) { foo[x+1]='\\'; foo[x+2]='*'; foo[x+3]='.'; foo[x+4]=end[0]; foo[x+5]=end[1]; foo[x+6]=end[2]; return; } } } } void clean(char* target) { char blank=' '; for(int x=0;x<100;x++) { target[x]=blank; } }
Last edited by Dragoon_42; 12-01-2005 at 09:31 AM. Reason: finished code