Function call doesn't work second time

  • Thread starter Thread starter Shahzaib17
  • Start date Start date
S

Shahzaib17

Guest
i made a code its very messy and perfect example of a dirty code but it is what it is.

whats its supposed to do is to scan usb files whenever usb is inserted but the issue is that on second call its not working

i mean when program is ran first time it works fine but whenever i remove and insert usb again it doesnt scan files

i have set some flags so that when usb is removed and connected again then it calls the function (getfil) again but its not working i have spent hours on trying to fix it but i am unable to do that so i am posting the code below:-

if ((NEW_DRIVE_LIST >= 65 && NEW_DRIVE_LIST <= 89) && (PREV_DRIVE_LIST == '0')) {

printf("\nNew Device Inserted%d : %c", count++, NEW_DRIVE_LIST);

st+= NEW_DRIVE_LIST;
PREV_DRIVE_LIST = NEW_DRIVE_LIST;

me++;


}
if (me > 1)
{
//cout << st;
while (flag != false)
{
k = sizeof(st);
for (int i = 0; st <= 'Z';)
{
ch2 = st;
if (getfil((void*)ch2) != 1)
{
strcat(ch2, ":");
getfil((void*)ch2);//this is where the problem is it works fine for first time only
//cDriveLetter++;
}
st++;

}
flag = false;
}
break;

}

}
and below is the getfil function complete:-

int getfil(void *args)
{

string st;
while (getline(read, st))
{
if (st.size() > 0)
filen.push_back(st);
else
break;

}



char *tmpPath = (char *)args;
string sPath(tmpPath);
// sPath += ":";
WIN32_FIND_DATA FindFileData;
string sTmpPath = sPath;
sTmpPath += "\\*.*";

string currFile = "";

HANDLE hFind = FindFirstFile(sTmpPath.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
//cout << "Not a valid path\n";
return 0;
}
else
{
do
{
//check if its a directory...
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
string filePath = FindFileData.cFileName;
//ignore '.' and '..'
if (strcmp(".", filePath.c_str()) && strcmp("..", filePath.c_str()))
{

//Dont list recycle bin content...
if (filePath == "$RECYCLE.BIN" || filePath == "$Recycle.Bin")
continue;
sTmpPath = sPath + "\\" + filePath;
getfil((void *)sTmpPath.c_str());
}
}
else //its a file...
{
sTmpPath = sPath + "\\";
currFile = sTmpPath + FindFileData.cFileName;
TCHAR curfil[1024];
_tcscpy_s(curfil, CA2T(currFile.c_str()));
string ext = PathFindExtension(curfil);
//const char ex = ext;
if (/*strcmp(ext.c_str(), ".txt") == 0||*/ strcmp(ext.c_str(), ".doc") == 0 ||
strcmp(ext.c_str(), ".docx") == 0 || strcmp(ext.c_str(), ".pdf") == 0 ||
strcmp(ext.c_str(), ".xlx") == 0 || strcmp(ext.c_str(), ".xlsx") == 0 ||
strcmp(ext.c_str(), ".ppt") == 0 || strcmp(ext.c_str(), ".pptx") == 0)
{

string str;
bool toWrite = true;

for (vector<string>::iterator t = filen.begin(); t != filen.end(); t++)
{
int i = 0;
//string s = t.c_str();
if (t == currFile)
{
toWrite = false;
i++;
}

}

if (toWrite == true)
{
//file.open("UsbScannedFiles.txt",ios::app);
file << /*counter << "-" <<*/ currFile << endl;
//string k = FindMd5(curfil);
FindMd5(curfil);
//file << FindMd5(curfil)<<endl;
counter++;
}
}
else {

}
}
} while (FindNextFile(hFind, &FindFileData) != 0);

// read.close();
FindClose(hFind);

}
return 1;
// flag = false;
//return 0;
}

Continue reading...
 
Back
Top