Create Copies of Files

This is a discussion on Create Copies of Files within the C++ Programming forums, part of the General Programming Boards category; I'm creating a utility program to create empty text files using the filenames of PDFs, but with the case--upper and ...

  1. #1
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27

    Create Copies of Files

    I'm creating a utility program to create empty text files using the filenames of PDFs, but with the case--upper and lower--preserved. I need duplicate files with zero footprint on the HD for indexing purposes. Can anybody help me with this? I managed to write something in another language, but it came out all in lowercase so I'm looking into C/C++.

    Example
    ----------------------------
    Input: 0763704814 - C++ Plus Data Structures, 3ed (Jones and Bartlett-2003).pdf
    Output: 0763704814 - C++ Plus Data Structures, 3ed (Jones and Bartlett-2003).pdf.txt
    ---------------------------

    And I also want to manipulate the file names and move the ISBN to the end.
    ----------------------------
    Input: 0763704814 - C++ Plus Data Structures, 3ed (Jones and Bartlett-2003).pdf
    Output: C++ Plus Data Structures, 3ed (Jones and Bartlett-2003) - 0763704814.pdf.txt
    ---------------------------

    Why do I want this? I download a lot of ebooks--PDF's, CHM's, DJVU's, etc. I want to create a separate index using the operating system's folders as a kind of catalog and store the ebooks elsewhere. My solution is to create proxy files for the ebooks (i.e., empty text files with the same names as the original). The text files now become
    like catalog cards in the library. One "catalog card" (text file) will have the ISBN in front and another has the book name in front.

    I need to do the creation of the files in bulk because I download like hundreds of ebooks per day. So, the cataloging part is really slowing me down.

    BTW, I'm using Bloodshed Dev-C++ compiler.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    How can we help?

  3. #3
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    What C++ file I/O function do I use? I need something to get all info about the files in a particular folder (say "*.pdf" in the current folder) into some array or collection.

    And then, I need the function to create text files from the filenames I collected.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Read the FAQ.

    When you have a good filepath, open a new txt file with the name you want and close it.

    #include <iostream>
    using std:: ofstream;

    ofstream creator;
    creator.open( filepath );
    creator.close( );

    That's all you really have to do to make an empty file.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,406
    O_o

    I'm not one to discourage the use of C++, but this sounds like a job for a SQL database.
    (Certainly, you may feel free to use an embedded database with C++.)

    You'll have to use something external to C++ to get a directory listing.
    (The Boost libraries have some really nice iterators for this purpose.)

    Just to satisfy my curiosity, could you elaborate on what you intend?

    And there really is no such thing as "zero footprint" file.

    Soma

  6. #6
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    whiteflags: I think the FAQ link has the necessary codes I need to create the utility program. Thanks.

    phantomotap: Actually, I already wrote a Visual Foxpro program to do this. It just have that annoying flaw--all filenames are in lower case.

    As for my intentions, like I said, I download a lot of ebooks. I want a simple catalog that I can check to see if I already have a copy of the book. Previously, I use a text file with all the filenames in my library. I use Notepad++ to do a text search, but over time, the contents do not reorder/rearrange itself after adding more to the list. Sometimes, I want to do a keyword search and the files are far apart.

    Now, I'm relying on Windows file system to solve the catalog problem. I just need a proxy file with negligible size with the list books--one has the ISBN in front and the other with the book title in front.

  7. #7
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Excel and other spreadsheet editors offer a Sort feature. You should be able to populate the fields by reading the content of the folder etc.

  8. #8
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    I already tried using Excel last year to catalog hundreds of chess ebooks I downloaded. It was too much work and the problem of updating the list is a problem.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds like you need a proper database - although "hundreds" doesn't sound too bad, in my opinion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Updating the list?

    Open command, cd to the folder,
    Code:
    dir /B > c:\list.txt
    And open the file in Excel.

    From there on it's just some string manipulation functions to extract the ISBN from the title.

  11. #11
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Chess is just one category of ebooks I download. I already went past the 2,000 mark for the others months ago. All in all, I probably have 30GB of ebooks already.

    Anyway, adding entries to some database or spreadsheet do add up, but that is not the crux of the problem for me. In all my previous solutions, I cannot be sure if I have already processed or cataloged an ebook. The act of cataloging or recording is totally detached from the act of handling the actual ebook.

    With my system of using proxy files, I know for sure I handled/cataloged an ebook because I used it as an actual input to create the catalog entry. With a utility program to create a proxy file or virtual "catalog card", the process is instantaneous.

  12. #12
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    I know about dir > list.txt. That's how I populated my Excel file before. The problem comes in when you want to insert newer entries. It's not a straightforward process.

    Anyway, the spreadsheet actually gave me the idea about using the Windows file system as the database or catalog. If you will look at the Explorer window using the "detailed" view, you will notice that the filename, date, size, etc. are automatically segregated just like a spreadsheet.

    Then, it hit me, when I use the output of dir > list.txt, I'm actually getting the details about the files from the file system as strings and then proceed to break them up again inside the spreadsheet into their parts.

  13. #13
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    I did it. Thanks a million guys. By the way, it's not a C++ program. I had to be less ambitious regarding my programming abilities. Anyway, now that I have a working program, maybe I can get help converting it into a C++ program.

    Code:
    /*---------------------------------------------------------
    Program     : eBook Catalog Maker
    Author        : Hans alias Kanshu / Hanabi
    Description : Creates proxy / empty files for ebooks of type PDF, CHM, DJVU, RAR, ZIP
    Date           : 05/09/2009 2102H 
    Notes         : Solutions provided by programmers from comp.lang.c and 
                           cprogramming.com                
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>
    
    FILE *thisCard;  // catalog card; proxy file
    
    //---------------------------------------------------------
      int main( int argc , char *argv[] ) {
    //-------------------------------------------------------  
      struct dirent *thisFile;
      
      char CatalogCard[768];
      char FileType[16];
      char Title[512];
      char ISBN[16];
      
      char TypePDF[]  = "pdf";
      char TypeCHM[]  = "chm";
      char TypeDJVU[] = "djvu";
      char TypeZIP[]  = "zip";
      char TypeRAR[]  = "rar";
    
      int Matched;
      int i;
    
      DIR *thisFolder;
      thisFolder = opendir( "." );
    
      if ( thisFolder ) {
           
        while ( ( thisFile = readdir( thisFolder ) ) != NULL ) {
    
          // typical filename + extension
          Matched = sscanf( thisFile->d_name , "%511[^.].%15s" , Title , FileType );
    
          if ( Matched == 2 ) {
            
            // recognize only PDF, CHM, DJVU, ZIP, RAR
            for( i = 0 ; ( FileType[i] = tolower( FileType[i] ) ) ; i++ );
            
            if ( ( strstr( FileType , TypePDF )  != NULL ) || 
                 ( strstr( FileType , TypeCHM )  != NULL ) || 
                 ( strstr( FileType , TypeDJVU ) != NULL ) ||
                 ( strstr( FileType , TypeZIP )  != NULL ) ||
                 ( strstr( FileType , TypeRAR )  != NULL ) ) {
               
              // create 1st catalog card; ISBN card
              //----------------------------------------------------------------- 
              if ( strstr( FileType , TypePDF ) != NULL )
                snprintf( CatalogCard , sizeof CatalogCard ,
                          "%s.%s.txt" , Title , FileType );
    
              // NOTE: MQH filetype is the MetaQuotes Language file
              //   that has a yellow icon in the file explorer
              if ( ( strstr( FileType , TypeCHM )  != NULL ) ||
                   ( strstr( FileType , TypeDJVU ) != NULL ) )
                snprintf( CatalogCard , sizeof CatalogCard ,
                          "%s.%s.mqh" , Title , FileType );
    
              if ( ( strstr( FileType , TypeRAR ) != NULL ) ||
                   ( strstr( FileType , TypeZIP ) != NULL ) )
                snprintf( CatalogCard , sizeof CatalogCard ,
                          "%s.%s.zip" , Title , FileType );
    
              if ( ( thisCard = fopen( CatalogCard , "w+" ) ) == NULL )
                printf( "Can't create file.\n" );
              else
                fclose( thisCard );
    
              // create 2nd catalog card; Title + ISBN card
              //----------------------------------------------------------------- 
              Matched = sscanf( thisFile->d_name , "%15s - %511[^.].%15s" ,
                                                   ISBN , Title , FileType );
    
              if ( Matched == 3 ) {
    
                for( i = 0 ; ( FileType[i] = tolower( FileType[i] ) ) ; i++ );
                
                if ( strstr( FileType , TypePDF ) != NULL )
                  snprintf( CatalogCard , sizeof CatalogCard ,
                            "%s - %s.%s.txt" , Title , ISBN , FileType );
                          
                if ( ( strstr( FileType , TypeCHM )  != NULL ) ||
                     ( strstr( FileType , TypeDJVU ) != NULL ) )
                  snprintf( CatalogCard , sizeof CatalogCard ,
                            "%s - %s.%s.mqh" , Title , ISBN , FileType );
    
                if ( ( strstr( FileType , TypeRAR ) != NULL ) ||
                     ( strstr( FileType , TypeZIP ) != NULL ) )
                  snprintf( CatalogCard , sizeof CatalogCard ,
                            "%s - %s.%s.zip" , Title , ISBN , FileType );
    
                if ( ( thisCard = fopen( CatalogCard , "w+" ) ) == NULL )
                  printf( "Can't create file.\n" );
                else
                  fclose( thisCard );
    
              }
            }
          }
        }
    
        closedir( thisFolder );
      }
      
      system( "PAUSE" );
      return 0;
    }

  14. #14
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Some refinement needed. It's not skipping some text files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-15-2007, 03:25 PM
  2. 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, 09:12 PM
  3. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM
  4. How to use multiple files?
    By fry in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2002, 09:07 PM
  5. Create exe files
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-16-2001, 09:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21