Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Free utility to rename all files in a folder. (http://www.go4expert.com/articles/free-utility-rename-files-folder-t13027/)

shabbir 18Aug2008 20:00

Free utility to rename all files in a folder.
 

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 19Aug2008 21:06

Re: Free utility to rename all files in a folder.
 
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 19Aug2008 22:16

Re: Free utility to rename all files in a folder.
 
Please don't jump in any article with your request.

oogabooga 29Aug2008 01:14

Re: Free utility to rename all files in a folder.
 
It is interesting to compare the C++ version with a Perl version!
Code:

while (<*>) {
  $old = $_;
  $_ = lc;
  s/[^a-z0-9.]/_/g;
  rename $old, $_;
}


shabbir 5Sep2008 23:00

Re: Free utility to rename all files in a folder.
 
Nominate for article of the month for August 2008

happyz 22Nov2008 19:34

Re: Free utility to rename all files in a folder.
 
havnt done anytype of programing like this....will try this code

skp819 13Jan2009 14:37

Re: Free utility to rename all files in a folder.
 
That is good program. thanks for sharing it.


All times are GMT +5.5. The time now is 13:13.