Free utility to rename all files in a folder.

Discussion in 'C++' started by shabbir, Aug 18, 2008.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    Background



    I happen to manage many sites this days and so at times I need to upload lots of things to my servers and when uploading from windows PC to Linux servers the biggest problem that comes up is the case sensitive issues and so I tried googling it but what I found are some cool utilities that integrate well in your explorer for some good amount of $$ and so thought why not save that $$ for some promotion of this site and so I created this utility.

    The code



    It converts all the filename to lower characters as well as replace any other characters apart from a-z 0-9 and . to underscor _

    Code:
    #include <iostream>
    #include <windows.h> // For File related stuff.
    #include <direct.h> // For _getcwd
    #include <string>
    using namespace std;
    
    int ApplyRenameLogic(string&);
    
    int main()
    {
    	char currentPath[_MAX_PATH],tempPath[_MAX_PATH];
    	
    	_getcwd((char*)currentPath, _MAX_PATH);
    	strcat_s(currentPath, "\\");
    	strcpy_s(tempPath,currentPath);
    	strcat_s(tempPath, "*.*");
    	
    	cout<<"Current Directory = "<<currentPath<<endl;
    
    	WIN32_FIND_DATA Win32FD;
    	ZeroMemory(&Win32FD,sizeof(WIN32_FIND_DATA));
    
    	HANDLE hFile = ::FindFirstFile((LPCSTR)tempPath,&Win32FD);
    
    	if(hFile == INVALID_HANDLE_VALUE)
    		return FALSE;
    	
    	do
    	{
    		// Skip current folder ".", parent folder ".."
    		if( _strcmpi((const char*)Win32FD.cFileName, (const char*)TEXT(".")) == 0 || 
    			_strcmpi((const char*)Win32FD.cFileName, (const char*)TEXT("..")) == 0 )
    			continue;
    		
    		// Skip hidden files
    		if( Win32FD.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )
    			continue;
    
    		string oldfilename, filename;
    		filename.insert(0,(char*)(&Win32FD.cFileName[0]));
    		oldfilename.insert(0,(char*)(&Win32FD.cFileName[0]));
    		
    		cout<<"File name before "<<filename<<endl;
    		
    		ApplyRenameLogic(filename);
    
    		oldfilename.insert(0,currentPath);
    		filename.insert(0,currentPath);
    
    		int iRes = rename((const char*)oldfilename.c_str(),(const char*)filename.c_str());
    		DWORD e = GetLastError();
    		cout<<"    After "<<filename<<"\n"<<endl;
    
    	} while( ::FindNextFile(hFile, &Win32FD) != 0 );
    	
    	FindClose(hFile);
    
    	return 0;
    }
    
    
    int ApplyRenameLogic(string& fileName)
    {
    	//loop through each character and make it lower-case. stop when you hit '\0'.
    	for(int i = 0; fileName[i] != '\0'; i++)
    	{
    		fileName[i] = tolower(fileName[i]);
    		// Do not replace the extension
    		if(fileName[i] == '.')
    			continue;
    		if((fileName[i] < 'a' || fileName[i] > 'z') && 
    			(fileName[i] < '0' || fileName[i] > '9'))
    		{
    			fileName[i] = '_';
    		}
    	}
    	return TRUE;
    }
     
  2. soham_701

    soham_701 New Member

    Joined:
    Jun 25, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i have seen good piece of resource programming , but i havn't found any tutorials on that.
    Can u plz provide me some .pdf tutorials on that or any books.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please don't jump in any article with your request.
     
  4. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    It is interesting to compare the C++ version with a Perl version!
    Code:
    while (<*>) {
       $old = $_;
       $_ = lc;
       s/[^a-z0-9.]/_/g;
       rename $old, $_;
    }
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  6. happyz

    happyz New Member

    Joined:
    Nov 18, 2008
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    havnt done anytype of programing like this....will try this code
     
  7. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    That is good program. thanks for sharing it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice