Thread: File renamming

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    File renamming

    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.
    Last edited by geek@02; 01-20-2005 at 10:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM