rendered paste body//============================================================================// Name : Renamer.cpp// Author : Alexander Bliskovsky// Version : 1.05// Copyright : Copyright Alexander Bliskovksy 2007// Description : Renamer//============================================================================#include <fstream>#include <iostream>#include <stdio.h>#include <string.h>#include <windows.h>using namespace std;int oldNamesCount;int newNamesCount;int errorCount;int main ( int argc, char *argv[] ) { char newFileName[256]; char oldFileName[256]; MessageBox( NULL , "This program is still under development. Please make sure you have a backup of the files you are renaming." , "Backup Warning" , MB_OK ); WIN32_FIND_DATA FindFileData; HANDLE hFind= INVALID_HANDLE_VALUE; char DirSpec[5]= "."; // directory specification strncat(DirSpec, "\\*", 3); hFind = FindFirstFile(DirSpec, &FindFileData); int fileOutputCount = 0; ofstream a_file("oldnames.txt"); if (a_file.is_open() ) { while (FindNextFile(hFind, &FindFileData) != 0) { ++fileOutputCount; if (fileOutputCount > 1) { if (strcmp(FindFileData.cFileName, "Renamer.exe") != 0) if (strcmp(FindFileData.cFileName, "oldnames.txt") != 0) if (strcmp(FindFileData.cFileName, "index.txt") != 0) { a_file<<FindFileData.cFileName<<"\n"; oldNamesCount++; } } } } FindClose(hFind); a_file.close(); ifstream check("index.txt"); //Check for existance of index.txt if (check.is_open() ) { FILE* fp_fileNames = fopen("index.txt", "r"); FILE* fp_oldNames = fopen("oldnames.txt", "r"); FILE* fp_fileToName; char buf[256]; while (fgets(buf, sizeof(buf), fp_fileNames) != NULL) { newNamesCount++; } rewind(fp_fileNames); if (oldNamesCount == newNamesCount) { // Output names of files to be renamed. cout<< "The following files will be renamed (<oldname> to <newname>): \n"; for (int i = 0; i < newNamesCount; i++) { char checkOriginal[101]; char checkNew[101]; //Get Filenames fgets(checkOriginal, 100, fp_oldNames); char *o = strchr(checkOriginal, '\n'); //Find/Remove newline char if (o) *o = '\0'; fgets(checkNew, 100, fp_fileNames); char *p = strchr(checkNew, '\n'); //Find/Remove newline char if (p) *p = '\0'; strcat(checkNew, ".ab1"); printf("%-30s %s \n", checkOriginal, checkNew); } rewind(fp_oldNames); rewind(fp_fileNames); cout<<"Press enter to continue."; cin.get(); for (int x = 0; x < newNamesCount; x++) //Actual Renaming { fgets(oldFileName, 255, fp_oldNames); char *o = strchr(oldFileName, '\n'); //Find/Remove newline char if (o) *o = '\0'; fgets(newFileName, 255, fp_fileNames); char *p = strchr(newFileName, '\n'); //Find/Remove newline char if (p) *p = '\0'; strcat(newFileName, ".ab1"); //append extention fp_fileToName = fopen(oldFileName, "r"); /* open file just to check if it exists */ if (fp_fileToName != NULL) /* check if file exists */ { fclose(fp_fileToName); int result; result = rename(oldFileName, newFileName); if (result == 0) cout<<"Renamed "<< oldFileName <<" to "<< newFileName <<" \n"; else { perror("Error renaming file"); errorCount++; } } else { cout<< "Error, "<< oldFileName <<" does not exist. (To be renamed to " << newFileName <<" ) \n"; errorCount++; } } if (errorCount == 0) { MessageBox( NULL , "Renaming completed successfully." , "Completed Successfully" , MB_OK ); } else { char renameError[101] = ""; char errorCountChar[4]; sprintf(errorCountChar, "%i", errorCount); strcat(renameError, errorCountChar); if (errorCount > 1 ) strcat(renameError, " errors encountered."); else strcat(renameError, " error encountered."); MessageBox( NULL , renameError , "Renaming failed" , MB_OK ); } } else { // Build and display "Line Mismatch" message char LineMismatch[401] = "Line Mismatch. The number of lines in index.txt must be the same as the number of files to be renamed in the folder. \n\nLines in index.txt: "; char LineMismatch2[51] = "\nFiles to be renamed: "; char newNamesChar[4]; char oldNamesChar[4]; //Convert Int to Char sprintf(oldNamesChar, "%i", oldNamesCount); sprintf(newNamesChar, "%i", newNamesCount); //Concat messagebox text strcat(LineMismatch, newNamesChar); strcat(LineMismatch2, oldNamesChar); strcat(LineMismatch, LineMismatch2); MessageBox( NULL , LineMismatch , "Error", MB_OK); } } else { MessageBox( NULL ,"index.txt does not exist. \nAn index.txt has been created for you. Please populate it with file names.\nRenamer will now terminate.", "Error", MB_OK); ofstream indexcreate("index.txt"); } remove("oldnames.txt"); return 1; } // main