Free utility to rename all files in a folder.

shabbir's Avatar author of Free utility to rename all files in a folder.
This is an article on Free utility to rename all files in a folder. in C++.
Rated 5.00 By 1 users

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: Cpp
#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;
}
Newbie Member
19Aug2008,21:06   #2
soham_701's Avatar
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.
Go4Expert Founder
19Aug2008,22:16   #3
shabbir's Avatar
Please don't jump in any article with your request.
Ambitious contributor
29Aug2008,01:14   #4
oogabooga's Avatar
It is interesting to compare the C++ version with a Perl version!
Code:
while (<*>) {
   $old = $_;
   $_ = lc;
   s/[^a-z0-9.]/_/g;
   rename $old, $_;
}
Go4Expert Founder
5Sep2008,23:00   #5
shabbir's Avatar
Nominate for article of the month for August 2008
Go4Expert Member
22Nov2008,19:34   #6
happyz's Avatar
havnt done anytype of programing like this....will try this code
Contributor
13Jan2009,14:37   #7
skp819's Avatar
That is good program. thanks for sharing it.