Thread: How would I rename a folder?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    How would I rename a folder?

    How would I rename a folder using C++?

    without using a system(); command, i need something like renameFolder() or something, thx

    Any help would be appreciated,
    thanks,

    Matt N.
    Last edited by guitarist809; 08-16-2006 at 09:30 AM.
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main ()
    {   
       char oldn[_MAX_PATH], newn[_MAX_PATH];
       int result; 
    
       printf ("Enter the old filename: ");
       gets (oldn); 
    
       printf ("Enter the new filename: ");
       gets (newn); 
    
       result = rename (oldn, newn); 
    
       if (result == 0)
          printf ("File successfully renamed.\n"); 
       else
          perror("Unable to rename file"); 
    }

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Depends on the operating system.

    libraries like boost.filesystem provide portable access.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > gets (oldn);
    Never use gets(). Read the FAQ for why.

    >void main ()
    main returns an int:
    int main()

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Here is the C program for MS-Windows os
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    int main(int argc, char* argv[])
    {
       char oldn[_MAX_PATH], newn[_MAX_PATH];
       int result; 
    
       printf ("Enter the old filename: ");
       fgets (oldn,sizeof(oldn),stdin); 
       if(oldn[strlen(oldn)-1] == '\n')
    	   oldn[strlen(oldn)-1]  = 0;
    
       printf ("Enter the new filename: ");
       fgets (newn,sizeof(newn),stdin); 
       if(newn[strlen(newn)-1] == '\n')
    	   newn[strlen(newn)-1]  = 0;
    
       result = MoveFile (oldn, newn); 
    
       if (result != 0)
          printf ("File successfully renamed.\n"); 
       else
       {
    	   char buf[255] = {0};
    	  DWORD dwError = GetLastError();
    	  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
    	  printf("%s\n",buf);
    	}
    	return 0;
    }

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. read only folder on Windows
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2007, 09:18 AM
  3. Hiding a folder
    By nextstep in forum Windows Programming
    Replies: 16
    Last Post: 03-20-2005, 03:07 PM
  4. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM
  5. rename() problem
    By Dimeslime in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 05:38 PM