Thread: Reading the names of files in a filesystem?

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Reading the names of files in a filesystem?

    Is there a way to do even do this? I want to store the names in an array and then rename them.

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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'.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Thanks very much for that link. It helped quite a bit. Now to go through the file names and remove spaces.

  4. #4
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    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;
                    }  
                 }
         }
    }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    void fixname(CHAR* cpt, int length)
    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:
    makepath(path,strlen(path));
    secondly, cin>> path will not accept any spaces. you have to use getline() if you want to get a path from the keyboard with spaces
    Code:
    cin.getline(path,sizeof(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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Daved
    >> The second parameter is an integer, but you are passing a char array.

    I think you are confusing makepath and fixname. .
    Yes, I did get then confused.

    Code:
    if(cpt[x]= ' ')
    This is wrong -- should use '==' boolean operator, not the assignment operator.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Think you have to work on the function fixname.

    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
                      }
                 }
         }
    }
    and you should consider the case when there is more then one space in the string.
    Kurt

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> // 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 ==.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  11. #11
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Reading xls files
    By dlwlsdn in forum C Programming
    Replies: 20
    Last Post: 12-04-2008, 07:38 AM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 10:34 AM
  4. Reading data from consecutively named files
    By a1pro in forum C Programming
    Replies: 10
    Last Post: 04-15-2005, 01:48 AM
  5. Reading Files off the internet
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 02:18 PM