Thread: move files to folders

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    move files to folders

    Hi Friends,
    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.
    1234_name1.txt
    1234_name123.pdf
    4567_name1.txt
    4567_name123.pdf

    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.

    Example:
    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.

    Regards,
    Diwakar

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well as far as extracting the digits, you could probably use something like:

    Code:
    char* extract_leading_digits( char* dst, const char* src )
    {
    	char* ptr = dst;
    	if( !isdigit( *src ) )
    		return 0;
    	while( isdigit( *src ) )
    		*ptr++ = *src++;
    	*ptr = 0;
    	return dst;
    }
    The rest would just be a matter of using the CreateDirectory/CreateDirectoryEx and MoveFile/MoveFileEx API's (and SetCurrentDirectory, if the process is recursive).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Those are all C solutions
    The C++ solutions are much more elegant, which is why I ask!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    I am pretty much new to C.
    I do not have experience in writing windows programming in C/C++.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> The C++ solutions are much more elegant, which is why I ask!

    Snob. Example, please?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    I am proficient in COBOL, JCL.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Sebastiani View Post
    >> The C++ solutions are much more elegant, which is why I ask!

    Snob. Example, please?
    Like...
    Code:
    namespace fs = boost::filesystem;
    fs::path Path("C:\\");
    fs::directory_iterator it(Path), end_it;
    for (; it != end_it; it++)
    {
        fs::file_status Status(*it);
        if (Status->is_directory()); // Is directory, do something
        
        std::string Filename(it->filename());
        std::string ExtractedFilename(Filename.begin(), Filename.begin() + 4);
        fs::create_directory(ExtractedFilename);
        fs::rename_file(Filename, ExtractedFilename + "\\" + Filename);
    }
    Not tested for bugs and stuff, of course.
    Last edited by Elysia; 07-22-2009 at 04:02 PM. Reason: Whoops... fixed bug.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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?).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Like...

    Show off.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by diva_thilak View Post
    I am proficient in COBOL, JCL.
    In case you want to do something like this is in C/C++, you would have better start learning the language properly first...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  2. Problems Moving Certain Folders...
    By notsocks in forum C Programming
    Replies: 8
    Last Post: 12-16-2007, 07:25 AM
  3. Difference between files and folders?
    By fanoliv in forum C Programming
    Replies: 4
    Last Post: 06-20-2006, 09:15 AM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM