Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

can i do this in c++

Hi all,
I'm new enough to c++, put can program quiet well. I'm now looking to use c++ for a practical application.
I'm working on a windows platform and want to search a folder in the c-drive to find a file based on its date newest, oldest etc and move it to another folder.
I can't find any info on this in my book so was wondering if possible? if it is, is there a website i can check of anybody got an example of the code to use.

Thanks for your time,
David
[506 byte] By [whitehart] at [2007-11-11 10:08:04]
# 1 Re: can i do this in c++
ofcorse it's possible .. but not too easy , u want to know about how to access file creation date "get it from fat file how ?" ; also u need to know search algorithms to do that ... normal search file by file will take too many time .. actually what u r trying to do is what google had implemented it in its "google search software" .
I see u need a particule case but finally you need a good search algorithm , access files stored on harddisk and access their data info . I'm not trying to get u back but if u want an engine to do that so u can download a freeware software(s) .. but if u want to know how to do that u need first to continue reading our book , read about data-structure , files-database then u will have what u need .
Amahdy at 2007-11-11 20:59:22 >
# 2 Re: can i do this in c++
Sure you can do this fairly easily by calling FindFirstFile and then FindNextFile to traverse through the files in a directory. Then for each file use GetFileAttributes to get the info on the files. More on these API-s can be found on MSDN.
Ivan** at 2007-11-11 21:00:22 >
# 3 Re: can i do this in c++
I think those functions search only for a "file name" , you give it file name it gives you results ... not give it full drive c search for specified attributes .. so u need to know the whole drive c container , search in it file by file which will take lots of time OR using advanced search algorithm .
Amahdy at 2007-11-11 21:01:27 >
# 4 Re: can i do this in c++
if you cannot find an efficient way (iterating each file and getting its attributes is terrible) you can always call a system call to list the files into a text file and simply extract the info from that.

for example
system("dir /d /b /a-d >unlikelyname.asdf"); to list into unlikelyname.asdf the sorted by date, name only, no folders list of files such as you asked for. the top and bottom of the list are the newest / oldest files respectively.
jonnin at 2007-11-11 21:02:27 >
# 5 Re: can i do this in c++
great jonnin !!! using simple consol commands to read fat file , just it needs update to read subfolders .. "/t" for specified time date .
**white heart, if u just only want to search in a folder without subdirs in it , jonnin's method is perfectly doing that .
Amahdy at 2007-11-11 21:03:27 >
# 6 Re: can i do this in c++
Hi all,
I'm new enough to c++, put can program quiet well. I'm now looking to use c++ for a practical application.
I'm working on a windows platform and want to search a folder in the c-drive to find a file based on its date newest, oldest etc and move it to another folder.
I can't find any info on this in my book so was wondering if possible? if it is, is there a website i can check of anybody got an example of the code to use.

Thanks for your time,
David
Of course it's possible. There are several standard, quasi-standard and non-standard (i.e., proprietary) APIs for doing this. Two of the most standard ones are described here:
http://www.dev-archive.com/cplus/10MinuteSolution/26748
Danny at 2007-11-11 21:04:26 >
# 7 Re: can i do this in c++
From what I could figure out the user wanted to do a file search for a particular directory on drive c. And I've used the APIs mentioned in my previous post. They are standard APIs and they worked fine for me but you could always take a different route
Ivan** at 2007-11-11 21:05:24 >
# 8 Re: can i do this in c++
Well as reference for u whiteheart here is a list af all the APIs u will need to make this :

GetDriveType()
GetFileAttributes()
GetShortPathName()
GetExpandedName()
SystemTimeToFileTime()
FileTimeToSystemTime()
GetFileInformationByHandle()
CloseHandle()
OpenFile()
FindClose()
FindNextFile()
FindFirstFile()

There are optional functions in them and there are many other functions but I think u don't need them like check if it a network path or not [for "/" or "\"] and like get drive number , u allready want it in c only .
Amahdy at 2007-11-11 21:06:27 >
# 9 Re: can i do this in c++
if you want subfolders add a /s to it of course, and double check the output it might give the oldest per folder not total at -- might have to add date to the output and parse it -- blech.
jonnin at 2007-11-11 21:07:31 >
# 10 Re: can i do this in c++
Hi Again,
I welcome all your help but a lot of what you are saying is going over my head,
or maybe what i want to do is over my head :)
if you see this code which is in every tutorial on input\output with c++
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}

The line --myfile.open ("example.txt");-- writes to the local directory where ever the exe is, i'd like to read or write to say c:\example.txt when the exe is in c:\cplusplus\ or vice a versa, is there a simple way to this or does it require a bit of work?
whitehart at 2007-11-11 21:08:32 >
# 11 Re: can i do this in c++
yes:
ofstream myfile;
myfile.open ("c:\\mydir\\example.txt");
Danny at 2007-11-11 21:09:36 >