Repository navigation
Expand file tree
/
Copy pathRenameFilesThread.cpp
More file actions
144 lines (129 loc) · 5.66 KB
/
Copy pathRenameFilesThread.cpp
File metadata and controls
144 lines (129 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//---------------------------------------------------------------------------
#include <vcl.h>
#include <Registry.hpp>
#include <StrUtils.hpp>
//---------------------------------------------------------------------------
#include "RenameFilesThread.h"
//---------------------------------------------------------------------------
#pragma hdrstop
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall RenameFilesThread::RenameFilesThread(bool CreateSuspended, TFileList* AFileList, AnsiString AGameTag, TLogs* ALog)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
Total = 0;
GameTag = AGameTag;
FileTypes = new TStringList;
FileList = new TFileList;
Log = ALog; // usedf ro accessing the TLogs ! (make sure its Synchronize though!!!)
GetFileTypes();
FileList->Copy(AFileList); // copy over so the other list can be free from thread
}
//---------------------------------------------------------------------------
void __fastcall RenameFilesThread::DoTerminate()
{
if(FOnFilesRenamed)
Synchronize(OnFilesRenamedEvent);
TThread::DoTerminate();
}
//---------------------------------------------------------------------------
__fastcall RenameFilesThread::~RenameFilesThread()
{
delete FileList;
delete FileTypes;
}
//---------------------------------------------------------------------------
// Main Execute()
//---------------------------------------------------------------------------
void __fastcall RenameFilesThread::Execute()
{
if(Terminated==false)
{
int fCount = FileList->Count;
int tCount = FileTypes->Count;
for(int i=0;i<tCount;i++) // loop throught the files types
{
for(FNum=0;FNum<fCount;FNum++) // loop through the files
{
MapName = StringReplace(FileList->Files[FNum]->FileName, " ", "", TReplaceFlags() << rfReplaceAll); // Remove white space from filenames, all of them
if(AnsiStartsText(FileTypes->Strings[i], MapName)) // this is case insencative ;) find FileType filter
{
if(MapName.Length()>FileTypes->Strings[i].Length()+2) // so we dont have and dstring errore for blank strings ;)
{ // incase we have, old -> ctf-chrome.ut2, new -> CTF-chrome.ut2
MapName = StringReplace(MapName, FileTypes->Strings[i], FileTypes->Strings[i], TReplaceFlags() << rfIgnoreCase); // only the very first 1 !!
if(MapName[FileTypes->Strings[i].Length()+1]!='-') // make sure we have, old CTFchrome.ut2, new CTF-chrome.ut2
MapName.Insert("-", FileTypes->Strings[i].Length()+1);
MapName[FileTypes->Strings[i].Length()+2] = UpCase(MapName[FileTypes->Strings[i].Length()+2]); // incase we have, old -> CTF-chrome.ut2, new -> CTF-Chrome.ut2
if(Rename())
{
Total++;
fCount--;
}
continue; // skips the checking below, no need to check
}
}
}
if(i==tCount) // only if not found in the filters then come in here and do renaming
{
MapName = FileTypes->Strings[i];
MapName[0] = UpCase(MapName[0]);
Rename();
}
}
if(FOnFilesRenamed)
Synchronize(OnFilesRenamedEvent);
}
}
//---------------------------------------------------------------------------
void __fastcall RenameFilesThread::RenameInLog()
{
if(Log->FileIsInLogs(FileList->Files[FNum]))
{
ShowMessage("Found it");
}
}
//---------------------------------------------------------------------------
bool __fastcall RenameFilesThread::Rename()
{
if(FileList->Files[FNum]->FileName.AnsiCompare(MapName)) // only rename if different! case sencative ;)
{
// Look in logs and change to new file name as needed
// this function is NOT Synchronized
Synchronize(RenameInLog);
if(RenameFile(FileList->Files[FNum]->FileLocation + FileList->Files[FNum]->FileName, FileList->Files[FNum]->FileLocation + MapName))
{
if(FOnFileRenamed)
Synchronize(OnFileRenamedEvent);
FileList->Delete(FNum); // remove from list, its been done already, no point in looping with it again
FNum--;
return true;
}
}
return false;
}
//---------------------------------------------------------------------------
// Events
//---------------------------------------------------------------------------
void __fastcall RenameFilesThread::OnFilesRenamedEvent()
{
FOnFilesRenamed(this);
}
//---------------------------------------------------------------------------
void __fastcall RenameFilesThread::OnFileRenamedEvent()
{
FOnFileRenamed(this, FileList->Files[FNum]->FileLocation + FileList->Files[FNum]->FileName, FileList->Files[FNum]->FileLocation + MapName);
}
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
void __fastcall RenameFilesThread::GetFileTypes()
{
TRegistry *Registry = new TRegistry;
Registry->RootKey = HKEY_LOCAL_MACHINE;
if(Registry->OpenKey("SOFTWARE\\Nasty PC's\\Map Manager\\DisplayMenus\\" + GameTag + "\\Maps", false))
Registry->GetValueNames(FileTypes);
Registry->CloseKey();
delete Registry;
}
//---------------------------------------------------------------------------