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;
}
soham_701's Avatar, Join Date: Jun 2008
Newbie 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.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Please don't jump in any article with your request.
oogabooga's Avatar
Ambitious contributor
It is interesting to compare the C++ version with a Perl version!
Code:
while (<*>) {
   $old = $_;
   $_ = lc;
   s/[^a-z0-9.]/_/g;
   rename $old, $_;
}
happyz's Avatar, Join Date: Nov 2008
Go4Expert Member
havnt done anytype of programing like this....will try this code
skp819's Avatar
Contributor
That is good program. thanks for sharing it.