Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Counting files

hype8912hype8912 Member Posts: 4
I am pretty new to C programming. I've taken a couple college classes. I have been tasked by work to develop a program that first move files from folder 1 to folder 2, then copies files from folder 2 to a folder on the server. I have done this by using plain dos commands to do this. I've even added a sleep command to delay the next command. Pretty Easy.

Now I was asked to add error prevention to this by counting the number of *.cgm files and *.xml files in a specified folder to a variable. I am able to do this through the following dos command: dir /a "I:" |find /c /v "" , but I'm not able to get the counted number to a variable.

The second part is to parse an outputed text file and then move the files based to a specified folder based on the text file. I haven't even decided to task this part yet. One step at a time.

Comments

  • Griz803Griz803 Member Posts: 100
    Well, assuming DOS or Windows console environment, first look up the functions findfirst and findnext from the dos.h libraries, usually I think. Then you can iterate through the files in a direcotry and increment a couple of variables every time you encounter a filename with *.cgm or *.xml. That should give you totals, then do the same with the directory you transferred into and compare the counters. Throw up an all good message or have it look for any files left behind or an error message to finish, and you should be all good. Lots of luck, hope this helps you get started.
  • hype8912hype8912 Member Posts: 4
    : Well, assuming DOS or Windows console environment, first look up the functions findfirst and findnext from the dos.h libraries, usually I think. Then you can iterate through the files in a direcotry and increment a couple of variables every time you encounter a filename with *.cgm or *.xml. That should give you totals, then do the same with the directory you transferred into and compare the counters. Throw up an all good message or have it look for any files left behind or an error message to finish, and you should be all good. Lots of luck, hope this helps you get started.
    :

    Thanks for the help. I used the findfirst and findnext, but how do you specify which folder you want it to search in. The program and the files are in different folders on the same network drive.
  • Griz803Griz803 Member Posts: 100
    : Thanks for the help. I used the findfirst and findnext, but how do you specify which folder you want it to search in. The program and the files are in different folders on the same network drive.
    :
    Well, that can be accomplished depending on DOS or Windows, by using the chdir function if the directory names are known and constant. You could also feed them in from a log file or use a wild card argument assuming that you have appropriate library routines in place to support that. You really should add a bit more information to your posts, like OS, compiler and any available libraries that you have available that you think could affect these things. For example, I'm absolutely sure that chdir exists in dos.h distributed with Borland C/C++ version 4.5.OLD but have not a clue without looking if it's included in MinGW version 2.3.X. So, help us help you better next time, Ok? Hope this helps too.
  • hype8912hype8912 Member Posts: 4
    [b][red]This message was edited by hype8912 at 2005-9-5 2:7:0[/red][/b][hr]
    ......

    I usually use Notepad to write in, but use Microsoft Visual C++ or LCC-win32 to compile my programs. I'm writing to be used on a Windows XP machine. I normally use MVC++, because anything I write at home, I have to submit my source code to be evaluated be for compiling. It has to be checked for malicious code. We only have MS Visual Studio at work.

    I will try the chdir. The main problem I had with the previous program was that it would only count the files in the folder that the .exe file was sitting in.

    Update....
    I finally got the chdir working. I was passing it as a dos system command instead of a internal dos command. I post a sample of my code so far. I know its messy, but I'm slowly remembering everything.

    #include
    #include
    #include
    #include

      #include

      #define GRAPHICS_HERE "C:\grap\" /*Counts in specified directory 8.3 format*/
      #define PARSE_IMPORT "C:\import\"

      int cgm=0, xml=0;

      int main(void){
      struct _finddata_t finddata;
      long res;

      chdir(GRAPHICS_HERE); /*Points to specified directory*/

      if ((res = _findfirst("*.cgm", &finddata))==-1)
      printf("NO CGM files found
      ");

      do{
      cgm++;
      }while(!_findnext(res, &finddata));

      printf("%d
      ", cgm);

      if ((res = _findfirst("*.xml", &finddata))==-1)
      printf("NO XML files found
      ");

      do{
      xml++;
      }while(!_findnext(res, &finddata));

      printf("%d
      ", xml);

      if((cgm == 0)&&(xml == 0))
      printf("No CGM or XML files found.
      ");
      else
      if(cgm == xml)
      {system("MOVE C:\*.cgm C:\docume~1\joshua~1\mydocu~1\");
      system("sleep.com 05");

      system("MOVE C:\*.xml C:\docume~1\joshua~1\mydocu~1\");
      system("sleep.com 05");}
      else
      if(cgm < xml)
      {MessageBox (NULL, "Missing CGM file.
      Check graphics go here folder for missing CGM file." , "Hello1", 0);
      return -1;}
      else
      {MessageBox (NULL, "Missing XML file
      Check graphics go here folder for missing XML file." , "Hello2", 0);
      return -1;}


      chdir(PARSE_IMPORT);
      cgm = 0;
      xml = 0;

      if ((res = _findfirst("*.cgm", &finddata))==-1)
      printf("NO CGM files found
      ");

      do{
      cgm++;
      }while(!_findnext(res, &finddata));

      printf("%d
      ", cgm);

      if ((res = _findfirst("*.xml", &finddata))==-1)
      printf("NO XML files found
      ");

      do{
      xml++;
      }while(!_findnext(res, &finddata));

      printf("%d
      ", xml);

      if((cgm == 0)&&(xml == 0))
      printf("No CGM or XML files found.
      ");
      else
      if(cgm == xml)
      {system("net use H: \\server\MUSIC"); /*Maps server address to drive H: */

      system("COPY C:\*.cgm H:\docume~1\joshua~1\mydocu~1\");
      system("sleep.com 05");

      system("COPY C:\*.xml H:\docume~1\joshua~1\mydocu~1\");
      system("sleep.com 05");

      system("net use H: /delete");} /*Deletes the use of drive H: */
      else
      if(cgm < xml)
      {printf("Missing CGM file.
      ");
      return -1;}
      else
      {printf("Missing XML file.
      ");
      return -1;}

      return 0;
      }


  • Griz803Griz803 Member Posts: 100
    Ok, I'm glad you got it working. Two points I'd like to make for future reference though. First, working under XP to compile, but what is the systems of deployment? I don't mean to be critical or nosy, but you might want to consider using a console ap with the Win32 API calls for any Windows 32 bit development. They are a pain in the tookus to learn and lookup until you do, but they could benefit your program's stability in the long run. Second, your code isn't very portable with all those system calls in it. That will make no difference if the executable will be distributed after it is okayed on only the operating system it is compiled to, but will lead to a mess should your company try and distribute the executable and/or source to other platforms. Just a reminder, glad the info helped.
  • stoberstober Member Posts: 9,765 ✭✭✭
    A few suggested changes in [red]RED[/red]
    [code]:
    : #include
    : #include
    [red]^^^ remove the above line. MSVC doesn't support that file, and will
    not compile any program that includes it.[/red]
    : #include
    : #include

      [red]^^^ this file isn't supported either. It's a *nix file[/red]
      : #include
      :
      : #define GRAPHICS_HERE "C:\grap\" /*Counts in specified directory 8.3 format*/
      : #define PARSE_IMPORT "C:\import\"
      :
      : int cgm=0, xml=0;
      :
      : int main(void){
      : struct _finddata_t finddata;
      : long res;
      :
      : chdir(GRAPHICS_HERE); /*Points to specified directory*/
      :
      : if ((res = _findfirst("*.cgm", &finddata))==-1)
      : printf("NO CGM files found
      ");
      :
      : do{
      : cgm++;
      : }while(!_findnext(res, &finddata));
      : [red] _findclose(res);[/red]
      : printf("%d
      ", cgm);
      :
      : if ((res = _findfirst("*.xml", &finddata))==-1)
      : printf("NO XML files found
      ");
      :
      : do{
      : xml++;
      : }while(!_findnext(res, &finddata));
      : [red] _findclose(res);[/red]
      :
      : printf("%d
      ", xml);
      :
      : if((cgm == 0)&&(xml == 0))
      : printf("No CGM or XML files found.
      ");
      : else
      : if(cgm == xml)
      : {system("MOVE C:\*.cgm C:\docume~1\joshua~1\mydocu~1\");
      : system("sleep.com 05");
      :
      : system("MOVE C:\*.xml C:\docume~1\joshua~1\mydocu~1\");
      : system("sleep.com 05");}
      : else
      : if(cgm < xml)
      : {MessageBox (NULL, "Missing CGM file.
      Check graphics go here folder for missing CGM file." , "Hello1", 0);
      : return -1;}
      : else
      : {MessageBox (NULL, "Missing XML file
      Check graphics go here folder for missing XML file." , "Hello2", 0);
      : return -1;}
      :
      :
      : chdir(PARSE_IMPORT);
      : cgm = 0;
      : xml = 0;
      :
      : if ((res = _findfirst("*.cgm", &finddata))==-1)
      : printf("NO CGM files found
      ");
      :
      : do{
      : cgm++;
      : }while(!_findnext(res, &finddata));
      : [red] _findclose(res);[/red]
      :
      : printf("%d
      ", cgm);
      :
      : if ((res = _findfirst("*.xml", &finddata))==-1)
      : printf("NO XML files found
      ");
      :
      : do{
      : xml++;
      : }while(!_findnext(res, &finddata));
      : [red] _findclose(res);[/red]
      :
      : printf("%d
      ", xml);
      :
      : if((cgm == 0)&&(xml == 0))
      : printf("No CGM or XML files found.
      ");
      : else
      : if(cgm == xml)
      : {system("net use H: \\server\MUSIC"); /*Maps server address to drive H: */
      :
      : system("COPY C:\*.cgm H:\docume~1\joshua~1\mydocu~1\");
      : system("sleep.com 05");
      :
      : system("COPY C:\*.xml H:\docume~1\joshua~1\mydocu~1\");
      : system("sleep.com 05");
      :
      : system("net use H: /delete");} /*Deletes the use of drive H: */
      : else
      : if(cgm < xml)
      : {printf("Missing CGM file.
      ");
      : return -1;}
      : else
      : {printf("Missing XML file.
      ");
      : return -1;}
      :
      : return 0;
      : }
      :
      [/code]:
      :

  • hype8912hype8912 Member Posts: 4
    : A few suggested changes in [red]RED[/red]
    : [code]:
    : : #include
    : : #include
    : [red]^^^ remove the above line. MSVC doesn't support that file, and will
    : not compile any program that includes it.[/red]
    : : #include
    : : #include

      : [red]^^^ this file isn't supported either. It's a *nix file[/red]
      : : #include
      : :
      : : #define GRAPHICS_HERE "C:\grap\" /*Counts in specified directory 8.3 format*/
      : : #define PARSE_IMPORT "C:\import\"
      : :
      : : int cgm=0, xml=0;
      : :
      : : int main(void){
      : : struct _finddata_t finddata;
      : : long res;
      : :
      : : chdir(GRAPHICS_HERE); /*Points to specified directory*/
      : :
      : : if ((res = _findfirst("*.cgm", &finddata))==-1)
      : : printf("NO CGM files found
      ");
      : : [blue]else{[/blue]
      : : do{
      : : cgm++;
      : : }while(!_findnext(res, &finddata));
      : : [red] _findclose(res);[/red]
      : : printf("%d
      ", cgm);
      : : [blue]}[/blue]
      : : if ((res = _findfirst("*.xml", &finddata))==-1)
      : : printf("NO XML files found
      ");
      : : [blue]else{[/blue]
      : : do{
      : : xml++;
      : : }while(!_findnext(res, &finddata));
      : : [red] _findclose(res);[/red]
      : : [blue]}[/blue]
      : : printf("%d
      ", xml);
      : :
      : : if((cgm == 0)&&(xml == 0))
      : : printf("No CGM or XML files found.
      ");
      : : else
      : : if(cgm == xml)
      : : {system("MOVE C:\*.cgm C:\docume~1\joshua~1\mydocu~1\");
      : : system("sleep.com 05");
      : :
      : : system("MOVE C:\*.xml C:\docume~1\joshua~1\mydocu~1\");
      : : system("sleep.com 05");}
      : : else
      : : if(cgm < xml)
      : : {MessageBox (NULL, "Missing CGM file.
      Check graphics go here folder for missing CGM file." , "Hello1", 0);
      : : return -1;}
      : : else
      : : {MessageBox (NULL, "Missing XML file
      Check graphics go here folder for missing XML file." , "Hello2", 0);
      : : return -1;}
      : :
      : :
      : : chdir(PARSE_IMPORT);
      : : cgm = 0;
      : : xml = 0;
      : :
      : : if ((res = _findfirst("*.cgm", &finddata))==-1)
      : : printf("NO CGM files found
      ");
      : :
      : : do{
      : : cgm++;
      : : }while(!_findnext(res, &finddata));
      : : [red] _findclose(res);[/red]
      : :
      : : printf("%d
      ", cgm);
      : :
      : : if ((res = _findfirst("*.xml", &finddata))==-1)
      : : printf("NO XML files found
      ");
      : :
      : : do{
      : : xml++;
      : : }while(!_findnext(res, &finddata));
      : : [red] _findclose(res);[/red]
      : :
      : : printf("%d
      ", xml);
      : :
      : : if((cgm == 0)&&(xml == 0))
      : : printf("No CGM or XML files found.
      ");
      : : else
      : : if(cgm == xml)
      : : {system("net use H: \\server\MUSIC"); /*Maps server address to drive H: */
      : :
      : : system("COPY C:\*.cgm H:\docume~1\joshua~1\mydocu~1\");
      : : system("sleep.com 05");
      : :
      : : system("COPY C:\*.xml H:\docume~1\joshua~1\mydocu~1\");
      : : system("sleep.com 05");
      : :
      : : system("net use H: /delete");} /*Deletes the use of drive H: */
      : : else
      : : if(cgm < xml)
      : : {printf("Missing CGM file.
      ");
      : : return -1;}
      : : else
      : : {printf("Missing XML file.
      ");
      : : return -1;}
      : :
      : : return 0;
      : : }
      : :
      : [/code]:
      : :
      :
      :


      Thanks for the help. I added the 2 libraries to my MSVC libraries. I noticed they were missing, but were included in my LCC libraries. After getting my code to work and compiling it. I noticed a couple of things were missing. I added some elses as shown in blue. I just did one set to show. I was thinking of putting the counts in as a function and just calling up that function when I need it.
  • Griz803Griz803 Member Posts: 100
    A quick note to say that I'm sorry for having given you any incomplete or inaccurate info earlier. And to thank Stober for clearing that up, it has been a long time since I worked with anything but *nix and older compilers. I am sorry to have misled you.
  • stoberstober Member Posts: 9,765 ✭✭✭
    : I was thinking of putting the counts in as a function and just calling up that function when I need it.
    :

    That's the easiest way to do it, and more versatile too -- it can count the number of files of any specified file extension.
Sign In or Register to comment.