Thread: can c++ do batch file processing?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    can c++ do batch file processing?

    I have a directory, which contains 100,000 input text files. I need to read in each file, to do some computation.

    The lousy way I thought of is create a name file which contains all the 100,000 input files name, i.e. each line contains the input file name. My program will use the name file and read in each line of the name file, put into a string, then use it to open the input text file. But this is tedious because I need to manually key in the input files names in the name file.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Ken, you are the man!!! Thanks alot! I go try it out

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I tried this code below from FAQ > How do I... (Level 3) > Accessing a directory and all the files within it. But I encountered error when I'm using Microsoft Visual Studio 2005. Does any one knows the problem? It seems like .net does not support windows.h?

    Code:
    // xtree.cpp : Defines the entry point for the console application.
    //
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    
    void errormessage(void)
    {
      LPVOID  lpMsgBuf;
      FormatMessage
      (
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),  // Default language
        (LPTSTR) & lpMsgBuf,
        0,
        NULL
      );
      fprintf(stderr, "%s\n", (char*)lpMsgBuf);
      LocalFree(lpMsgBuf);
    }
    
    void format_time(FILETIME *t, char *buff)
    {
      FILETIME    localtime;
      SYSTEMTIME  sysloc_time;
      FileTimeToLocalFileTime(t, &localtime);
      FileTimeToSystemTime(&localtime, &sysloc_time);
      sprintf
      (
        buff,
        "%4d/%02d/%02d %02d:%02d:%02d",
        sysloc_time.wYear,
        sysloc_time.wMonth,
        sysloc_time.wDay,
        sysloc_time.wHour,
        sysloc_time.wMinute,
        sysloc_time.wSecond
      );
    }
    
    void format_attr(DWORD attr, char *buff)
    {
      *buff++ = attr & FILE_ATTRIBUTE_ARCHIVE ? 'A' : '-';
      *buff++ = attr & FILE_ATTRIBUTE_SYSTEM ? 'S' : '-';
      *buff++ = attr & FILE_ATTRIBUTE_HIDDEN ? 'H' : '-';
      *buff++ = attr & FILE_ATTRIBUTE_READONLY ? 'R' : '-';
      *buff++ = attr & FILE_ATTRIBUTE_DIRECTORY ? 'D' : '-';
      *buff++ = attr & FILE_ATTRIBUTE_ENCRYPTED ? 'E' : '-';
      *buff++ = attr & FILE_ATTRIBUTE_COMPRESSED ? 'C' : '-';
      *buff = '\0';
    }
    
    struct flist
    {
      int             num_entries;
      int             max_entries;
      WIN32_FIND_DATA *files;
    };
    void addfile(flist *list, WIN32_FIND_DATA data)
    {
      if (list->num_entries == list->max_entries)
      {
        int             newsize = list->max_entries == 0 ? 16 : list->max_entries * 2;
        WIN32_FIND_DATA *temp = (WIN32_FIND_DATA *) realloc(list->files, newsize * sizeof(WIN32_FIND_DATA));
        if (temp == NULL)
        {
          fprintf(stderr, "Out of memory\n");
          exit(1);
        }
        else
        {
          list->max_entries = newsize;
          list->files = temp;
        }
      }
    
      list->files[list->num_entries++] = data;
    }
    
    int sortfiles(const void *a, const void *b)
    {
      const WIN32_FIND_DATA *pa = (WIN32_FIND_DATA *) a;
      const WIN32_FIND_DATA *pb = (WIN32_FIND_DATA *) b;
      int                   a_is_dir = pa->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
      int                   b_is_dir = pb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
      if (a_is_dir ^ b_is_dir)
      {
        // one of each, prefer the directories first
        if (a_is_dir) return(-1);
        if (b_is_dir) return(+1);
        return(0);
      }
      else
      {
        // both files, or both directories - return the strcmp result
        return(strcmp(pa->cFileName, pb->cFileName));
      }
    }
    
    void doit(char *root)
    {
      flist           list = { 0, 0, NULL };
      HANDLE          h;
      WIN32_FIND_DATA info;
      int             i;
    
      // build a list of files
      h = FindFirstFile("*.*", &info);
      if (h != INVALID_HANDLE_VALUE)
      {
        do
        {
          if (!(strcmp(info.cFileName, ".") == 0 || strcmp(info.cFileName, "..") == 0))
          {
            addfile(&list, info);
          }
        } while (FindNextFile(h, &info));
        if (GetLastError() != ERROR_NO_MORE_FILES) errormessage();
        FindClose(h);
      }
      else
      {
        errormessage();
      }
    
      // sort them
      qsort(list.files, list.num_entries, sizeof(list.files[0]), sortfiles);
    
      // print out in sorted order
      int numdirs = 0;
      for (i = 0; i < list.num_entries; i++)
      {
        char  t1[50], t2[50], t3[50], a[10];
        format_time(&list.files[i].ftCreationTime, t1);
        format_time(&list.files[i].ftLastAccessTime, t2);
        format_time(&list.files[i].ftLastWriteTime, t3);
        format_attr(list.files[i].dwFileAttributes, a);
        if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
          // 'null' date for directory access times, which change each time
          // we run this tool
          sprintf(t2, "%4d/%02d/%02d %02d:%02d:%02d", 2000, 1, 1, 0, 0, 0);
        }
    
        printf("%s %10ld %s %s %s %s\\%s\n", a, list.files[i].nFileSizeLow, t1, t2, t3, root, list.files[i].cFileName);
        if (list.files[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) numdirs++;
      }
    
      // now process all the sub-dirs
      // free all the files first, to save a bit of space
      // the sort function will have put them all at the end.
      list.files = (WIN32_FIND_DATA *) realloc(list.files, numdirs * sizeof(WIN32_FIND_DATA));
      for (i = 0; i < numdirs; i++)
      {
        char  newroot[MAX_PATH];
        sprintf(newroot, "%s\\%s", root, list.files[i].cFileName);
        SetCurrentDirectory(list.files[i].cFileName);
        doit(newroot);
        SetCurrentDirectory("..");
      }
    
      // free the remainder
      free(list.files);
    }
    
    void banner(void)
    {
      //       12345678901234567890
      printf("Attribs ");
      printf("      Size ");
      printf("       CreationTime ");
      printf("     LastAccessTime ");
      printf("      LastWriteTime ");
      printf("Filename\n");
    }
    
    int main(int argc, char *argv[])
    {
      if (argc > 1)
      {
        char  olddir[MAX_PATH];
        if (GetCurrentDirectory(MAX_PATH, olddir) == 0)
        {
          errormessage();
          exit(1);
        }
    
        if (!SetCurrentDirectory(argv[1]))
        {
          errormessage();
          exit(1);
        }
    
        banner();
        doit(".");
        SetCurrentDirectory(olddir);
      }
      else
      {
        banner();
        doit(".");
      }
    
      return(0);
    }

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If you are using MSVC++2005 you will need the platform SDK included for the compiler to read windows.h. That may be your problem. You can download it free from Microsoft's website

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    so I just download SDK from http://www.microsoft.com/downloads/d...displaylang=en

    install it, then i'm able to use windows.h under msvc++2005?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    If you're using msvc-express edition then you need to install and configure the ide to use the psdk. If you're using ms-visual studio then you should be good to go but you should install the psdk as an 'update' anyway.

    If you are getting specific error messages when you try to compile your code you should post those errors, or at least a representative subsample of them if you get many similar ones.

    edit:

    I supect the errors are unicode related; it will be simpler for that particular example to disable unicode. To do this: project menu -->'your_project' properties, configuration properties, set Character set to use multi-byte character set.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    thanks guys! i go try it out! big file size, got to wait for it to d/l =)

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Please note my edit above.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But I encountered error when I'm using Microsoft Visual Studio 2005.
    Here's a tip - say what error you got!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    Thumbs up

    Quote Originally Posted by Ken Fitlike

    I supect the errors are unicode related; it will be simpler for that particular example to disable unicode. To do this: project menu -->'your_project' properties, configuration properties, set Character set to use multi-byte character set.
    How do you know that? I only said I got errors and You have solved my problem! What is unicode and multi-byte? You are great, I salute you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM