1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Free utility to rename all files in a folder.

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

  1. shabbir

    shabbir Administrator Staff Member

    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

    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

    Please don't jump in any article with your request.
     
  4. oogabooga

    oogabooga New Member

    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

  6. happyz

    happyz New Member

    havnt done anytype of programing like this....will try this code
     
  7. skp819

    skp819 New Member

    That is good program. thanks for sharing it.
     

Share This Page