move files to folders
Below is my requirement and i am not clear how to approach this issue in windows programming.
I have a folder with 2500 files. The files are in below format.
and i need a program which will read each file from this folder and read the first 4 bytes and move the file to the folder (if it does not exist create a new one) with the name of the file.
read 1234_name1.txt , extract 1234 and create a folder 1234. Move the file to the folder 1234.
read 1234_name123.pdf, extract 1234 and now the folder exist, now simply move the file to the folder 1234.
Your views/suggestions are highly appreciated.
Thanks for your help in advance.
Well as far as extracting the digits, you could probably use something like:
The rest would just be a matter of using the CreateDirectory/CreateDirectoryEx and MoveFile/MoveFileEx API's (and SetCurrentDirectory, if the process is recursive).
char* extract_leading_digits( char* dst, const char* src )
char* ptr = dst;
if( !isdigit( *src ) )
while( isdigit( *src ) )
*ptr++ = *src++;
*ptr = 0;
Those are all C solutions ;)
The C++ solutions are much more elegant, which is why I ask!
I am fine with C.
But my only concern is that the files are in windows machine and i am not sure how to execute the program in C.
I doubt whether i will be having the required libaries to execute the script.
How much do you know about programming in C then?
You basically need to find the files, get their filenames, extract the first 4 chars, create the directory, move the files.
Sebastiani already told you how to create directories and move files.
Do you know of ways to get the files, their filenames and extract part of the name?
I am pretty much new to C.
I do not have experience in writing windows programming in C/C++.
>> The C++ solutions are much more elegant, which is why I ask!
Snob. Example, please? :p
>> I am pretty much new to C. I do not have experience in writing windows programming in C/C++.
Well then you may run into some problems, I'd imagine. What languages are you proficient at? Perhaps you could use one that is more familiar to you?
I am proficient in COBOL, JCL.
Originally Posted by Sebastiani
Not tested for bugs and stuff, of course.
namespace fs = boost::filesystem;
fs::directory_iterator it(Path), end_it;
for (; it != end_it; it++)
if (Status->is_directory()); // Is directory, do something
std::string ExtractedFilename(Filename.begin(), Filename.begin() + 4);
fs::rename_file(Filename, ExtractedFilename + "\\" + Filename);
>> I am proficient in COBOL, JCL.
Geez, man. You do know that those languages went out of style about the same time as 8-track tapes, don't you? =D
Well, your best bet would probably be VisualBasic or similar, then. Fairly short learning curve and whatnot. Otherwise you might consider hiring a programmer (RentaCoder, perhaps?).
In case you want to do something like this is in C/C++, you would have better start learning the language properly first...
Originally Posted by diva_thilak