VB icon

Bulk File Renamer

Email
Submitted on: 8/29/2016 7:04:02 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1559
 
     This is a windows file renamer that can take a folder path and file type and rename all the files with that file type, and rename to the file type you added. Hope you find this code usfull.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Bulk File Renamer
// Description:This is a windows file renamer that can take a folder path and file type and rename all the files with that file type, and rename to the file type you added. Hope you find this code usfull.
// By: Ben
//**************************************

/*	Bulk File Renamer v1.0
	By DreamVB
	Usage rename.exe c:\work\*.doc .txt
*/
#include <iostream>
#include <Windows.h>
using namespace std;
using std::cout;
using std::endl;
int ChrPos(char *s, char ch){
	int idx = -1;
	int x = 0;
	//Find the last backslash in lzCmd
	for (x = 0; x < strlen(s); x++){
		if (s[x] == ch){
			idx = x;
		}
	}
	return idx;
}
int main(int argc, char *argv[]){
	HANDLE f = 0;
	WIN32_FIND_DATAA wfd;
	char lzPath[MAX_PATH];
	char lzCmd[MAX_PATH];
	char OrgFile[MAX_PATH];
	char NewFile[MAX_PATH];
	char fExtension[30];
	int pos = -1;
	int x = 0;
	//Check argv
	if (argc != 3){
		cout << "Usage: <Folder> <FileType>" << endl;
		exit(1);
	}
	//The two lines below copy the filename and file type form argv
	strcpy(lzCmd, argv[1]);
	strcpy(fExtension, argv[2]);
	strcpy(lzPath, lzCmd);
	//Find the last backslash in lzPath
	pos = ChrPos(lzPath, '\\');
	if (pos != -1){
		//Terminate string
		lzPath[pos + 1] = '\0';
		//Reset pos
		pos = -1;
	}
	//Find first file.
	f = FindFirstFileA(lzCmd, &wfd);
	//Check for inaild path.
	if (f == INVALID_HANDLE_VALUE){
		cout << "Folder or files not found." << endl << lzPath << endl;
		exit(1);
	}
	//Gets the list of files
	do{
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
			//Left for folders
		}
		else{
			//Build path and filename
			strcpy(OrgFile, lzPath);
			strcat(OrgFile, wfd.cFileName);
			//Set new filename
			strcpy(NewFile, OrgFile);
			//Remove file type from NewFile
			pos = ChrPos(NewFile, '.');
			//Append new file type to NewFile
			if (pos != -1){
				//Terminate string
				NewFile[pos] = '\0';
				//Append extension to new file
				strcat(NewFile, fExtension);
				//Rename the file
				if (rename(OrgFile, NewFile) != 0){
					//Output error
					cout << "IO/Error, Cannot rename file." <<
						endl << NewFile << endl;
				}
			} 
		}
	} while (FindNextFileA(f, &wfd) != false);
	//Close file
	FindClose(f);
	//Clear up.
	memset(lzCmd, 0, sizeof(lzCmd));
	memset(lzPath, 0, sizeof(lzPath));
	memset(OrgFile, 0, sizeof(OrgFile));
	memset(NewFile, 0, sizeof(NewFile));
	memset(fExtension, 0, sizeof(fExtension));
	return EXIT_SUCCESS;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.