Hi!
I’m just trying to create a useless program, which monitors a folder to find any changes in it, and renaming it based on the number of files in the folder. Things seems to work fine until it comes to renaming the folder. I’ve used the function MoveFile() to do this, but it doesn’t work. Can anyone please check in and point out the hitch?
Code:
#include<windows.h>
#include<iostream.h>
#include<stdio.h>
#include<fstream>
char dir[256];
char ext[256];
char path[256];
void updateFolder(){
lstrcpy(path, dir);
lstrcat(path, ext);
bool bfinished=false;
HANDLE hSearch;
WIN32_FIND_DATA fdat;
int nfile=0;
//start file find
hSearch = FindFirstFile(path,&fdat);
if(hSearch==INVALID_HANDLE_VALUE)
cout<<"No files of that type found."<<endl;
else{
nfile++;
cout<<fdat.cFileName<<endl;
}
while(!bfinished){
if(!FindNextFile(hSearch, &fdat)){
if(GetLastError()==ERROR_NO_MORE_FILES){
cout<<"Search complete."<<endl;
cout<<nfile<<" files found."<<endl;
}
bfinished=true;
}else{
nfile++;
cout<<fdat.cFileName<<endl;
}
}
int ePos=lstrlen(dir);
char ddir[256];
lstrcpy(ddir,dir);
ePos--;
ddir[ePos]='\0';//clear out last '\' char
char nDir[256];
char catStr[256];
//create a (strange)new folder name based on the number of files found
lstrcpy(catStr,"a");
for(;nfile>0;nfile--)
lstrcat(catStr,"a");
lstrcpy(nDir, ddir);
lstrcat(nDir, catStr);
if(!MoveFile(ddir, nDir))
cout<<"Error renamming"<<endl;
//if(rename(ddir, nDir))
//cout<<"Error renamming"<<endl;
}
void main(){
cout<<"Directory path: ";
cin.getline(dir,256);
cout<<"Extention: ";
cin.getline(ext,256);
updateFolder();
DWORD dwWaitStatus;
HANDLE dwChangeHandle;
dwChangeHandle=FindFirstChangeNotification(dir,TRUE,FILE_NOTIFY_CHANGE_SIZE);
if(dwChangeHandle==INVALID_HANDLE_VALUE)
cout<<GetLastError()<<endl;
int x=1;
while(TRUE){
dwWaitStatus=WaitForSingleObject(dwChangeHandle,NULL);
if(dwWaitStatus==WAIT_OBJECT_0){//signaled
//MessageBeep(0);
system("cls");
cout<<"Changes: "<<x++<<endl;
updateFolder();//update folder name when something changes
//break;
FindNextChangeNotification(dwChangeHandle);
}
}
getchar();
}
Thanks.