How would I rename a folder?

This is a discussion on How would I rename a folder? within the C++ Programming forums, part of the General Programming Boards category; How would I rename a folder using C++? without using a system(); command, i need something like renameFolder() or something, ...

  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
    Portugal
    Posts
    7,510
    Depends on the operating system.

    libraries like boost.filesystem provide portable access.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 08:18 AM
  3. Hiding a folder
    By nextstep in forum Windows Programming
    Replies: 16
    Last Post: 03-20-2005, 02: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, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21